Question

I want to change this code and need help. I want the code to not use...

I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great.

#include
#include
#include
#include

using namespace std;

const int NUMBER_OF_ROWS = 10; //number of students
const int NUMBER_OF_COLUMNS = 5; //number of scores


void readData(ifstream&, string[], int, int[][NUMBER_OF_COLUMNS]);
void calculateAverages(const int[][NUMBER_OF_COLUMNS], int, char[], double&);
void printResults(string names[NUMBER_OF_ROWS], int scores[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS],
   char grade[NUMBER_OF_ROWS], double avgScore);

int main()
{

   ifstream infile;
   infile.open("Data.txt");
  
   string names[NUMBER_OF_ROWS]; //students names
   char grades[NUMBER_OF_ROWS]; //grades
   int scores[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]; //test scores

   double avgScore; //overall class Average

   if (!infile) {
       cout << "Cannot open input file. Program terminated!" << endl;
       return 1; //end program with error
   }
  
   readData(infile, names, NUMBER_OF_ROWS, scores);
  
   calculateAverages(scores, NUMBER_OF_ROWS, grades, avgScore);

   printResults(names, scores, grades, avgScore);
   cout << endl;

   infile.close();//close input file


   return 0;
}

void readData(ifstream& infile, string names[], int length, int scores[][NUMBER_OF_COLUMNS]) {
   for (int index = 0; index < length; index++) {
       infile >> names[index]; //name stored in 1-dim string array
  
       for (int ts = 0; ts < NUMBER_OF_COLUMNS; ts++) {
           infile >> scores[index][ts];
       }

   }
}


void calculateAverages(const int scores[][NUMBER_OF_COLUMNS], int length, char grades[], double& avgScore) {
   int totalStudentScore = 0;
   int totalClassScore = 0;

   for (int index = 0; index < length; index++) {
       totalStudentScore = 0;
       for (int s = 0; s < NUMBER_OF_COLUMNS; s++) {
           totalStudentScore += scores[index][s];
       }

       //grade ranges: A(90-100), B(80-89), C(70-79), D(60-69), F(<60)
       int studentAvg = (totalStudentScore / NUMBER_OF_COLUMNS);
       if (studentAvg >= 90) {
           grades[index] = 'A';
       }
       else if (studentAvg >= 80 && studentAvg < 90) {
           grades[index] = 'B';
       }
       else if (studentAvg >= 70 && studentAvg < 80) {
           grades[index] = 'C';
       }
       else if (studentAvg >= 60 && studentAvg < 70) {
           grades[index] = 'D';
       }
       else {
           grades[index] = 'F';
       }
       totalClassScore += totalStudentScore; //add to total class score
   }


   avgScore = static_cast (totalClassScore) / (length * NUMBER_OF_COLUMNS); //avg score range 0-100

}

void printResults(string names[NUMBER_OF_ROWS], int scores[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS],
   char grade[NUMBER_OF_ROWS], double avgScore)
{

   cout << fixed << showpoint << setprecision(2);
   cout << setw(10) << left << "Name" << setw(9);
   cout.setf(ios::right);
   cout << "Test1" << setw(8) << "Test2" << setw(9) << "Test 3" << setw(8) << "Test 4" << setw(7)
       << "Test5" << setw(19) << "Average Score" << setw(8) << "Grade" << endl;

cout << "-------------------------------------------------------------------------------" << endl;

   for (int i = 0; i < NUMBER_OF_COLUMNS; i++)
   {
       cout.unsetf(ios::right);
       cout.setf(ios::left);
       cout << setw(7) << names[i] << setw(9);
       cout.unsetf(ios::left);
       cout.setf(ios::right);
       cout << fixed << showpoint << setprecision(2);
       for (int index = 0; index < NUMBER_OF_COLUMNS + 1; index++)
       {
           if (index == NUMBER_OF_COLUMNS)
           {
               cout << setw(11) << scores[i][index];
               cout << setw(15) << grade[i] << endl;
           }
           else
           {
               cout << scores[i][index] << setw(8);
           }
       }
   }
   cout << endl << "The Average for the Class is: " << avgScore << endl;

}

Here is Data.txt

Jack Ray 85 83 77 91 76
Nate Johnson 80 90 95 93 48
Sam Tylor 78 81 11 90 73
Zach Bell 92 83 30 69 87
Billy Bob 23 45 96 38 59
Alex Great 60 85 45 39 67
Toby Keith 77 31 52 74 83
Mark Alan 93 94 89 77 97
Faith Peep 79 85 28 93 82
Randy Willison 85 72 49 75 63

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below are the screenshots of modified code and output:

I modified your code so as to use a struct array of student instead of using multiple arrays to store names,scores,grades and averages. The student struct has members firstname,lastname,grade,scores & avgScore. Below is the modified code:

 #include<bits/stdc++.h> using namespace std; const int NUMBER_OF_STUDENTS = 10; //number of students const int NUMBER_OF_SCORES = 5; //number of scores //creating struct student struct student{ string firstname;//students first names string lastname;//students last names char grade; //grades int scores[NUMBER_OF_SCORES]; //test scores double avgScore;//avg score }; //modified prototype declaration void readData(ifstream&, student[], int); void calculateAverages(student[], int,double& ); void printResults(student[], double avgScore); int main() { ifstream infile; infile.open("Data.txt"); //struct array declaration student students[NUMBER_OF_STUDENTS]; double avgScore; //overall class Average if (!infile) { cout << "Cannot open input file. Program terminated!" << endl; return 1; //end program with error } //modified function calls readData(infile, students, NUMBER_OF_STUDENTS); calculateAverages(students, NUMBER_OF_STUDENTS,avgScore); printResults(students, avgScore); cout << endl; infile.close();//close input file return 0; } //modified function definition void readData(ifstream& infile, student students[], int length) { for (int index = 0; index < length; index++) { infile >> students[index].firstname >> students[index].lastname; //name stored in student array's firstname and lastname attribute for (int ts = 0; ts < NUMBER_OF_SCORES; ts++) { infile >> students[index].scores[ts];//scores stored in student array's scores[] attribute } } } //modified function definition void calculateAverages(student students[], int length ,double& avgScore) { int totalStudentScore = 0; int totalClassScore = 0; for (int index = 0; index < length; index++) { totalStudentScore = 0; for (int s = 0; s < NUMBER_OF_SCORES; s++) { totalStudentScore += students[index].scores[s]; } //grade ranges: A(90-100), B(80-89), C(70-79), D(60-69), F(<60) students[index].avgScore = (totalStudentScore / NUMBER_OF_SCORES); if (students[index].avgScore >= 90) { students[index].grade = 'A'; } else if (students[index].avgScore >= 80 && students[index].avgScore < 90) { students[index].grade = 'B'; } else if (students[index].avgScore >= 70 && students[index].avgScore < 80) { students[index].grade = 'C'; } else if (students[index].avgScore >= 60 && students[index].avgScore < 70) { students[index].grade = 'D'; } else { students[index].grade = 'F'; } totalClassScore += totalStudentScore; //add to total class score } avgScore = (totalClassScore)/(length * NUMBER_OF_SCORES); //avg score range 0-100 } //modified function definition void printResults(student students[], double avgScore) { cout << fixed << showpoint << setprecision(2); cout << setw(10) << left << "Name" << setw(9); cout.setf(ios::right); cout << "Test1" << setw(8) << "Test2" << setw(9) << "Test 3" << setw(8) << "Test 4" << setw(7) << "Test5" << setw(19) << "Average Score" << setw(8) << "Grade" << endl; cout << "-------------------------------------------------------------------------------" << endl; for (int i = 0; i < NUMBER_OF_STUDENTS; i++) { cout.unsetf(ios::right); cout.setf(ios::left); cout << setw(16) << students[i].firstname+" "+students[i].lastname; cout.unsetf(ios::left); cout.setf(ios::right); cout << fixed << showpoint << setprecision(2); for (int index = 0; index < NUMBER_OF_SCORES + 1; index++) { if (index == NUMBER_OF_SCORES) { cout << setw(15) << students[i].avgScore; cout << setw(11) << students[i].grade << endl; } else { cout << students[i].scores[index] << setw(8); } } } cout << endl << "The Average for the Class is: " << avgScore << endl; }
Add a comment
Know the answer?
Add Answer to:
I want to change this code and need help. I want the code to not use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • Please help!! I am supposed to write a program in C++ about student & grades. Needs...

    Please help!! I am supposed to write a program in C++ about student & grades. Needs to have two functions that sorts students letter grade, and another one to sort Students name in your student’s record project. Remember, to add necessary parameters and declaration in main to call each function properly. Order of functions call are as follow Read Data Find Total Find average Find letter grade Call display function Call sort letter grade function Call display function Call sort...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • hello there. can you please help me to complete this code. thank you. Lab #3 -...

    hello there. can you please help me to complete this code. thank you. Lab #3 - Classes/Constructors Part I - Fill in the missing parts of this code #include<iostream> #include<string> #include<fstream> using namespace std; class classGrades      {      public:            void printlist() const;            void inputGrades(ifstream &);            double returnAvg() const;            void setName(string);            void setNumStudents(int);            classGrades();            classGrades(int);      private:            int gradeList[30];            int numStudents;            string name;      }; int main() {          ...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

  • Today assignment , find the errors in this code and fixed them. Please I need help...

    Today assignment , find the errors in this code and fixed them. Please I need help with find the errors and how ro fixed them. #include <iostream> #include <cstring> #include <iomanip> using namespace std; const int MAX_CHAR = 100; const int MIN_A = 90; const int MIN_B = 80; const int MIN_C = 70; double getAvg(); char determineGrade(double); void printMsg(char grade); int main() { double score; char grade; char msg[MAX_CHAR]; strcpy(msg, "Excellent!"); strcpy(msg, "Good job!"); strcpy(msg, "You've passed!"); strcpy(msg, "Need...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT