In: Computer Science
C language you are to write a a program that will first read in the heights and weights of a series of people from a file named values.dat. Create this file using your editor so that each line contains a height and corresponding weight. For instance, it might look likea: 69.0 125.0 44.0 100.0 60.0 155.0 49.0 190.0 65.0 115.0 50.0 80.0 30.0 129.0 72.0 99.0 68.0 122.0 50.0 105.0 and so on.
The formula for the standard deviation of a series of numbers x[0], x[1] ... x[n] is std = sqrt( sum( (x[i] - xbar)**2 ) / (n) ) where xbar is the average value of x, n is the number of people, and **2 means squared.
------header file------
#define MAXNUM 100
typedef struct person
{
double height;
double weight;
} Person;
//prototypes follow:
int getData(FILE *input, Person people[]);
void getAverages(Person people[], double *aveHeight, double *aveWeight, int
numPeople);
void getStandardDevs(Person people[], double aveHeight, double aveWeight,
double *stdHeight, double *stdWeight, int numPeople);
--------main template ----------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXNUM 100
#include "stats.h"
void main( void )
{
FILE *input;
Person people[MAXNUM];
int numPeople = 0;
double aveHeight = 0.0, aveWeight = 0.0, stdHeight = 0.0, stdWeight = 0.0;
numPeople = getData(input, people);
getAverages(people, &aveHeight, &aveWeight, numPeople);
getStandardDevs(people, aveHeight, aveWeight, &stdHeight, &stdWeight, numPeople);
printf("\n\n\n\n\n\nThe average height is %lf\n", aveHeight);
printf("The average weight is %lf\n", aveWeight);
printf("The standard deviation of the heights is %lf\n", stdHeight);
printf("The standard deviation of the weights is %lf\n\n\n\n\n\n", stdWeight);
}
int getData( FILE* input, Person people[])
{
int numPeople = 0;
input = fopen( "values.dat", "r" ); // How to read file
if ( input == NULL )
{
printf( "\"values.dat\" does not exist!!\n" );
}
/*
your code here
*/
fclose(input);
return numPeople;
} // getData()
void getAverages( Person people[], double* aveHeight, double* aveWeight, int
numPeople)
{
/*
your code here
*/
} // getAverages()
void getStandardDevs( Person people[], double aveHeight, double aveWeight, double*
stdHeight, double* stdWeight, int numPeople )
{
/*
your code here
*/
} // getStandardDevs()
------header file------
#define MAXNUM 100
typedef struct person
{
double height;
double weight;
} Person;
//prototypes follow:
int getData(FILE *input, Person people[]);
void getAverages(Person people[], double *aveHeight, double *aveWeight, int
numPeople);
void getStandardDevs(Person people[], double aveHeight, double aveWeight,
double *stdHeight, double *stdWeight, int numPeople);
--------main template ----------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXNUM 100
#include "stats.h"
void main(void)
{
FILE* input=NULL;
Person people[MAXNUM];
int numPeople = 0;
double aveHeight = 0.0, aveWeight = 0.0, stdHeight =
0.0, stdWeight = 0.0;
numPeople = getData(input, people);
getAverages(people, &aveHeight, &aveWeight,
numPeople);
getStandardDevs(people, aveHeight, aveWeight,
&stdHeight, &stdWeight, numPeople);
printf("\n\n\n\n\n\nThe average height is %lf\n",
aveHeight);
printf("The average weight is %lf\n",
aveWeight);
printf("The standard deviation of the heights is
%lf\n", stdHeight);
printf("The standard deviation of the weights is
%lf\n\n\n\n\n\n", stdWeight);
}
int getData(FILE* input, Person people[])
{
int numPeople = 0;
#pragma warning(disable : 4996)
input = fopen("values.dat", "r"); // How to read
file
if (input == NULL)
{
printf("\"values.dat\" does not
exist!!\n");
}
double h, w;
while (fscanf(input, "%lf %lf", &h, &w) != EOF)
{
Person p = { h,w };
people[numPeople++] = p;
}
;
fclose(input);
return numPeople;
} // getData()
void getAverages(Person people[], double* aveHeight, double*
aveWeight, int
numPeople)
{
double sumH = 0;
double sumW = 0;
for (int i = 0; i < numPeople; i++)
{
sumH += people[i].height;
sumW += people[i].weight;
}
*aveHeight = sumH / numPeople;
*aveWeight = sumW / numPeople;
} // getAverages()
void getStandardDevs(Person people[], double aveHeight, double
aveWeight, double*
stdHeight, double* stdWeight, int numPeople)
{
double h = 0;
double w = 0;
for (int i = 0; i < numPeople; i++)
{
h += pow((people[i].height -
aveHeight),2);
w += pow((people[i].weight -
aveWeight),2);
}
h = h / numPeople;
w = w / numPeople;
*stdHeight = sqrt(h);
*stdWeight = sqrt(w);
} // getStandardDevs()