Gurmel Singh
Gurmel Singh

Reputation: 13

How to scan in matrix and add sum the row and col

include <stdio.h>

int main(){
    int number [5][5] , row=0, col=0,sumrow[5], sumcol[5];

        for(row=0;row<5;row++)
        {
            printf("Enter row %d ", row+1);

            for(col=0;col<5;col++)
            scanf("%d",&number[row][col]);
        }

// I want to read in row and col of numbers

       for(row=0;row<5,sumrow[row]=sumcol[col]=0;row++);
        for(row=0;row<5;row++)
            for(col=0;col<5;col++)
            {
                    sumrow[row]=sumrow[row]+number[row][col];
                    sumcol[col]=sumrow[col]+number[row][col];
            }
        // here I want to plus all the row in sumrow and col in sumcol

     printf("Row totals ");
        for(row=0;row<5;row++) {
            printf("%d ",sumrow[row]);
        }
            printf("\n Row totals ");
            for(col=0;col<5;col++){
            printf("%d ",sumrow[col]);
            }

    return 0;
    }

This code is not working. I want to read the matrix and all rows and columns. I don't know what the problem is.

Upvotes: 1

Views: 93

Answers (2)

Sagar Patel
Sagar Patel

Reputation: 852

  • If u want to add all elements of row and column or sum of all rows and column below is the code,

    #include <stdio.h>
    
    int main ()
    {
        static int array[10][10];
        int i, j, m, n, sum = 0;
        int rowSum = 0;
        int colSum = 0;
        printf("Enter the order of the matrix\n");
        scanf("%d %d", &m, &n);
        printf("Enter the elements of the matrix\n");
        for (i = 0; i < m; ++i)
        {
            for (j = 0; j < n; ++j)
            {
               scanf("%d", &array[i][j]);
            }
        }
        for (i = 0; i < m; ++i)
        {
            for (j = 0; j < n; ++j)
            {
               sum = sum + array[i][j] ;
            }
            printf("Sum of the %d row is = %d\n", i, sum);
        rowSum += sum;
        sum = 0;
        }
        printf("Sum of all the row is = %d\n", rowSum);
        sum = 0;
        for (j = 0; j < n; ++j)
        {
           for (i = 0; i < m; ++i)
           {
             sum = sum + array[i][j];
           }
           printf("Sum of the %d column is = %d\n", j, sum);
           colSum += sum;
           sum = 0;
        }
        printf("Sum of all the column is = %d\n", colSum);
     }
    

Upvotes: 1

Gabriel
Gabriel

Reputation: 763

I don't know if I understood correctly, but if you want to sum all the elements of the matrix you should use this for example:

int a[5][5],n,m,i,j, soma=0;
printf("Enter value of m and n: ");
scanf("%d%d",&m,&n);
printf("\nEnter elemets of the matrix:\n");

for(i=0;i<m;++i)
for(j=0;j<n;++j)
    scanf("%d",&a[i][j]);

for(i=0;i<m;++i){
    sum_col += i;
}

for(j=0;j<n;++j){
    sum_row += ;
}

printf("\nYour sum is: %d", soma);
getch();
return 0;

PS: I also used conio library, so you should use #include<conio.h> at the beginning

But if you want to sum the numb of rows and the numb of cols and print one by one, you should use this one:

int number [5][5] , row=0, col=0,sumrow=0, sumcol=0, cont_1=0, cont_2=0;


        for(row=0;row<5;row++)
        {
            printf("Enter row %d ", row+1);

            for(col=0;col<5;col++)
            scanf("%d",&number[row][col]);
        }

// I want to read in row and col of numbers

       for(row=0;row<5;row++){
            cont_1++;
            for(col=0;col<5;col++)
            {
                    sumcol += number[row][col];

            }
            printf("\nThe sum of Col %d is %d", cont_1, sumcol);
            sumcol = 0;
       }

       for(col=0;col<5;col++){
            cont_2++;
            for(row=0;row<5;row++) 
            {
                    sumrow += number[row][col];

            }
            printf("\nThe sum of Row %d is %d", cont_2, sumrow);
            sumrow = 0;
       }


    return 0;

I just made your code more clear, so you should understand what you are doing.

Upvotes: 0

Related Questions