Question

C++ Redo PROG8, a previous program using functions and/or arrays. Complete as many levels as you...

C++
Redo PROG8, a previous program using functions and/or arrays. Complete as many levels as you can. 
 Level 1: (20 points) Write FUNCTIONS for each of the following: a) Validate #students, #scores. b) Compute letter grade based on average. c) Display student letter grade. d) Display course average. 
 Level 2: (15 points) Use ARRAYS for each of the following. e) Read a student's scores into array. f) Calculate the student's average based on scores in array. g) Display the student's scores in the roll book. 
 Level 3: [15 points] write FUNCTIONS with ARRAY parameters for each of the following: f) Use the array to calculate the student's average. g) Use the array when you display the student's scores in the roll book

<iostream>

#include <fstream>

#include <cstring>

#include <cmath>

using namespace std;

  

int main()

{

int numStudents = 0; // # students in class.

int numScores = 0; // # scores per student.

int ID = 0; // Student id (4-digits).

int score = 0; // Student score.

int s = 0; // Loop control variable (#students).

int sumScores = 0; // Sum of scores for given student.

float avg = 0; // Average score for given student.

float savg = 0;

ifstream scoreF; // Input file stream - student scores.

ofstream avgF; // Output file stream -- averages

ofstream gradesF; // [revOutput stream -- formatted grade book.

  

//-| -------------------------------------------------------------------------------

//-| Display intellectual property copyright notice.

//-| -------------------------------------------------------------------------------

cout << endl << "(c) 2019 NAME" << endl << endl;

  

  

//-| -------------------------------------------------------------------------------

//-| Open input/output file streams. If input file can not be

//-| opened, display message and terminate.

//-| -------------------------------------------------------------------------------

scoreF.open("scores.dat");

if (scoreF.fail())

{

cout << "FILE 'scores.dat' CANNOT BE OPENED." << endl << endl;

exit(1);

}   

avgF.open("averages.dat");

gradesF.open("grades.rpt");

  

//-| --------------------------------------------------------------------------------

//-| Do what comes next. Don't forget to describe your algorithm

//-| as program comments.

//-| --------------------------------------------------------------------------------

  

  

//-| --------------------------------------------------------------------------------

//-| Read control data from scores file: #students and #scores.

//-| --------------------------------------------------------------------------------

scoreF >> numStudents >> numScores;

avgF << "STUDENT AVERAGES" << endl << endl;

gradesF << "COURSE GRADES REPORT" << endl << endl;

  

if(!(numStudents >=1 && numScores >=1))

{

cout << "INVALID #STUDENTS (" << numStudents << ") or #SCORES (" << numScores

<< ")" << endl;

exit(1);

}

else

{

  

//-| --------------------------------------------------------------------------------

//-| REPEAT: for each student, 1,2,3,..., numStudents:

//-| a) Read ID

//-| b) Sum each of the numScores scores

//-| c) Compute the average

//-| d) Write ID and aveage to averages file.

//-| --------------------------------------------------------------------------------

for (s=1; s <= numStudents; s++)

{

scoreF >> ID;

gradesF << setw(5) << left << ID << " ";

  

for (int i=1; i <= numScores; i++)

{

scoreF >> score; sumScores += score;

gradesF << setw(4) << right << score << " ";

}

avg = (float) sumScores / (float) numScores;

sumScores = 0;

  

gradesF << " " << setw(5) << right << setprecision(1)

<< fixed << avg << " ";

avgF << setprecision(1) << fixed << ID << " " << avg << endl;

savg += avg;

//-| ----------------------------------------------------------------------------------

//-| Note: At this point, all data have been read from the file.

//-| Make sure you did the work inside the repetition/loop

//-| structure.

//-| ----------------------------------------------------------------------------------

if(avg >= 90) {

cout << ID << " " << "grade = A" << endl;

gradesF << setw(2) << right << "A";

}

else if(avg >= 80 && avg < 90){

cout << ID << " " << "grade = B" << endl;

gradesF << setw(2) << right << "B";

}

else if(avg >= 70 && avg < 80){

cout << ID << " " << "grade = C" << endl;

gradesF << setw(2) << right << "C";

}

else if(avg >= 60 && avg < 70){

cout << ID << " " << "grade = D" << endl;

gradesF << setw(2) << right << "D";

  

}

else if(avg < 60){

cout << ID << " " << "grade = F" << endl;

gradesF << setw(2) << right << "F";

}

gradesF << endl << endl;

  

} // Forloop

  

} // Else

//-| ----------------------------------------------------------------------------------

//-| 2.

//-|-----------------------------------------------------------------------------------

cout << endl << "COURSE AVERAGE = " << setprecision(2) << fixed <<

(float) savg / numScores;

  

gradesF << "COURSE AVERAGE = " << setprecision(2)

<< fixed << (float) savg / numScores;

  

  

//-| ----------------------------------------------------------------------------------

//-| Close open file streams.

//-| ----------------------------------------------------------------------------------

scoreF.close();

avgF.close();

gradesF.close();

  

//-| ----------------------------------------------------------------------------------

//-| Display intellectual property copyright notice.

//-| ----------------------------------------------------------------------------------

cout << endl << endl << "(c) 2019, NAME" << endl << endl;

  

cout << "\n\n\nView output file 'averages.dat' \n\n";

system("pause");

return 0;

}

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

// scores.dat

4 5
111 89 98 87 98 89
222 90 98 87 98 87
333 54 67 78 65 56
444 76 65 55 87 78

________________________

// Level1)

#include <iostream>

#include <fstream>

#include <cstring>
#include <iomanip>

#include <cmath>

using namespace std;

  
bool validateStudentScore(int score);
char computeLetterGrade(double avg);
void displayCourseAverage(float savg,int numScores);
int main()

{

int numStudents = 0; // # students in class.

int numScores = 0; // # scores per student.

int ID = 0; // Student id (4-digits).

int score = 0; // Student score.

int s = 0; // Loop control variable (#students).

int sumScores = 0; // Sum of scores for given student.

float avg = 0; // Average score for given student.

float savg = 0;

ifstream scoreF; // Input file stream - student scores.

ofstream avgF; // Output file stream -- averages

ofstream gradesF; // [revOutput stream -- formatted grade book.

  

//-| -------------------------------------------------------------------------------

//-| Display intellectual property copyright notice.

//-| -------------------------------------------------------------------------------

cout << endl << "(c) 2019 NAME" << endl << endl;

  

  

//-| -------------------------------------------------------------------------------

//-| Open input/output file streams. If input file can not be

//-| opened, display message and terminate.

//-| -------------------------------------------------------------------------------

scoreF.open("scores.dat");

if (scoreF.fail())

{

cout << "FILE 'scores.dat' CANNOT BE OPENED." << endl << endl;

exit(1);

}   

avgF.open("averages.dat");

gradesF.open("grades.rpt");

  

//-| --------------------------------------------------------------------------------

//-| Do what comes next. Don't forget to describe your algorithm

//-| as program comments.

//-| --------------------------------------------------------------------------------

  

  

//-| --------------------------------------------------------------------------------

//-| Read control data from scores file: #students and #scores.

//-| --------------------------------------------------------------------------------

scoreF >> numStudents >> numScores;


avgF << "STUDENT AVERAGES" << endl << endl;

gradesF << "COURSE GRADES REPORT" << endl << endl;

  

if(!(numStudents >=1 && numScores >=1))

{

cout << "INVALID #STUDENTS (" << numStudents << ") or #SCORES (" << numScores

<< ")" << endl;

exit(1);

}

else

{

  

//-| --------------------------------------------------------------------------------

//-| REPEAT: for each student, 1,2,3,..., numStudents:

//-| a) Read ID

//-| b) Sum each of the numScores scores

//-| c) Compute the average

//-| d) Write ID and aveage to averages file.

//-| --------------------------------------------------------------------------------
int validCnt=0;
for (s=1; s <= numStudents; s++)

{
validCnt=0;
scoreF >> ID;

gradesF << setw(5) << left << ID << " ";

  

for (int i=1; i <= numScores; i++)

{

scoreF >> score;
bool b=validateStudentScore(score);
if(b)
{
sumScores += score;
validCnt++;  
}


gradesF << setw(4) << right << score << " ";

}

avg = (float) sumScores / (float) validCnt;

sumScores = 0;

  

gradesF << " " << setw(5) << right << setprecision(1)

<< fixed << avg << " ";

avgF << setprecision(1) << fixed << ID << " " << avg << endl;

savg += avg;

char letterGrade=computeLetterGrade(avg);

//-| ----------------------------------------------------------------------------------

//-| Note: At this point, all data have been read from the file.

//-| Make sure you did the work inside the repetition/loop

//-| structure.

//-| ----------------------------------------------------------------------------------

cout << ID << " " << "grade = "<<computeLetterGrade(avg) << endl;

gradesF << setw(2) << right << computeLetterGrade(avg);

gradesF << endl << endl;

  

} // Forloop

  

} // Else

//-| ----------------------------------------------------------------------------------

//-| 2.

//-|-----------------------------------------------------------------------------------

displayCourseAverage(savg,numScores);

  

gradesF << "COURSE AVERAGE = " << setprecision(2)

<< fixed << (float) savg / numScores;

  

  

//-| ----------------------------------------------------------------------------------

//-| Close open file streams.

//-| ----------------------------------------------------------------------------------

scoreF.close();

avgF.close();

gradesF.close();

  

//-| ----------------------------------------------------------------------------------

//-| Display intellectual property copyright notice.

//-| ----------------------------------------------------------------------------------

cout << endl << endl << "(c) 2019, NAME" << endl << endl;

  

cout << "\n\n\nView output file 'averages.dat' \n\n";

system("pause");

return 0;

}
bool validateStudentScore(int score)
{
   if(score<0 || score>100)
   return false;
   else
   return true;
}
char computeLetterGrade(double avg)
{
   char letterGrade;
   if(avg >= 90) {
       letterGrade='A';
   }
   else if(avg >= 80 && avg < 90){  
   letterGrade='B';
}
   else if(avg >= 70 && avg < 80){
       letterGrade='C';
   }
       else if(avg >= 60 && avg < 70){
           letterGrade='D';
       }
           else if(avg < 60){
               letterGrade='F';
           }
           return letterGrade;
}
void displayCourseAverage(float savg,int numScores)
{
   cout << endl << "COURSE AVERAGE = " << setprecision(2) << fixed <<(float) savg / numScores;
}

________________________

Output: (Console)

______________________

Add a comment
Know the answer?
Add Answer to:
C++ Redo PROG8, a previous program using functions and/or arrays. Complete as many levels as you...
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 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...

  • Please program in C++ and document the code as you go so I can understand what...

    Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...

  • Hello, I have written a code that I now need to make changes so that arrays...

    Hello, I have written a code that I now need to make changes so that arrays are transferred to the functions as pointer variable. Below is my code I already have. #include<iostream> #include<string> #include<iomanip> using namespace std; //functions prototypes void getData(string id[], int correct[], int NUM_STUDENT); void calculate(int correct[], int incorrect[], int score[], int NUM_STUDENT); double average(int score[], int NUM_STUDENT); void Display(string id[], int correct[], int incorrect[], int score[], double average, int NUM_STUDENT); int findHigh(string id[], int score[], int NUM_STUDENT);...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • Need help with a C++ program. I have been getting the error "this function or variable...

    Need help with a C++ program. I have been getting the error "this function or variable may be unsafe" as well as one that says I must "return a value" any help we be greatly appreciated. I have been working on this project for about 2 hours. #include <iostream> #include <string> using namespace std; int average(int a[]) {    // average function , declaring variable    int i;    char str[40];    float avg = 0;    // iterating in...

  • Arrays C++ #include <iostream> using namespace std; int main() {       int   tests[6]; // array declaration...

    Arrays C++ #include <iostream> using namespace std; int main() {       int   tests[6]; // array declaration       int   sum = 0;       float avg;       //input test scores       cout << " Enter " << 6 << " test scores: " << endl;       for (int i = 0; i < 6; i++)       {             cout << "Enter Test " << i + 1 << ": ";             cin >> tests[i];       }       return 0; }    Type...

  • 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;   ...

  • The following C++ program is a slightly modified version of Program 5.20 from Gary J. Bronson...

    The following C++ program is a slightly modified version of Program 5.20 from Gary J. Bronson textbook. In this program, you compute and display the average of three grades of each student for a class of four students. You need to modify the code to compute and display the class average as well as the standard deviation. Your code changes are as follows: 1. The variable “double grade” should be replaced by a two-dimensional array variable “double grade[NUMSTUDENTS][NUMGRADES].” Also replace...

  • C++ Assignment on Search and Sort for Chapter 8 Problem: Modify the student grade problem to...

    C++ Assignment on Search and Sort for Chapter 8 Problem: Modify the student grade problem to include the following: •   Write a Search function to search by student score •   Write a Search function to search by student last name •   Write a Sort function to sort the list by student score •   Write a Sort function to sort the list by student last name •   Write a menu function that lets user to choose any action he/she want to...

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