In: Computer Science
Write a program that uses a structure to store the following data:
(Remember to use proper formatting and code documentation)
Member Name | Description |
name | student name |
idnum | Student ID number |
Scores [NUM_TESTS] | an array of test scores |
Average | Average test score |
Grade | Course grade |
Declare a global const directly above the struct declaration const int NUM_TESTS = 4; //a global constant |
The program should ask the user how many students there are and should then dynamically allocate an array of structures.
The Student struct includes a fixed size scores array to store a list of four test scores for a group of students .
After the array of struct has been dynamically allocated, the program should ask for the name, ID number and four test scores for each student. The average test score should be calculated and stored in the average member of each structure.
The course grade should be computed on the basis of the following grading scale:
Average Test Grade | Course Grade |
91-100 | A |
81-90 | B |
71-80 | C |
61-70 | D |
60 or below | F |
The course grade should then be stored in the Grade member of each structure.
Once all this data is calculated, a table should be displayed on the screen listing each student's name, ID number, average test score, and course grade.
Input Validation: Be sure all the data for each student is entered. Do not accept negative numbers for any test score.
Simple Screen Output How many students? 3 Student name: Sandy Shell ID Number: 12345 Test # 1: 100 Test # 2: 56 Test # 3: 78 Test # 4: 66 Student name: Timothy Moriarty ID Number: 23456 Test # 1: 90 Test # 2: 78 Test # 3: 45 Test # 4: 56 Student name: Joice Smith ID Number: 54321 Test # 1: 100 Test # 2: 98 Test # 3: 67 Test # 4: 75 =========================== Course Grade Report =========================== Student name: Sandy Shell ID number: 12345 Average test score: 75.0 Grade: C Student name: Timothy Moriarty ID number: 23456 Average test score: 67.2 Grade: D Student name: Joice Smith ID number: 54321 Average test score: 85.0 Grade: B Process returned 0 (0x0) execution time : 94.219 s Press any key to continue.
___________________________________________________________________________________________
Please make sure to declare the global constant and your struct above any function prototypes and include appropriate function documentation as needed.
Here is a sample of function prototypes that you may consider implementing...
// Function prototypes Student *initArrays(int); void getInfo(Student [], int); void showInfo(Student [], int);
You may use pointers or constant reference parameters when passing the structure to the functions mentioned above.
*EXTRA CREDIT COMPONENT FOR ASSIGNMENT#7 (5 points) - Select only one and insert as a multiline comment in your source code submitted.
1. How would you modify the program that you wrote for Programming Challenge 7a (monthly budget) so it defines an enumerated data type with enumerators for the months (JANUARY, FEBRUARY, so on). Provide a code snippet on how the program may use the enumerated type to step through each months expense and budget comparison for a full year.
or
2. How would you modify the program that you wrote for Programming Challenge 7b (course grades) so it defines struct member scores as a pointer to an array of test scores. What additional program statements will be needed so that each structure's score member should point to a dynamically allocated array that will hold the test scores.
/***************************************************/
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;
const int NUM_TESTS = 4;
// create Student structure
struct Student
{
// Declaring variables
string name;
int idnum;
int Scores[NUM_TESTS];
double Average;
char Grade;
};
// Function prototypes
Student* initArrays(int);
void getInfo(Student[], int);
void showInfo(Student[], int);
int main()
{
// Declaring variables
int noOfStudents;
// Getting the input entered by the user
cout << "How many students ? ";
cin >> noOfStudents;
cout << endl;
// calling the functions
Student* array = initArrays(noOfStudents);
getInfo(array, noOfStudents);
showInfo(array, noOfStudents);
return 0;
}
// Thuis function will create struct array dynamically
Student* initArrays(int n)
{
// Creating array dynamically
Student* studs = new Student[n];
return studs;
}
/*
* This function will read the inputs entered by the user
* and populate those values into array
*/
void getInfo(Student s[], int n)
{
for (int i = 0; i < n; i++)
{
cin.ignore();
cout << "Student name :";
getline(cin, s[i].name);
cout << "ID Number : ";
cin >> s[i].idnum;
for (int j = 0; j < NUM_TESTS; j++)
{
cout << "\tTest # " << (j + 1) << ": ";
cin >> s[i].Scores[j];
}
}
}
/*
* This function will display the report
*/
void showInfo(Student s[], int n)
{
// setting the precision to two decimal places
std::cout << std::setprecision(1) << std::fixed;
double average = 0.0, sum = 0.0;
char gradeLetter;
cout << "==========================================="
<< endl;
cout << "Course Grade Report" << endl;
cout << "==========================================="
<< endl;
for (int i = 0; i < n; i++)
{
sum = 0.0;
cout << "Student name :" << s[i].name <<
endl;
cout << "ID number :" << s[i].idnum <<
endl;
for (int j = 0; j < NUM_TESTS; j++)
{
sum += s[i].Scores[j];
}
average = (sum / NUM_TESTS);
if (average >= 90 && average <= 100)
gradeLetter = 'A';
else if (average >= 80 && average < 90)
gradeLetter = 'B';
else if (average >= 70 && average < 80)
gradeLetter = 'C';
else if (average >= 60 && average < 70)
gradeLetter = 'D';
else if (average < 60)
gradeLetter = 'F';
cout << "Average test score: " << average <<
endl;
cout << "Grade " << gradeLetter << endl;
cout << endl;
}
}
/***************************************************/
/***************************************************/
Output:
/***************************************************/