Q:

C Program to find sum of all elements of each row of a matrix

1

C Program to find sum of all elements of each row of a matrix

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

This C program will read a Matrix (two dimensional arrays) and print the sum of all elements of each row.

#include <stdio.h>

#define MAXROW		10
#define MAXCOL		10

int main()
{
	int matrix[MAXROW][MAXCOL];
	int i,j,r,c;
	int sum,product;
	
	printf("Enter number of Rows :");
	scanf("%d",&r);
	printf("Enter number of Cols :");
	scanf("%d",&c);

	printf("\nEnter matrix elements :\n");
	for(i=0;i< r;i++)
	{
		for(j=0;j< c;j++)
		{
			printf("Enter element [%d,%d] : ",i+1,j+1);
			scanf("%d",&matrix[i][j]);
		}
	}
	printf("\n");
	/*sum of all rows*/
	for(i=0;i< r;i++)
	{
		sum=0;		/*initializing sum*/
		for(j=0;j< c;j++)
		{
			printf("%d\t",matrix[i][j]);	/*print elements*/
			sum		+=	matrix[i][j];
		}
		printf("\tSUM : %d",sum);
		printf("\n");	/*after each row print new line*/		
	}

}

Output

    Enter number of Rows :3
    Enter number of Cols :3

    Enter matrix elements :
    Enter element [1,1] : 1
    Enter element [1,2] : 2
    Enter element [1,3] : 3
    Enter element [2,1] : 4
    Enter element [2,2] : 5
    Enter element [2,3] : 6
    Enter element [3,1] : 7
    Enter element [3,2] : 8
    Enter element [3,3] : 9

    1	2	3		SUM : 6
    4	5	6		SUM : 15
    7	8	9		SUM : 24

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program to read a matrix and find sum, product o... >>
<< C program to transpose a matrix...