Question
C++: Im trying to remove any hard coded values in my code which will be provided below. we are not supposed to use hardcoded values
C++:
Im trying to remove any hard coded values in my code which will be provided below. we are not supposed to use hardcoded values instead we are supposed to use the heap by dynamically-allocated memory for how much the user enters the number of students. So for example the user can enter as many students as they want and everything should be going through the heap and pointerss and refrences. My header for the input Function should be like this: int inputData(string*&, double**&); for the harded coded valuse i mean like: int numberOfTests[10]; OR char calgrade[10]; so these shouldnt be like this it shoud be the user can enter as many as they want.
so here is my code:
#include
#include
using namespace std;
int numberOfTests[10];
char calgrade[10];
int howManyStudents;
int inputData(string* str, double** grade);
char* calcGrade(double** grade, int howManyStudents);
void displayData(string* students, double** grade, char* calgrade, int howManyStudents);
void cleanHeap(string* students, double** grade, char* calgrade, int howManyStudents);
int main()
{
string students[10];
double** ary = new double*[10];
for(int i = 0; i < 10; ++i)
ary[i] = new double[10];
inputData(students,ary);
calcGrade(ary,howManyStudents);
displayData(students,ary,calgrade,howManyStudents);
}
int inputData(string* str, double** grade)
{
int numStudents;
double gr;
string students;
cout << "How many students do you have in the system? ";
cin >> numStudents;
cout << endl;
str = new string[numStudents];
for(int i = 0; i < howManyStudents; i++)
{
cout << "Enter the student's name: ";
cin.ignore();
getline(cin, students);
label1 :
cout << "How many tests " << students << " took: ";
cin >> numberOfTests;
if( numberOfTests[i] < 0)
{
cout << "Please enter a positive number of tests taken" << endl;
goto label1;
}
str[i] = students;
for(int j = 0; j < numberOfTests[i]; j++)
{
label2 :
cout << "please enter grade #"<< j+1 << ": ";
cin >> gr;
if( gr < 0 || gr > 100)
{
cout << "Please enter a grade between 0 and 100." << endl;
goto label2;
}
grade[i][j] = gr;
}
cout << endl;
}
}
char* calcGrade(double** grade, int howManyStudents)
{
double average;
for(int i = 0; i < howManyStudents; i++)
{
average = 0;
for(int j = 0; j < numberOfTests[i]; j++)
average += grade[i][j];
average = average/numberOfTests[i];
if(average >= 90 && average <= 100)
calgrade[i] = 'A';
else if(average >= 80 && average <= 89)
calgrade[i] = 'B';
else if(average >= 70 && average <= 79)
calgrade[i] = 'C';
else if(average >= 60 && average <= 69)
calgrade[i] = 'D';
else if(average < 60)
calgrade[i] = 'F';
}
return calgrade;
}
void displayData(string* students, double** grade, char* calgrade, int howManyStudents)
{
cout << " You have " << howManyStudents << " students in the system. ";
for(int i = 0; i < howManyStudents; i++)
{
cout << " Name of the student #" << i+1 << ": " << students[i];
cout << " Grades for student #" << i+1 << ": ";
for(int j = 0; j < numberOfTests[i]; j++)
cout << grade[i][j] << " ";
cout << " Average of grades for student #" << i+1 << ": " << calgrade[i];
cout << endl;
cout << endl;
}
}
void cleanHeap(string* students, double** grade, char* calgrade, int howManyStudents)
{
delete[] students;
delete[] calgrade;
for(int i = 0; i < 10; ++i)
delete[] grade[i];
delete[] grade;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started