Question

In: Computer Science

#include<iostream> #include<string> #include<fstream> #include<vector> /*HW: Create two more functions, Modify the posted grade example that uses...

#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;

}

Solutions

Expert Solution

// 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


Related Solutions

#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip>...
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip> #include<algorithm> using namespace std; const int row = 10; const int col = 7;   ifstream name; ifstream grade; ofstream out; void readData(string stname[row], int stgrade[row][col]); void stsum(int stgrade[row][col], int total[row]); void staverage(int total[row], double average[row]); void stlettergrade(double average[row],char ltrgrade[row]); void displayData(string stname[row], int stgrade[row][col], int total[row], double staverage[row], char ltrgrade[row]); void swapltrgrade(char* xp, char* yp); int main() {    int STG[row][col] = { {0},{0}...
Modify the program to double each number in the vector. #include <iostream> #include <vector> using namespace...
Modify the program to double each number in the vector. #include <iostream> #include <vector> using namespace std; int main() { const int N=8; vector<int> nums(N); // User numbers int i=0; // Loop index cout << "\nEnter " << N << " numbers...\n"; for (i = 0; i < N; ++i) { cout << i+1 << ": "; cin >> nums.at(i); } // Convert negatives to 0 for (i = 0; i < N; ++i) { if (nums.at(i) < 0) {...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void selectionSort(string [], int); void displayArray(string [], int); void readNames(string[], int); int main() {    const int NUM_NAMES = 20;    string names[NUM_NAMES];    // Read the names from the file.    readNames(names, NUM_NAMES);    // Display the unsorted array.    cout << "Here are the unsorted names:\n";    cout << "--------------------------\n";    displayArray(names, NUM_NAMES);    // Sort the array.    selectionSort(names, NUM_NAMES);    //...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3;...
modify this program to either simulate a teacher or used car salesperson. #include <iostream> #include <string>...
modify this program to either simulate a teacher or used car salesperson. #include <iostream> #include <string> using namespace std; int Menu(string a, string b, string c) { int choice =-1; cout<<" 1)"<<a<<endl; cout<<" 2)"<<b<<endl; cout<<" 3)"<<c<<endl; cout<<" 4) I don't want to talk to you!!"<<endl; cin>>choice; return choice; } int Money(int x) { int c=-1; cout<<"\n Are you that materialistic? \n"; c = Menu(" No.", " Yes.", " Can we talk about something else?"); if(c == 1|| c ==2 )...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h"...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h" #include"BST.cpp" using namespace std; std::vector<std::string> tokenize(char line[]) {    std::vector<std::string> tok;        std::string word = "";        for (int i = 0; i < strlen(line); i++)        {            if (i == strlen(line) - 1)            {                word += line[i];                tok.push_back(word);                return tok;       ...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y);...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y); int main(){ char s1[] = "zoey"; char s2[] = "zoey"; string s3 = s1; string s4 = s2; cout << "1. sizeof(s1) = " << sizeof(s1) << endl; if(s1 == s2) cout << "2. s1 == s2 is true" << endl; else cout << "2. s1 == s2 is false" << endl; if(s3 == s4) cout << "3. s3 == s4 is true" <<...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT