In: Computer Science
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
/*HW: Create two more functions,
Modify the posted grade example that uses dynamic arrays and file I/O to include the following:
-Use vectors
-read from values from the file and store them in the vector.
Modify the function getGrades
Extend the program to also include the following features:
-A menu option that allows the user to add more students and
there grades. Assume the number of quizzes stay the same based on
the value read from the file
-A menu option that allows the user to display all students that
were added and their grades. As well as, the average of the quizzes
and the average of the student
-A menu option to search for a student by ID, the program must
display the student ID, and the student grades
-An option to exit.
Make sure the new students added to the vector are stored in the original file that you read from. Hence, if you exit the program and run it again, the new students added will show in your new run of the program.*/
using namespace std;
//Don't forget to pass sid and grade by reference
void getGrades(ifstream &fin, vector<string> &sid, vector<vector<double>> &grade, int num_stud, int NUM_Q)
{
for (int i = 0; i<num_stud; i++)
{
string tempId;
double sgradetemp;
fin >> tempId;
sid.push_back(tempId);
vector<double>sgradetempv;
fin >> sid[i];
for (int j = 0; j < NUM_Q; j++)
{
fin >> sgradetemp;
//Vector of values
sgradetempv.push_back (sgradetemp);
}
//Vector of a vector
grade.push_back(sgradetempv);
}
}
void compute_st_ave(vector<vector<double>> grade, vector<double> &st_ave, int num_stud, int NUM_Q)
{
for (int s = 0; s<num_stud; s++)
{
double sum = 0;
for (int q = 0; q<NUM_Q; q++)
{
sum = sum + grade[s][q];
}
st_ave.push_back (s = sum / NUM_Q);
}
}
void compute_q_ave(vector<vector<double>>grade, vector<double>quiz_ave, int num_stud, int NUM_Q)
{
for (int q = 0; q<NUM_Q; q++)
{
double sum = 0;
for (int s = 0; s<num_stud; s++)
{
sum = sum + grade[s][q];
}
quiz_ave.push_back(q = sum / num_stud);
}
}
void saveToFile(vector<string> sid, vector<vector<double>>grade, vector<double>st_ave, vector<double>quiz_ave, int num_stud, int NUM_Q)
{
ofstream fout("results.txt");
fout << "Dispalying the results" << endl;
fout << "\t\t\t\tAverage" << "\t\t" << "Quizzes\n";
for (int s = 0; s<num_stud; s++)
{
fout << "Student " << sid[s] << ":\t" << st_ave[s] << " \t\t";
for (int q = 0; q<NUM_Q; q++)
{
fout << grade[s][q] << "\t";
}
fout << endl;
}
fout << "Quiz Average: \t\t\t";
for (int q = 0; q<NUM_Q; q++)
{
fout << "\t" << quiz_ave[q] << " ";
}
fout << endl;
fout.flush();
fout.close();
}
int main()
{
int NUM_STUD, NUM_Q;
ifstream fin("grades.txt");
fin >> NUM_STUD >> NUM_Q;
/*
string *sid = new string[NUM_STUD];
double **grade = new double *[NUM_STUD];
for (int i = 0; i < NUM_STUD; i++)
{
grade[i] = new double[NUM_Q];
}
double *st_ave = new double[NUM_STUD];
double *quiz_ave = new double[NUM_Q];
*/
vector<string>sid;
vector<vector<double>>grade;
vector<double> st_ave;
vector<double>quize_ave;
getGrades(fin, sid, grade, NUM_STUD, NUM_Q);
compute_st_ave(grade, st_ave, NUM_STUD, NUM_Q);
compute_q_ave(grade, quiz_ave, NUM_STUD, NUM_Q);
saveToFile(sid, grade, st_ave, quiz_ave, NUM_STUD, NUM_Q);
fin.close();
system("pause");
return 0;
}
// Assuming the input file is of format:
/*
<NUM_STUD> <NUM_QUIZ>
<SID1> <QUIZ1> <QUIZ2> <QUIZ3>.... <QUIZQN>
<SID2> <QUIZ1> <QUIZ2> <QUIZ3>.... <QUIZQN>
.....
<SIDSN> <QUIZ1> <QUIZ2> <QUIZ3>.... <QUIZQN>
*/
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
/*HW: Create two more functions,
Modify the posted grade example that uses dynamic arrays and file I/O to include the following:
-Use vectors
-read from values from the file and store them in the vector. Modify the function getGrades
Extend the program to also include the following features:
-A menu option that allows the user to add more students and there grades. Assume the number of quizzes stay the same based on the value read from the file
-A menu option that allows the user to display all students that were added and their grades. As well as, the average of the quizzes and the average of the student
-A menu option to search for a student by ID, the program must display the student ID, and the student grades
-An option to exit.
Make sure the new students added to the vector are stored in the original file that you read from. Hence, if you exit the program and run it again, the new students added will show in your new run of the program.*/
//Don't forget to pass sid and grade by reference
void getGrades(ifstream &fin, vector<string> &sid, vector< vector<double> > &grade, int num_stud, int NUM_Q)
{
for (int i = 0; i<num_stud; i++)
{
string tempId;
double sgradetemp;
fin >> tempId;
sid.push_back(tempId);
vector<double>sgradetempv;
//fin >> sid[i]; // this is not required as id has been read and inserted in sid in steps: fin>>tempId; sid.push_back(tempId);
for (int j = 0; j < NUM_Q; j++)
{
fin >> sgradetemp;
//Vector of values
sgradetempv.push_back (sgradetemp);
}
//Vector of a vector
grade.push_back(sgradetempv);
}
}
void compute_st_ave(vector< vector<double> > grade, vector<double> &st_ave, int num_stud, int NUM_Q)
{
for (int s = 0; s<num_stud; s++)
{
double sum = 0;
for (int q = 0; q<NUM_Q; q++)
{
sum = sum + grade[s][q];
}
st_ave.push_back (s = sum / NUM_Q);
}
}
void compute_q_ave(vector< vector<double> >grade, vector<double> &quiz_ave, int num_stud, int NUM_Q)
{
for (int q = 0; q<NUM_Q; q++)
{
double sum = 0;
for (int s = 0; s<num_stud; s++)
{
sum = sum + grade[s][q];
}
quiz_ave.push_back(q = sum / num_stud);
}
}
void saveToFile(vector<string> sid, vector< vector<double> >grade, vector<double>st_ave, vector<double>quiz_ave, int num_stud, int NUM_Q)
{
ofstream fout("results.txt");
fout << "Dispalying the results" << endl;
fout << "\t\t\t\tAverage" << "\t\t" << "Quizzes\n";
for (int s = 0; s<num_stud; s++)
{
fout << "Student " << sid[s] << ":\t" << st_ave[s] << " \t\t";
for (int q = 0; q<NUM_Q; q++)
{
fout << grade[s][q] << "\t";
}
fout << endl;
}
fout << "Quiz Average: \t\t\t";
for (int q = 0; q<NUM_Q; q++)
{
fout << "\t" << quiz_ave[q] << " ";
}
fout << endl;
fout.flush();
fout.close();
}
// function to add a student
void addStudent(vector<string> &sid, vector< vector<double> > &grade, vector<double> &st_ave, int NUM_Q, int &NUM_STUD)
{
string tempId;
double sgradetemp;
vector<double>sgradetempv;
// input of student id
cout<<"Enter Student id : ";
cin>>tempId;
sid.push_back(tempId); // insert the id into sid
double sum = 0;
// loop to input NUM_Q grades for the student and calculate total of grades
for(int i=0;i<NUM_Q;i++)
{
// input of grade
cout<<"Enter grade for Quiz-"<<(i+1)<<" : ";
cin>>sgradetemp;
sum += sgradetemp;
//Vector of values
// add the grade to the vector of grades
sgradetempv.push_back (sgradetemp);
}
//Vector of a vector
// add the vector of grades to grade vector
grade.push_back(sgradetempv);
st_ave.push_back(sum / NUM_Q); // add the student average in the st_ave vector
NUM_STUD++;// increment the number of students
}
void display(vector<string> sid, vector< vector<double> >grade, vector<double>st_ave, vector<double>quiz_ave, int num_stud, int NUM_Q)
{
cout << "Displaying the results" << endl;
cout << "\t\t\t\tAverage" << "\t\t" << "Quizzes\n";
for (int s = 0; s<num_stud; s++)
{
cout << "Student " << sid[s] << ":\t" << st_ave[s] << " \t\t";
for (int q = 0; q<NUM_Q; q++)
{
cout << grade[s][q] << "\t";
}
cout << endl;
}
cout << "Quiz Average: \t\t\t";
for (int q = 0; q<NUM_Q; q++)
{
cout << "\t" << quiz_ave[q] << " ";
}
cout<<endl;
}
void updateFile(vector<string> sid, vector< vector<double> > grade, int num_stud, int NUM_Q)
{
ofstream fout("grades.txt"); // open the file grades.txt for writing
// write the number of student and number of quizzes in the first line
fout<<num_stud<<" "<<NUM_Q<<endl;
// loop to write student details
for(int i=0;i<num_stud;i++)
{
fout<<sid[i]; // write the student id
// loop over the quizzes, writing each quiz grade to file separated by a space
for (int j = 0; j < NUM_Q; j++)
{
fout<<" "<<grade[i][j];
}
// if it is not the last record, insert a new line
if(i < (num_stud-1))
fout<<endl;
}
fout.close(); //close th file
}
void searchStudent(vector<string> sid, vector< vector<double> > grade, int num_stud, int NUM_Q, string targetId)
{
bool found = false;
for(int i=0;i<num_stud;i++)
{
if(sid[i] == targetId)
{
cout<<"Student ID : "<<sid[i]<<" Grades : ";
for(int j=0;j<NUM_Q;j++)
{
cout<<grade[i][j]<<" ";
}
cout<<endl;
found = true;
break;
}
}
if(!found)
cout<<"Student id : "<<targetId<<" doesn't exist"<<endl;
}
int main() {
int NUM_STUD, NUM_Q, INITIAL_NUM_STUD;
ifstream fin("grades.txt");
fin >> NUM_STUD >> NUM_Q;
INITIAL_NUM_STUD = NUM_STUD;
/*
string *sid = new string[NUM_STUD];
double **grade = new double *[NUM_STUD];
for (int i = 0; i < NUM_STUD; i++)
{
grade[i] = new double[NUM_Q];
}
double *st_ave = new double[NUM_STUD];
double *quiz_ave = new double[NUM_Q];
*/
vector<string>sid;
vector< vector<double> >grade;
vector<double> st_ave;
vector<double> quiz_ave;
getGrades(fin, sid, grade, NUM_STUD, NUM_Q);
compute_st_ave(grade, st_ave, NUM_STUD, NUM_Q);
compute_q_ave(grade, quiz_ave, NUM_STUD, NUM_Q);
fin.close();
int choice;
string targetId;
// loop that continues till the user exits
do
{
cout<<"1. Add a student"<<endl;
cout<<"2. Display students"<<endl;
cout<<"3. Search a student"<<endl;
cout<<"4. Exit"<<endl;
cout<<"Enter a choice(1-4) : ";
cin>>choice;
switch(choice)
{
case 1:
addStudent(sid,grade, st_ave, NUM_Q, NUM_STUD);
quiz_ave.clear();// remove the entries from quiz average
// recompute the quiz average
compute_q_ave(grade, quiz_ave, NUM_STUD, NUM_Q);
break;
case 2:
display(sid, grade, st_ave, quiz_ave, NUM_STUD, NUM_Q);
break;
case 3:
cout<<"Enter the Student id to search : ";
cin>>targetId;
searchStudent(sid,grade,NUM_STUD, NUM_Q,targetId);
break;
case 4:
// quit the application, save the details to results.txt and update the input file to include new records
saveToFile(sid, grade, st_ave, quiz_ave, NUM_STUD, NUM_Q);
// if students has been added, update the input file to include the new records
if( NUM_STUD != INITIAL_NUM_STUD)
updateFile(sid, grade, NUM_STUD, NUM_Q);
break;
default:
cout<<"Invalid choice"<<endl;
}
}while(choice != 4);
system("pause");
return 0;
}
//end of program