In: Computer Science
The Programming Language is C++
PLEASE, Make sure to read the requirements and grading criteria for homework first... Thank you!!!
Objective:
The purpose of this project is to expose you to:
One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions.
Problem Specification:
Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student ID number. The number of students in the class is unknown, therefore we need to detect end of file to stop. The file pr2data.txt is included and contains the data.
The program calculates the statistics for each student and for each quiz as shown in the sample output. The output goes to a file and is in the same order as the input and should be similar to the following: any other improvements to the output are welcomed.
CIS Department – Fall 2018
CIS 161 Class Statistics
Student Quiz 1 Quiz 2 Quiz 3 Average
1234 78 83 87 82.67
2134 67 77 84 76.00
3124 77 89 93 86.33
High score 78 89 93
Low score 67 77 84
Quiz Average 73.4 83.0 88.2
pr2data
1234 52 70 75
2134 90 76 90
3124 90 95 98
4532 21 17 81
5678 20 22 45
6134 34 45 55
7874 60 99 56
8026 70 10 66
9893 34 09 77
2233 78 20 78
1947 45 40 88
3456 78 55 78
2877 55 50 95
3189 70 98 78
2132 77 97 80
4602 89 50 91
3445 78 60 78
5405 35 33 15
4556 78 20 18
6999 88 98 89
9898 48 78 68
2323 78 20 78
Requirements:
Grading Criteria:
5 points There are sufficient comments in the programs.
5 points use the typedef to define all arrays.
5 points a flowchart of the function main () is included and is correct (only main()).
5 points use a counter to count the number of elements read.
5 points generate an error if file does not exist, or if not open.
10 points loop is included and reads the data until it encounters the end of file.
5 points proper passing by value/by reference for all functions.
5 points the function findstavg () finds the float average for each student.
5 points the function findhigh () is clear correct and returns the highest quiz.
5 points the function findlow () is clear correct and returns the lowest quiz.
10 points the function findqzavg () is clear correct and returns a quiz average.
5 points every function has specifications.
5 points headings, column titles are printed, formatted and looks nice.
5 points data/calculated results are printed with proper spacing and proper formatting.
15 points the program runs correctly and produces the intended results.
5 points output are printed to a file.
Summary :
Provided Notes , Code , Flowchart and output.
Notes :
Utilized typedef for the main student data and same can be done for other arrays as well.
Since the size is not defined , while declaring the data variable , need to provide the actual definition or else one can declare a very large array and utilize the count to keep track .
Further introduced 2 functions getLines() - which returns the number of students and printSUmmary() which prints the data in desired output format.
################## COde ####################
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
typedef int scores[][4] ;
// Function to get the count of lines
int getLines()
{
// initilize num lines to 0
int numLines = 0 ;
// Open the file read line
string line ;
ifstream ifile( "pr2data.txt");
while ( getline(ifile,line) )
numLines += 1;
return numLines ;
}
void Setdata(scores data,int count )
{
int i = 0 ;
// open the file
ifstream ifile( "pr2data.txt");
if ( ifile )
{
// while not reach eof read
while( !ifile.eof() )
{
// this is required otherwise the last line is read twice
if ( ifile.eof()) break;
ifile >> data[i][0] >> data[i][1] >> data[i][2] >> data[i][3] ;
i++ ;
}
} else
{
cout << "\n Error opening pr2data.txt \n" ;
}
}
// The student avg is stored in avg array
void findstavg (scores data, double avg[] , int count )
{
// iterate over the student scores and calculate avg
for( int i=0; i< count ; i++ )
{
avg[i] = (double) ( data[i][1] + data[i][2] + data[i][3] )*1.0 / 3.0 ;
}
}
// Finds the avg of given Quicz ( row )
float Findqzavg(scores data, int row, int count )
{
double avg = 0.0;
// Iterate over the scores and calculate avg of only the quiz number
for(int i=0 ; i < count ; i++ )
{
avg += data[i][row];
}
return avg/count ;
}
// Finds the max of Quilz number
int Findhigh (scores data, int row, int count )
{
int max = 0 ;
// Iterate over the Quiz scores and find max
for ( int i=0 ; i < count ; i++ )
{
if ( data[i][row] > max )
max = data[i][row];
}
return max;
}
int Findlow (scores data, int row, int count )
{
int min = 10000 ;
// Iterate over the Quiz scores and find min
for ( int i=0 ; i < count ; i++ )
{
if ( data[i][row] < min )
min = data[i][row];
}
return min;
}
// This function prints the summary to the file
void printSummary(scores data, double avgs[] , int count )
{
// open the file
ofstream outfile;
outfile.open("pr2data_out.txt");
// Print heading
outfile << setw(9) << " Student"
<< setw(9) << "Quiz 1"
<< setw(9) << "Quiz 2"
<< setw(9) << "Quiz 3"
<< setw(12) << " Average\n";
// Print student scores with avg
for( int i=0 ; i < count ; i++ )
{
outfile << setw(9) << data[i][0]
<< setw(9) << data[i][1]
<< setw(9) << data[i][2]
<< setw(9) << data[i][3]
<< setw(12) << fixed << setprecision(2) << (double)avgs[i] * 1.0 << "\n";
}
// Print High , Low and Avg scores
outfile << " High score" << setw(7) << Findhigh(data,1,count)
<< setw(9) << Findhigh(data,2,count)
<< setw(9) << Findhigh(data,3,count) << "\n";
outfile << " Low score " << setw(7) << Findlow(data,1,count)
<< setw(9) << Findlow(data,2,count)
<< setw(9) << Findlow(data,3,count) << "\n";
outfile << " Quiz Average " << setw(7) << fixed << setprecision(2) << Findqzavg(data,1,count)
<< setw(9) << fixed << setprecision(2) << Findqzavg(data,2,count)
<< setw(9) << fixed << setprecision(2)<< Findqzavg(data,3,count) << "\n";
outfile.close();
}
int main()
{
int student_count = getLines() ;
//cout << " Student Count " << student_count << "\n";
// one can utilize type def here to delcare very large array at the begining and only use the count
int data[student_count][4] ;
double student_avgs[student_count];
Setdata(data,student_count);
findstavg(data,student_avgs,student_count);
printSummary(data,student_avgs,student_count);
}
Flowchart
Output :