Question

In: Computer Science

Write a program that processes numbers, corresponding to student records read in from a file, and...

Write a program that processes numbers, corresponding to student records read in from a file, and writes the required results to an output file (see main ( )). Your program should define the following functions:

double read_double (FILE *infile) — Reads one double precision number from the input file. Note: You may assume that the file only contains real numbers.

int read_integer (FILE *infile) - Reads one integer number from the input file.

double calculate_sum (double number1, double number2, double number3, double number4, double number5) - Finds the sum of number1, number2, number3, number4, and number5 and returns the result.

double calculate_mean (double sum, int number) - Determines the mean through the calculation sum / number and returns the result. You need to check to make sure that number is not 0. If it is 0 the function returns -1.0 (we will assume that we are calculating the mean of positive numbers), otherwise it returns the mean.

double calculate_deviation (double number, double mean) - Determines the deviation of number from the mean and returns the result. The deviation may be calculated as number - mean.

double calculate_variance (double deviation1, double deviation2, double deviation3, double deviation4, double deviation5, int number) - Determines the variance through the calculation:

             ((deviation1)^2 + (deviation2)^2 + (deviation3)^2 + (deviation4)^2 + (deviation5)^2) / number

and returns the result. Hint: you may call your calculate_mean ( ) function to determine the result!

double calculate_standard_deviation (double variance) - Calculates the standard deviation as sqrt (variance) and returns the result. Recall that you may use the sqrt ( ) function that is found in math.h.

double find_max (double number1, double number2, double number3, double number4, double number5) — Determines the maximum number out of the five input parameters passed into the function, returning the max.

double find_min (double number1, double number2, double number3, double number4, double number5) — Determines the minimum number out of the five input parameters passed into the function, returning the min.

void print_double (FILE *outfile, double number) — Prints a double precision number (to the hundredths place) to an output file.

A main ( ) function that does the following (this is what the program does!!!):

Opens an input file "input.dat" for reading;

Opens an output file "output.dat" for writing;

Reads five records from the input file (input.dat); You will need to use a combination of read_double ( ) and read_integer ( ) function calls here!

Calculates the sum of the GPAs;

Calculates the sum of the class standings;

Calculates the sum of the ages;

Calculates the mean of the GPAs, writing the result to the output file (output.dat);

Calculates the mean of the class standings, writing the result to the output file (output.dat);

Calculates the mean of the ages, writing the result to the output file (output.dat);

Calculates the deviation of each GPA from the mean (Hint: need to call calculate_deviation ( ) 5 times)

Calculates the variance of the GPAs

Calculates the standard deviation of the GPAs, writing the result to the output file (output.dat);

Determines the min of the GPAs, writing the result to the output file (output.dat);

Determines the max of the GPAs, writing the result to the output file (output.dat);

Closes the input and output files (i.e. input.dat and output.dat)

Expected Input File Format (real numbers only):

For this assignment you will be required to read five records from the "input.dat" file. Each record will have the following form:

    Student ID# (an 8 digit integer number)

    GPA (a floating-point value to the hundredths place)

    Class Standing (1 - 4, where 1 is a freshmen, 2 is a sophomore, 3 is a junior, and 4 is a senior --> all integers)

    Age (a floating-point value)

Example data for 1 student record in the file could be as follows:

    12345678

    3.78

    3

    20.5

IV. Expected Results:

The following sample session demonstrates how your program should work.

Assuming input.dat stores the following records:

    12345678

    3.78

    3

    20.5

  

87654321

    2.65

    2

    19.25

   

08651234

    3.10

    1

    18.0

   

   

11112222

    3.95

    4

    22.5

   

22223234

    2.45

    3

    19.3333

Your program should write the following to output.dat: NOTE: you only need to output the numbers, the text is for demonstration purposes only.

        3.19     -- GPA Mean

        2.60     -- Class Standing Mean

        19.92   -- Age Mean

        0.60     -- GPA Standard Deviation

        2.45     -- GPA Min

        3.95     -- GPA Max

Solutions

Expert Solution

I have coded the requirement. Just one thing... the standard deviation function code, which you have provided, is wrong. It should be

  • Take the Mean (the simple average)
  • for each number: subtract the Mean and square the result
  • Then find out the mean of all squared differences.
  • Take the square root of that. This is Standard deviation.

main.c

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

double read_double (FILE *infile) {
   double d;
   fscanf(infile,"%lf",&d);
   return d;
}

int read_integer (FILE *infile) {
   int d;
   fscanf(infile,"%d",&d);
   return d;     
}

double calculate_sum (double number1, double number2, double number3, double number4, double number5) {
       return number1 + number2 + number3 + number4 + number5;
}

double calculate_mean (double sum, int number) {
   if(number == 1) {
       return -1;
   }
   return sum/number;
}
double calculate_deviation (double number, double mean) {
   return number - mean;
}
double calculate_variance (double deviation1, double deviation2, double deviation3, double deviation4, double deviation5, int number) {
   return calculate_mean( pow(deviation1, 2) + pow(deviation2, 2) + pow(deviation3, 2) + pow(deviation4, 2) + pow(deviation5, 2), number);
}
double calculate_standard_deviation (double number1, double number2, double number3, double number4, double number5, double mean, int number) {
   return sqrt((pow(number1 - mean, 2) + pow(number2 - mean, 2) + pow(number3 - mean, 2) + pow(number4 - mean, 2) + pow(number5 - mean, 2))/ number);
}
double find_max (double number1, double number2, double number3, double number4, double number5) {
   double first = (number1 > number2)? number1: number2;
   double second = (number3 > number4)? number3: number4;
   double third = (first > second)? first: second;
  
   return (third > number5)? third: number5;
}
  
double find_min (double number1, double number2, double number3, double number4, double number5) {
   double first = (number1 < number2)? number1: number2;
   double second = (number3 < number4)? number3: number4;
   double third = (first < second)? first: second;
  
   return (third < number5)? third: number5;
  
}
  
void print_double (FILE *outfile, double number) {
   fprintf(outfile, "%.2f", number);
}
  
int main()
{
FILE *inptr;
FILE *outptr;
inptr = fopen("C:\\Users\\ykgupta\\Documents\\df\\SD\\input.dat","r");
outptr = fopen("C:\\Users\\ykgupta\\Documents\\df\\SD\\output.dat","w");

double gpa[5], age[5];
int standing[5];

// in case if there is some issue in opening files
if((inptr == NULL) || (outptr == NULL))
{
printf("Error!");   
exit(1);   
}
  
// reading 5 records
int i=0;
for(; i< 5; i++) {
read_integer(inptr);
   gpa[i] = read_double(inptr);
   standing[i] = read_integer(inptr);
   age[i] = read_double(inptr);
}

// calulation sum of gpas, standings, ages
double gpaSum = calculate_sum(gpa[0], gpa[1], gpa[2], gpa[3], gpa[4]);
double standingSum = calculate_sum(standing[0], standing[1], standing[2], standing[3], standing[4]);
double ageSum = calculate_sum(age[0], age[1], age[2], age[3], age[4]);

// calulation mean of gpas, standings, ages
double gpaMean = calculate_mean(gpaSum, 5);
double standingMean = calculate_mean(standingSum, 5);
double ageMean = calculate_mean(ageSum, 5);

// Prinitng means to output file
fprintf(outptr,"%.2f\n", gpaMean);
fprintf(outptr,"%.2f\n", standingMean);
fprintf(outptr,"%.2f\n", ageMean);

// calculating deviations
for(int i=0; i<5; i++) {
   calculate_deviation(gpa[i], gpaMean);
}

// calculating variances
double variance = calculate_variance(gpa[0], gpa[1], gpa[2], gpa[3], gpa[4], 5);

// calculating standard deviations, max and min
double sd = calculate_standard_deviation(gpa[0], gpa[1], gpa[2], gpa[3], gpa[4], gpaMean, 5);   
double minGpa = find_min(gpa[0], gpa[1], gpa[2], gpa[3], gpa[4]);
double maxGpa = find_max(gpa[0], gpa[1], gpa[2], gpa[3], gpa[4]);

// printing to output file
fprintf(outptr,"%.2f\n", sd);
fprintf(outptr,"%.2f\n", minGpa);
fprintf(outptr,"%.2f\n", maxGpa);

// giving successful completion information
printf("Program completed.");

fclose(inptr);
fclose(outptr);

return 0;
}


/****** Please change the locations of your input and output files ************/

/**** Sample input: input.dat ********/

12345678
3.78
3
20.5
  
87654321
2.65
2
19.25

08651234
3.10
1
18.0


11112222
3.95
4
22.5

22223234
2.45
3
19.3333

/**** Sample Output: output.dat **********/

3.19
2.60
19.92
0.60
2.45
3.95


Related Solutions

Write a modularized, menu-driven program to read a file with unknown number of records. Input file...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file has unknown number of records of inventory items, but no more than 100; one record per line in the following order: item ID, item name (one word), quantity on hand , and a price All fields in the input file are separated by a tab (‘\t’) or a blank ( up to you) No error checking of the data required Create a menu which...
This program will read a bunch of numbers from an external file (numbers.txt) and calculate their...
This program will read a bunch of numbers from an external file (numbers.txt) and calculate their total and average. In addition, the total will be multiplied by an integer that is supplied by the user. ** IMPORTANT: The numbers are included in your starter code as a separate file (numbers.txt). Don't touch that file! All of your code should be placed in code.cpp, as usual. ** Input: After all numbers are read, what number would you like to multiply the...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT