AnonymousEng

Array-fixed

Jun 26th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. //write a program that calculates the sum of rows and sum of columns ,
  2. //the user inputs the matrix and it's dimensions .
  3. #define MAX_ROWS 10
  4. #define MAX_COLS 10
  5.  
  6.  
  7. #include <stdio.h>
  8. int main()
  9. {
  10.     int row,col ; //dimensions
  11.     //receiving dimensions :
  12.     printf("Input the matrix dimensions : ");
  13.     scanf("%d%d",&row,&col);
  14.     //receiving elements :
  15.     printf("Enter the matrix elements : ");
  16.     float x[MAX_ROWS][MAX_COLS] ; //subscripts of an array must be constants
  17.     int i; int j;
  18.     for (i=0;i<row;i++)
  19.     {
  20.         for (j=0;j<col;j++)
  21.         {
  22.             scanf("%f",&x[i][j]);
  23.         }
  24.     }
  25.     //printing matrix :
  26.     for (i=0;i<row;i++)
  27.     {
  28.         for (j=0;j<col;j++)
  29.             printf("%.2f\t",x[i][j]);
  30.         printf("\n");
  31.     }
  32.     //sum of rows :
  33.     float sum_row[MAX_ROWS] = {0.0};
  34.     printf("\n");
  35.     for (i=0;i<row;i++)
  36.     {
  37.         for (j=0;j<col;j++)
  38.         {
  39.             sum_row[i] += x[i][j];
  40.         }
  41.         printf("sum of row %d = %.2f\n",i,sum_row[i]);
  42.     }
  43.     //sum of columns :
  44.     float sum_column[MAX_COLS]= {0.0};
  45.     printf("\n");
  46.     printf("\n");
  47.     for (j=0;j<col;j++)
  48.     {
  49.         for (i=0;i<row;i++)
  50.         {
  51.             sum_column[j] += x[i][j];
  52.         }
  53.         printf("sum of column %d = %.2f\n",j,sum_column[j]);
  54.     }
  55.     printf("\n");
  56.     printf("\n");
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment