下列程序在主程序中初始化一个矩阵并将每个元素输出,然后调用子函数,分别计算每一行元素之和,将和直接存放在每行的第一个元素中,返回主函数之后输出各行元素的和。
#include “stdio.h”
void RowSum(int A[][4], int nrow)
{
for (int i = 0; i < nrow; i++)
{for(int j = 1; j < 4; j++)
A[i][0] += A[i][j];
}
}
void main()
{int Table[3][] = {{1,2,3,4},{2,3,4,5},{3,4,5,6}}; ★
for (int i = 0; i < 3; i++)
{for(int j = 0; j < =4; j++)
printf(“%d “,Table[i][j] );
printf(“\n”);
}
RowSum(Table[3][4],3); ★
for (i = 0; i < 3; i++)
printf( “Sum of row%d is %d \n”,i ,Table[i][0]);
}
int Table[3][] = {{1,2,3,4},{2,3,4,5},{3,4,5,6}};
改为:int Table[3][4] = {{1,2,3,4},{2,3,4,5},{3,4,5,6}};
或: int Table[][4] = {{1,2,3,4},{2,3,4,5},{3,4,5,6}};
RowSum(Table[3][4],3);
改为:RowSum(Table,3);
暂无评论内容