-
Notifications
You must be signed in to change notification settings - Fork 2
/
File_Array_total_sum.c
59 lines (48 loc) · 1.1 KB
/
File_Array_total_sum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// find average weight of number of arr
// author: jaydattpatel
#include<stdio.h>
#include <stdlib.h>
float average_fun(float x[],int size)
{
float sum,avg;
for(int i=0;i<size;i++)
{
sum = x[i] + sum;
}
avg = sum/size;
return(avg);
};
void display(float x[],int size)
{
for(int i=0;i<size;i++)
printf("\n(%d) %f",i+1,x[i]);
};
int main()
{
float average;
float arr[2000];
int data,size=0;
FILE *fp;
fp = fopen("array_data.txt", "r");
if(fp == NULL)
{
printf("fail to open file!");
exit(1);
}
rewind(fp);
int i=0;
while(fgetc(fp) != EOF)
{
data=0;
if(fscanf(fp,"%d",&data)==1){
arr[i++] = (float)data;
size++;
printf("%d\t",data);
};
}
printf("\nSize:%d",size);
fclose(fp);
average = average_fun(arr,size);
printf("\nAverage = %f",average);
return 0;
}