Question

In: Computer Science

C language you are to write a a program that will first read in the heights...

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()

Solutions

Expert Solution

------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()


Related Solutions

For your first project, write a C program (not a C++ program!)that will read in a...
For your first project, write a C program (not a C++ program!)that will read in a given list of non-negative integers and a target integer and checks if there exist two integers in the list that sum up to the target integer. Example:List: 31, 5, 8, 28, 15, 21, 11, 2 Target: 26 Yes!, 44 No! your C program will contain the following: •Write a function that will make a copy of the values from one array to another array....
C++ programming language. Write a program that will read in id numbers and place them in...
C++ programming language. Write a program that will read in id numbers and place them in an array.The array is dynamically allocated large enough to hold the number of id numbers given by the user. The program will then input an id and call a function to search for that id in the array. It will print whether the id is in the array or not. Sample Run: Please input the number of id numbers to be read 4 Please...
C++ The program you write for this lab will read in the number of nodes and...
C++ The program you write for this lab will read in the number of nodes and a binary relation representing a graph. The program will create an adjacency matrix from the binary relation. The program will then print the following : 1. The adjacency matrix 2. Determine if there are any isolated nodes and print them 3. Determine if an Euler path exists and said so. The sample run of the program is as follows. The output should just like...
IN C LANGUAGE This program will read in a series of strings and print only the...
IN C LANGUAGE This program will read in a series of strings and print only the consonants (including Y) until the word "stop" appears. No string will be longer than 100 characters. A consonant is any letter that is not a vowel. Don't forget to follow the standard read pattern! Examples Enter a string: Hello Hll Enter a string: World! Wrld Enter a string: 123! Enter a string: stop Enter a string: stop
C++ Funcion For this lab you need to write a program that will read in two...
C++ Funcion For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using Euclidean algorithm) to a file. You will also need to demonstrate using ostream and ostringstream by creating 2 functions to output your print heading: one that uses ostream and the other uses ostringstream. Using ostream and ostringstream Write two PrintHeader functions that will allow you to output to the screen and to an...
Write a program that calculates the compound interest for an investment. (C++ coding language) If you...
Write a program that calculates the compound interest for an investment. (C++ coding language) If you deposit an amount of money P , the principal, at an interest rate r then the interest will compound over time. This means that the interest earned each period becomes part of the principal and the next time you get interest you earn interest on the interest. This is known as compounding. The equation for compound interest is ( r)n·t Pn=P0 1+n where P0...
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
Assembly Language for x86 processors You are to write a program which should first ask for...
Assembly Language for x86 processors You are to write a program which should first ask for 4 random numbers from 0-20 (user will inpute these numbers in no preset order). Input these 5 numbers in variables called num1, num2, num3, num4, and num5. When done, your program should sort these numbers (you will use lots of conditions to check order). num1 should contain smallest number while num5 should contain the biggest. display the contents of num1 through num5 on the...
Write the following in C language for Arduino: Write a program that turns on the LED...
Write the following in C language for Arduino: Write a program that turns on the LED at 25%, 50%, 75%, 100%, and then 0% brightness with a one second delay in between each change. Remember you are going to need to use a PWM pin and use the "analogWrite" command. The maximum value for our Arduino R3 boards is 255 and you need five steps (25%, 50%, 75%, 100%, and 0%) so you will need to determine the values for...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT