Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //write a program that calculates the sum of rows and sum of columns ,
- //the user inputs the matrix and it's dimensions .
- #define MAX_ROWS 10
- #define MAX_COLS 10
- #include <stdio.h>
- int main()
- {
- int row,col ; //dimensions
- //receiving dimensions :
- printf("Input the matrix dimensions : ");
- scanf("%d%d",&row,&col);
- //receiving elements :
- printf("Enter the matrix elements : ");
- float x[MAX_ROWS][MAX_COLS] ; //subscripts of an array must be constants
- int i; int j;
- for (i=0;i<row;i++)
- {
- for (j=0;j<col;j++)
- {
- scanf("%f",&x[i][j]);
- }
- }
- //printing matrix :
- for (i=0;i<row;i++)
- {
- for (j=0;j<col;j++)
- printf("%.2f\t",x[i][j]);
- printf("\n");
- }
- //sum of rows :
- float sum_row[MAX_ROWS] = {0.0};
- printf("\n");
- for (i=0;i<row;i++)
- {
- for (j=0;j<col;j++)
- {
- sum_row[i] += x[i][j];
- }
- printf("sum of row %d = %.2f\n",i,sum_row[i]);
- }
- //sum of columns :
- float sum_column[MAX_COLS]= {0.0};
- printf("\n");
- printf("\n");
- for (j=0;j<col;j++)
- {
- for (i=0;i<row;i++)
- {
- sum_column[j] += x[i][j];
- }
- printf("sum of column %d = %.2f\n",j,sum_column[j]);
- }
- printf("\n");
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment