C programming brain teaser . I have the following function and as you can imagin
ID: 3588731 • Letter: C
Question
C programming brain teaser. I have the following function and as you can imagine compiling it with the file below it reads the data file and ignores the comment. Now I want to write a function int read_something(FILE *fp, double *height, double *weight); that reads the file and store height and weight info into the arrays so it returns the number of data items actually stored in the arrays. and also another function int average( double *x, int N, double *d); where x is the data array, N is the number of data item. So the function computes the average and standard deviation which are stored into d[0] and d[1] respectively.
#include # include #define MAX BUFFER 1024 int main(int argc, char *argv[]) 7 FILE *fin; char buffer [NAX BUFFER]: 9 10 if (argcExplanation / Answer
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
int average(double a[], int n, double *d){
double sum = 0;
int i;
for (i = 0; i<n; i++){
sum = sum + a[i];
}
d[0] = sum/n;
sum = 0;
for ( i = 0; i<n; i++){
sum = sum + ((a[i] - d[0]) * (a[i] - d[0]));
}
d[1] = sqrt(sum/n);
}
int main(int argc, char *argv[]){
FILE *fin;
double height[100];
double weight[100];
int count,i;
double d[2];
double d1[2];
if (argc < 2){
fprintf(stderr,"Usage %s filename ",argv[0]);
exit(1);
}
fin = fopen(argv[1],"r");
if (!fin){
printf("Error opening file ");
exit(0);
}
count = 0;
while (fscanf(fin,"%lf%lf", &height[count], &weight[count]) > 0){
count++;
}
printf("%5s%15s %-15s ","#index", "Height(inches)", "Weight(Pounds)");
for (i = 0 ; i<count; i++){
printf("%5d%15lf %-15lf ",i+1, height[i], weight[i]);
}
average(height,count,d);
average(weight,count,d1);
printf("Height %lf %lf ",d[0],d[1]);
printf("Weifht %lf %lf ",d1[0],d1[1]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.