C 将矩阵的一行乘以给定的数
我假设将给定矩阵的某一行(我在函数的第四个参数中指定哪一行)乘以一个数字 主要功能:C 将矩阵的一行乘以给定的数,c,matrix,C,Matrix,我假设将给定矩阵的某一行(我在函数的第四个参数中指定哪一行)乘以一个数字 主要功能: int main_tp05(int argc, const char *argv[]){ int mNx100[][MAXCOLS100] = {{1,2,3},{4,5,6},{7,8,9}}; multiply_matrixNx100_line_by_scalar(mNx100,3,3,1,2); return 0; } 我试着这样解决它: void multiply_matri
int main_tp05(int argc, const char *argv[]){
int mNx100[][MAXCOLS100] = {{1,2,3},{4,5,6},{7,8,9}};
multiply_matrixNx100_line_by_scalar(mNx100,3,3,1,2);
return 0;
}
我试着这样解决它:
void multiply_matrixNx100_line_by_scalar(int mNx100[][MAXCOLS100], int lines, int columns, int line, int scalar){
for (int i = 0; i < lines; i++) {
for (int j = 0; j < columns; j++) {
if(i == line){
printf("%d\n", mNx100[i*scalar][j] );
}
}
printf("\n");
}
}
我写了这个代码,并通过自己的验证,它工作成功
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,a[5][5],b,c,d,m,n,r;
printf("Enter the number of rows and columns\n");
scanf("%d %d",&m,&n);
printf("Enter the matrix\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the number to multiply the matrix\n");
scanf("%d",&b);
printf("Enter 0 to multiply row or 1 to multiply column\n");
scanf("%d",&d);
if(d==0)
{
printf("Enter the row number to be multiplied\n");
scanf("%d",&r);
for(i=r,j=1;j<=n;j++)
{
a[i][j]*=b;
}
}
else if(d==1)
{
printf("Enter the row number to be multiplied\n");
scanf("%d",&c);
for(j=c,i=1;i<=m;i++)
{
a[i][j]*=b;
}
}
printf("matrix after multiplied\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
#包括
#包括
int main()
{
int i,j,a[5][5],b,c,d,m,n,r;
printf(“输入行数和列数\n”);
scanf(“%d%d”,&m,&n);
printf(“输入矩阵\n”);
对于(i=1;i
我假设将给定矩阵的某一行(我在函数的第四个参数中指定哪一行)乘以一个数字
通常,“将矩阵的一行乘以标量”意味着将该行的每个元素乘以某个标量值。POST函数不是这样做的,它将该行的索引乘以传递的参数:
void multiply_matrixNx100_line_by_scalar(int mNx100[][MAXCOLS100], int lines, int columns,
int line, int scalar) {
for (int i = 0; i < lines; i++) { // <-- Useless, the row is known
for (int j = 0; j < columns; j++) {
if(i == line){
printf("%d\n", mNx100[i*scalar][j] );
// ^^^^^^^^
}
}
printf("\n");
}
}
在循环内部,代替调用printf
您尝试了什么,哪些不起作用?我尝试了这个问题中的方法,只做了一些小动作,但似乎没有任何效果来注意到一些有价值的进展。尝试mNx100[I*scalar][j]
-->mNx100[scalar][j]*I
@chux它不起作用mNx100[I*scalar[j] -->mNx100[i][j]*标量;
void multiply_matrixNx100_line_by_scalar(int mNx100[][MAXCOLS100], int lines, int columns,
int line, int scalar) {
for (int i = 0; i < lines; i++) { // <-- Useless, the row is known
for (int j = 0; j < columns; j++) {
if(i == line){
printf("%d\n", mNx100[i*scalar][j] );
// ^^^^^^^^
}
}
printf("\n");
}
}
void multiply_matrixNx100_line_by_scalar(int mNx100[][MAXCOLS100], int lines, int columns,
int line, int scalar) {
if (line < 0 || line >= lines)
return;
for (int j = 0; j < columns; j++) {
printf("%d\n", mNx100[line][j] * scalar);
}
printf("\n");
}
mNx100[line][j] *= scalar;