i need help writing a program. and please explain the steps if possible. there a
ID: 3548661 • Letter: I
Question
i need help writing a program. and please explain the steps if possible.
there are 1000 integers in a file called "data.txt". assume they are the experiment data you obtained, and now you want to write a program to do data analysis. it should include:
-write a function to find the average value of the 1000 data.
-write a function to calculate the average absolute deviation of all the data.
-write a function to calculate the variance of all the data.
you can use the function fabs(x) defined in <math.h> or do it in another way.
Explanation / Answer
#include<stdio.h>
#include<math.h>
//Function for finding average
float getAvg(int data[], int num)
{
int i;
float avg=0;
float nums;
//type casting num(int) to nums(float) to avoid integer division in loop
nums = (float)num;
for(i=0;i<num;i++)
{
avg+=data[i]/nums;
}
return avg;
}
//Function for finding Average absolute Deviation
float getAvgAbsDev(int data[], int num, float avg)
{
int i;
float dev = 0;
for(i=0;i<num;i++)
{
dev+=fabs(data[i] - avg)/num;
}
return dev;
}
//function for finding variance
float getVariance(int data[], int num, float avg)
{
int i;
float variance=0;
for(i=0;i<num;i++)
{
variance+=(data[i]-avg)*(data[i]-avg)/num;
}
return variance;
}
int main()
{
FILE *fp;
fp = fopen("data.txt","r");
int i,num,data[1000];
for(i=0;fscanf(fp,"%d",&data[i])!=EOF;i++);
//storing the number of elements scanned
num=i;
float avg = getAvg(data,num);
printf("average=%f ",avg);
printf("average absolute deviation=%f ",getAvgAbsDev(data,num,avg));
printf("variance=%f ",getVariance(data,num,avg));
return 0;
}
sourcecode : http://kopasite.net/up/yo4pvdl7r743yxr/BraveBattleship4289.c
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.