矩阵行的平方和
sum of squaresum of rows of the matrix
任务是在不使用数组的情况下输出每一行的平方和之和。我已经编写了一个代码并且它完成了工作,只是我找不到一种方法可以根据第一个输入动态调整矩阵的大小,这将是矩阵的大小,而不是按 'enter'在每个元素之后,用户可以用 'space' 分隔每个元素并用 'enter' 分隔行,就像在实际矩阵中一样。如果有办法,请告诉我。
这是需要在每个元素后按 'enter' 的代码。
/*Take matrix size as input. read each element and give out the sum of squaresum of rows of the matrix*/
#include <stdio.h>
int main(){
int m,n; //matrix size row,column
int rowsum = 0,sum = 0,a,col,row = 0;
printf("Enter the size of matrix\n");
scanf("%d %d",&m,&n); //take row and col
while (row!=m) //calculate sum of each element of column one by one row-wise
{
col = 0; //start from col 1 i.e col 0
rowsum = 0;
printf("Enter elements of %d row\n",row+1);
while (col!=n){ //calculate sum of each element of rows one by one col-wise
scanf("%d",&a); //read the element
rowsum += a; //add read element to sum of that row
col ++; //move to next element
}
sum += rowsum*rowsum; //add the sq. of that rowsum before moving on to next
row ++; //move to next row
}
printf("%d is sum of squaresum of rows",sum);
return 0;
}
提前致谢。
您可以在扫描时去掉下一个字符,这样您就可以在数字后只输入一个 space。此外,您可以通过这种方式在数字之间放置任何字符。使用“%*c”在扫描时跳过一个字符。这是代码:
/*Take matrix size as input. read each element and give out the sum of squaresum of rows of the matrix*/
#include <stdio.h>
int main(){
...
while (row!=m) //calculate sum of each element of column one by one row-wise
{
col = 0; //start from col 1 i.e col 0
rowsum = 0;
printf("Enter elements of %d row\n",row+1);
while (col!=n){ //calculate sum of each element of rows one by one col-wise
scanf("%d%*c",&a); //read the element
rowsum += a; //add read element to sum of that row
col ++; //move to next element
}
...
}
return 0;
}
当大小选择为 (2, 3) 时,一行的一些可能输入:
Enter the size of matrix
2 3
Enter elements of 1 row
1;2;3
Enter elements of 2 row
4;5;6
输出:
261 is sum of squaresum of rows
另一种可能的方式:
Enter the size of matrix
2 3
Enter elements of 1 row
1 2 3
Enter elements of 2 row
4 5 6
输出同上
任务是在不使用数组的情况下输出每一行的平方和之和。我已经编写了一个代码并且它完成了工作,只是我找不到一种方法可以根据第一个输入动态调整矩阵的大小,这将是矩阵的大小,而不是按 'enter'在每个元素之后,用户可以用 'space' 分隔每个元素并用 'enter' 分隔行,就像在实际矩阵中一样。如果有办法,请告诉我。 这是需要在每个元素后按 'enter' 的代码。
/*Take matrix size as input. read each element and give out the sum of squaresum of rows of the matrix*/
#include <stdio.h>
int main(){
int m,n; //matrix size row,column
int rowsum = 0,sum = 0,a,col,row = 0;
printf("Enter the size of matrix\n");
scanf("%d %d",&m,&n); //take row and col
while (row!=m) //calculate sum of each element of column one by one row-wise
{
col = 0; //start from col 1 i.e col 0
rowsum = 0;
printf("Enter elements of %d row\n",row+1);
while (col!=n){ //calculate sum of each element of rows one by one col-wise
scanf("%d",&a); //read the element
rowsum += a; //add read element to sum of that row
col ++; //move to next element
}
sum += rowsum*rowsum; //add the sq. of that rowsum before moving on to next
row ++; //move to next row
}
printf("%d is sum of squaresum of rows",sum);
return 0;
}
提前致谢。
您可以在扫描时去掉下一个字符,这样您就可以在数字后只输入一个 space。此外,您可以通过这种方式在数字之间放置任何字符。使用“%*c”在扫描时跳过一个字符。这是代码:
/*Take matrix size as input. read each element and give out the sum of squaresum of rows of the matrix*/
#include <stdio.h>
int main(){
...
while (row!=m) //calculate sum of each element of column one by one row-wise
{
col = 0; //start from col 1 i.e col 0
rowsum = 0;
printf("Enter elements of %d row\n",row+1);
while (col!=n){ //calculate sum of each element of rows one by one col-wise
scanf("%d%*c",&a); //read the element
rowsum += a; //add read element to sum of that row
col ++; //move to next element
}
...
}
return 0;
}
当大小选择为 (2, 3) 时,一行的一些可能输入:
Enter the size of matrix
2 3
Enter elements of 1 row
1;2;3
Enter elements of 2 row
4;5;6
输出:
261 is sum of squaresum of rows
另一种可能的方式:
Enter the size of matrix
2 3
Enter elements of 1 row
1 2 3
Enter elements of 2 row
4 5 6
输出同上