In: Computer Science
The Programming Language is C++
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.
Solution:
Givendata:
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.
Answer:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
//function ti calculate hte student Average
float findstavg(int q1,int q2,int q3)
{
return (q1+q2+q3)/3.0;
}
//function to find the high score
int findhigh(int q[],int n)
{
int max =q[0];
for(int i=1;i<n;i++)
{
if(q[i]>max) //condition for max
{
max=q[i];
}
}
return max;
}
//function to find the low score
int findlow(int q[],int n)
{
int min =q[0];
for(int i=1;i<n;i++)
{
if(q[i]<min)//condition for min
{
min=q[i];
}
}
return min;
}
//function to find the quiz average
float findqzavg(int q[],int n)
{
int s=0;
for(int i=0;i<n;i++)
{
s+=q[i]; //calculating the sum
}
double avg = s/(n*3.0); //calculating the avg
return avg;
}
int main()
{
string line;
ifstream myfile("pr2data.txt"); //opening the input file
ifstream myfile1("pr2data.txt");
//program to find the number of lines are present in the file for
declaring the array sozes
int linecount=0;
//condition to check whether able to open the file or not
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
linecount++;
}
myfile.close();
}
else {cout << "Error, the file does not exist\n"; }
//code to perform the required actions
if (myfile1.is_open())
{
ofstream outfile;
outfile.open("output.txt"); //output file to write the
contents
outfile<<"Student Quiz1 Quiz2 Quiz3 Average\n";
//arrays to store the values
int student[22];
int quiz1[22];
int quiz2[22];
int quiz3[22];
int j=0;
while ( getline (myfile1,line) )
{
outfile<<line;//writintg the contents to the output
file
int s[4];
string delimiter = " "; //splitting the line with spaces
int i=0;
size_t pos = 0;
string token;
while ((pos = line.find(delimiter)) != string::npos)
{
token = line.substr(0, pos);
stringstream geek(token); //converting the string to int
int x = 0;
geek >> x;
s[i]=x;
i++;
line.erase(0, pos + delimiter.length());
}
//converting the string to int
token = line;
stringstream geek(token);
int x = 0;
geek >> x;
s[i]=x;
//assigning the values to arrays
student[j]=s[0];
quiz1[j]=s[1];
quiz2[j]=s[2];
quiz3[j]=s[3];
//calling the student average function
float std_avg = findstavg(quiz1[j],quiz2[j],quiz3[j]);
outfile<<" "<<std_avg<<"\n"; //writintg the
student average details to the output file
j++;
}
int high_score_q1 = findhigh(quiz1,22);//function to find the high
score values
int high_score_q2 = findhigh(quiz2,22);//function to find the high
score values
int high_score_q3 = findhigh(quiz3,22);//function to find the high
score values
//writintg the high score values of each quiz to the output
file
outfile<<"High score "<<high_score_q1<<"
"<<high_score_q2<<" "<<high_score_q3<<"
\n";
int low_score_q1 = findlow(quiz1,22); //function to find the low
score values
int low_score_q2 = findlow(quiz2,22); //function to find the low
score values //function to find the low score values
int low_score_q3 = findlow(quiz3,22);
//writintg the low score values of each quiz to the output
file
outfile<<"Low score "<<low_score_q1<<"
"<<low_score_q2<<" "<<low_score_q3<<"
\n";
float avg_score_q1 = findqzavg(quiz1,22);//function to find the
quiz average values
float avg_score_q2 = findqzavg(quiz2,22);//function to find the
quiz average values
float avg_score_q3 = findqzavg(quiz3,22);//function to find the
quiz average values
//writintg the average quiz values of each quiz to the output
file
outfile<<"Quiz Average "<<avg_score_q1<<"
"<<avg_score_q2<<" "<<avg_score_q3<<"
\n";
myfile.close();
}
return 0;
}
Inputfile screenshot :
output screenshot :
Testcase1 :
Testcase 2:
I gave the file name as pr2dat.txt instead of pr2data.txt so we should get an error as the input file is not exist, attaching the screenshot for the same.
please giveme thumbup....................