C Programming code for Sum each Row Col 2D Array – KLETech

Write a Modular C Programming Code to Sum each Row & Columns of a 2D Array?

CODE:

#include<stdio.h>
#define ROW 10
#define COL 10

void ReadArray(int [ROW][COL], int, int);
void DisplayArray(int [ROW][COL], int, int);
void SumEachRow(int [ROW][COL], int, int);
void SumEachCol(int [ROW][COL], int, int);

main()
{
int r,c,sum;
int a[ROW][COL];
double avg;
printf("Enter the order of 2D matrix: ");
scanf("%d%d",&r,&c);
printf("\nEnter the Elements of Matix:\n");
ReadArray(a,r,c);
printf("\nThe Elements of Matix are\n");
DisplayArray(a,r,c);
SumEachRow(a,r,c);
SumEachCol(a,r,c);
}

void ReadArray(int a[ROW][COL], int r, int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
}

void DisplayArray(int a[ROW][COL], int r, int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}

void SumEachRow(int a[ROW][COL], int r, int c)
{
int i,j,sum=0;
for(i=0;i<r;i++)
{
sum=0;
for(j=0;j<c;j++)
{
sum=sum+a[i][j];
}
printf("Sum of %dth row is: %d\n",i,sum);
}
}

void SumEachCol(int a[ROW][COL], int r, int c)
{
int i,j,sum=0;
for(j=0;j<c;j++)
{
sum=0;
for(i=0;i<r;i++)
{
sum=sum+a[i][j];
}
printf("Sum of %dth Column is: %d\n",j,sum);
}
}

OUTPUT

Enter the order of 2D matrix: 3 3

Enter the Elements of Matix:
5
9
5
1
3
6
5
4
9

The Elements of Matix are
5 9 5
1 3 6
5 4 9
Sum of 0th row is: 19
Sum of 1th row is: 10
Sum of 2th row is: 18
Sum of 0th Column is: 11
Sum of 1th Column is: 16
Sum of 2th Column is: 20

Process returned 0 (0x0) execution time : 13.245 s
Press any key to continue.

 

Please find some more codes of 1D Arrays, 2D Arrays, Pointers on the below page:

Top 100+ C Programming codes – KLE Technological University

 







Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!