0% found this document useful (0 votes)
23 views2 pages

Week 5 Assignment

The document contains code for a C program that reads data from a text file into a two-dimensional array, calculates the average of the data, and prints the result. The program defines a function to calculate the average of a two-dimensional array and calls this function to print the average.

Uploaded by

Rajat Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Week 5 Assignment

The document contains code for a C program that reads data from a text file into a two-dimensional array, calculates the average of the data, and prints the result. The program defines a function to calculate the average of a two-dimensional array and calls this function to print the average.

Uploaded by

Rajat Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <math.h>
#include <malloc.h>
#include <stdlib.h>

double average(int row,int column,int mat[row][column])


{

int a;
int b;
double sum=0.0;
int t = 1000;

for(a=0;a<row;a++)
{
for(b=0;b<column;b++)
{
sum+=mat[a][b];
}
}
return(sum/t);

int main()

int i;
int j;
int r=50;
int col=20;
FILE *file;

int** mat=malloc(r*sizeof(int*));

for(i=0;i<r;++i)
{
mat[i]=malloc(r*sizeof(int));
}

file=fopen("data.txt", "r");/Text file consisting of raw data in 50 rows and 20


columns/

for(i = 0; i < 1000; i++)


{
for(j = 0; j <col; j++)
{
if (!fscanf(file, "%d", &mat[i][j]))
break;
printf("%d\t",mat[i][j]);

if(j==col-1)
{
printf("\n\n");
}

printf("Average of elephant seal is %.2f\n\n",average(r,col,mat));


}
}

fclose(file);
return 0;

You might also like