arrays - C++ - Read input file and store and calculate statistics -


i posted question on topic earlier when beginning problem. since then, have worked on source code , filled in lot of blanks. have experiencing troubles now, because there lot of discrepancies , problems source code.

please ignore comments. include them own personal benefit remind me of doing or going on when checking own work.

first, has 9 function prototypes categorized 3 groups 3 methods each same thing different variables. example, int getfirstlowest(studentdata[]) int getfirsthighest(studentdata[]), , double getaverage1(studentdata[], int). want different method find lowest, highest , average of each exam , lowest, highest , average value of totals of each. idea had create array of size 3 each statistic need calculate (lowest, highest, average , standard deviation), don't know how it.

next, curious if should combine gettotal function , getgrades function more efficient. gettotal method adds exam scores while getgrades filled if else statements categorize scores , determine grades.

in addition, need print function. output needs similar image attached below, doesn't. count each student turns out wrong , starts @ 0 , messes alignment why did not include it.

here image of how output should like: enter image description here

here image of current output: enter image description here

i printed lowest, highest, , average data @ top check if calculations correct each aspect still unsure how place data array print out more smoothly. in addition, please note of data in both images same , different, because first image sample previous semester. numbers correct in exam columns.


    #include "stdafx.h"     #include <iostream>      #include <string>      #include <fstream>     #include <iomanip>       using namespace std;       struct studentdata     {         int studentid;          string first_name;          string last_name;          int exam1;          int exam2;          int exam3;          int total;          char ch;      };       istream& operator >> (istream& in, studentdata& st)     {         return (in >> st.studentid                    >> st.first_name                    >> st.last_name                    >> st.exam1                    >> st.exam2                    >> st.exam3);     }      const int size = 9;       // function prototypes     void openinputfile(ifstream &, string);      int getfirstlowest(studentdata[]);      int getsecondlowest(studentdata[]);      int getthirdlowest(studentdata[]);      int getfirsthighest(studentdata[]);      int getsecondhighest(studentdata[]);      int getthirdhighest(studentdata[]);      double getaverage1(studentdata[], int);      double getaverage2(studentdata[], int);      double getaverage3(studentdata[], int);      void gettotal(studentdata arr[]);      void getgrade(studentdata arr[]);      void print(studentdata[]);       int main()     {         // variables         //standarddeviation;           studentdata arr[size];          int x, y, z, w; // stores lowest exam scores         int i, j, k, l; // holds highest exam scores         double average1, average2, average3, average4; // represents average of each exam       int lowest[3];      int highest[3];      double average[3];       ifstream infile;      string infilename = "c:\\users\\lisa\\desktop\\scores.txt";       // call function read data in file     openinputfile(infile, infilename);      // read data array of structs      size_t numitems = 0;     while (infile >> arr[numitems])     {         ++numitems;     }      // close input file     infile.close();        // lowest exam scores each exam     x = getfirstlowest(arr);      y = getsecondlowest(arr);      z = getthirdlowest(arr);       // highest exam scores each exam      = getfirsthighest(arr);      j = getsecondhighest(arr);      k = getthirdhighest(arr);       cout << "\nlowest exam scores (in order): " << x << " " << y << " " << z << endl;      cout << "\nhighest exam scores (in order): " << << " " << j << " " << k << endl;       cout << "\n";       // average score of each exam      average1 = getaverage1(arr, size);      average2 = getaverage2(arr, size);      average3 = getaverage3(arr, size);       cout << "exam 1 average: " << setprecision(2) << fixed << average1 << endl;      cout << "exam 2 average: " << setprecision(2) << fixed << average2 << endl;      cout << "exam 3 average: " << setprecision(2) << fixed << average3 << endl;       cout << "\n";       gettotal(arr);       getgrade(arr);       print(arr);       system("pause");       return 0;  }  /** * pre-condition:  * post-condition:  */ void openinputfile(ifstream &infile, string infilename) {     //open file     infile.open(infilename);      //input validation     if (!infile)     {         cout << "error open file." << endl;         cout << endl;         return;     } }  /** * pre-condition:  * post-condition:  */ int getfirstlowest(studentdata arr[]) {     int lowest = 0;      int num = arr[0].exam1;       (int = 0; i<size; i++)     {         if (num > arr[i].exam1)         {             num = arr[i].exam1;              lowest = i;          }     }     return num; }  /** * pre-condition:  * post-condition:  */ int getsecondlowest(studentdata arr[]) {     int lowest = 0;      int num = arr[0].exam2;       (int = 0; i<size; i++)     {         if (num > arr[i].exam2)         {             num = arr[i].exam2;              lowest = i;          }     }     return num; }  /** * pre-condition:  * post-condition:  */ int getthirdlowest(studentdata arr[]) {     int lowest = 0;      int num = arr[0].exam3;       (int = 0; i<size; i++)     {         if (num > arr[i].exam3)         {             num = arr[i].exam3;              lowest = i;          }     }     return num; }  /** * pre-condition:  * post-condition:  */ int getfirsthighest(studentdata arr[]) {     int highest = 0;      int num = arr[0].exam1;       (int = 0; i<size; i++)     {         if (num < arr[i].exam1)         {             num = arr[i].exam1;              highest = i;          }     }     return num; }  /** * pre-condition:  * post-condition:  */ int getsecondhighest(studentdata arr[]) {     int highest = 0;      int num = arr[0].exam2;       (int = 0; i<size; i++)     {         if (num < arr[i].exam2)         {             num = arr[i].exam2;              highest = i;          }     }     return num; }  /** * pre-condition:  * post-condition:  */ int getthirdhighest(studentdata arr[]) {     int highest = 0;      int num = arr[0].exam3;       (int = 0; i<size; i++)     {         if (num < arr[i].exam3)         {             num = arr[i].exam3;              highest = i;          }     }     return num; }  /** * pre-condition:  * post-condition:  */ double getaverage1(studentdata arr[], int size) {     int sum = 0;      double average = 0;       for(int = 0; < size; i++)     {         sum += arr[i].exam1;      }      average += static_cast<double>(sum)/size;       return average;  }  /** * pre-condition:  * post-condition:  */ double getaverage2(studentdata arr[], int size) {     int sum = 0;      double average = 0;       for(int = 0; < size; i++)     {         sum += arr[i].exam2;      }      average += static_cast<double>(sum)/size;       return average;  }  /** * pre-condition:  * post-condition:  */ double getaverage3(studentdata arr[], int size) {     int sum = 0;      double average = 0;       for(int = 0; < size; i++)     {         sum += arr[i].exam3;      }      average += static_cast<double>(sum)/size;       return average;  }  /** * pre-condition:  * post-condition:  */ void gettotal(studentdata arr[]) {     for(int = 0; < size; i++)     {         arr[i].total = arr[i].exam1 + arr[i].exam2 + arr[i].exam3;      } }  /** * pre-condition:  * post-condition:  */ void getgrade(studentdata arr[]) {     for(int = 0; < size; i++)     {         if(arr[i].total >= 270)             arr[i].ch = 'a';          else if(arr[i].total >= 240)             arr[i].ch = 'b';          else if(arr[i].total >= 210)             arr[i].ch = 'c';          else if(arr[i].total >= 180)             arr[i].ch = 'd';          else              arr[i].ch = 'f';      } }  /** * pre-condition:  * post-condition:  */ void print(studentdata arr[]) {     cout << "id first name  last name   exam1   exam2   exam3   total   grade" << endl;      for(int = 0; < size; i++)     {         cout << left << arr[i].studentid << "\t" << setw(10) <<  arr[i].first_name << "\t" << setw(5) << arr[i].last_name << "\t\t"               << arr[i].exam1 << "\t" << arr[i].exam2 << "\t" << arr[i].exam3 << "\t" << arr[i].total << "\t" << arr[i].ch << endl;      } } 

first, has 9 function prototypes categorized 3 groups 3 methods each same thing different variables.

this not extensible (i.e., because of work involved, meaning code changes, when new exam scores and/or students add). perhaps you're aware of since made following statement.

i want different method find lowest, highest , average of each exam , lowest, highest , average value of totals of each.


since learning assignment can show output, main, , skeleton implementation. if looks trying can finish implementing skeleton code. note, didn't try formatting exact since should easy enough you.

my output

1234            david           dalton          82              86              80              248 9138            shirley         gross           90              98              94              282 3124            cynthia         morley          87              84              82              253 4532            albert          roberts         56              89              78              223 5678            amelia          pauls           90              87              65              242 6134            samson          smith           29              65              33              127 7874            michael         garett          91              92              92              275 8026            melissa         downey          74              75              89              238 9893            gabe            yu              69              66              68              203                                  lowest:         29              65              33              127                                 higest:         91              98              94              283                                 avg:            74.2222         82.4444         75.6667         232.333 

my main

the implementation pretty straight-forward/simple.

int main() {     students students;     exams exams;      // read data file , populate student , exam data structures     if (!initialize(students, exams))     {         return -1;     }      // compute exam statistics     exams.computestatistics();      // print student information     (const auto& student : students)     {         std::cout << student << "\n";     }      // print exam information     std::cout << "\n";     std::cout << exams << "\n";      return 0; } 

skeleton code

here skeleton of code used may helpful you.

// class represent single exam class exam { public:     // computes min, max, , avg (later can extended compute     // additional statistics)     void computestatistics()     {         // todo     }      int min() const { return mmin; }     int max() const { return mmax; }     double avg() const { return mavg; }      // adds score earned student (i.e., keeps track of scores     // used 'computestatistics' function)     void addscore(int score)     {         // todo     }  private:     std::vector<int> mscores; // scores exam      // values computed available scores     int mmin;     int mmax;     double mavg; };  // class represent exams (this helpful since there // statistics need computed exams) class exams { public:     // exam associated specified exam numer     exam& operator[](std::size_t examnumber)     {         return mexams[examnumber];     }      // compute min total, max total, average average, store exam min     // scores, store exam max scores, store exam avg scores     void computestatistics()     {         // todo     }      // prints exam information (e.g., lowest, highest, average)     friend std::ostream& operator<<(std::ostream& stream, const exams& exams)     {         // todo     }  private:     // mapping 'exam number' exam data type     std::map<int, exam> mexams;      // stores min, max, , avg exam scores (for printing later)     std::vector<int> mminscores;     std::vector<int> mmaxscores;     std::vector<double> mavgscores;      // values computed available exams     int mmintotal;     int mmaxtotal;     double mavgavg; };  // class represent student class student { public:     student() :         mtotalscore(0)     {         // nothing     }      // records student's score on particular exam (this used     // keep running total of exam scores student,     // can printed later)     void addscore(int examnumber, int score)     {         // todo     }      // each exam score student added 'exams'     void updateexams(exams& exams) const     {         // todo     }      int mid;     std::string mfirstname;     std::string mlastname;      // mapping 'exam number' exam score     std::map<int, int> mexams;     int mtotalscore; // total of exam scores student };  // reads student stream (hint: uses 'addscore' function) std::istream& operator>>(std::istream& stream, student& student) {     // todo }  // prints out student (i.e., id, fist, last, exam1, exam2, exam3, examtotal) std::ostream& operator<<(std::ostream& stream, const student& student) {     // todo }  // alias don't have type 'std::vector<student>' on using students = std::vector<student>;  // reads data text file , adds each student 'students' // , each exam 'exams' (hint: after each student read can // used update exams) bool initialize(students& students, exams& exams) {     // todo } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -