C Programming code for Sum & Avg of 2D Array – KLE Tech

Write a Modular C Programming Code for Sum & Avg 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);
int SumMatrix(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);
sum=SumMatrix(a,r,c);
printf("The SUM of the Matrix is: %d\n",sum);
avg=(double)sum/r*c;
printf("The Avg of the Matrix is: %lf\n",avg);
}

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");
}
}

int SumMatrix(int a[ROW][COL], int r, int c)
{
int i,j,sum=0;

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
sum=sum+a[i][j];
}

}
return sum;
}

OUTPUT

Enter the order of 2D matrix: 2 3

Enter the Elements of Matix:
5
3
2
6
4
2

The Elements of Matix are
5 3 2
6 4 2
The SUM of the Matrix is: 22
The Avg of the Matrix is: 33.000000

Process returned 0 (0x0) execution time : 13.563 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 !!!