I need to take this program and convert all of the arrays to dynamic arrays as w
ID: 3598803 • Letter: I
Question
I need to take this program and convert all of the arrays to dynamic arrays as well as delete the dynamic memory before the program is finished.
I would appreciate some help as dynamic arrays are lost on me.
Code:
#include <iostream> //cin & cout
#include <conio.h> //getch
#include <iomanip> //setiosflags & setprecision
using namespace std;
//declarations for the functions
float fGetGrades(int);
void fOutputGPA(const char *[], float[]);
inline float fCalculateGPA(float fGrades) {return fGrades / 5.0;};
inline void ProgramHeader() {cout << "Programmed by Hugh Schuett. " <<
"Student GPA Calculator. " << "Enter five (5) alphabetical (ABCDF) " <<
"grades for each student. " << flush; };
//the main
int main()
{
const char *cStudentName [4] = {"Freddie", "Jane", "Jonathan", "Mary"};
float fGradeTotals [4] = {0.0, 0.0, 0.0, 0.0};
int iCounter2;
ProgramHeader(); //function call for the program header
//the loop to fill the GradeTotals
for (int iCounter1 = 0; iCounter1 < 4; iCounter1++)
{
cout << " The students name is " << cStudentName[iCounter1] << " " << flush;
iCounter2 = 0;
fGradeTotals[iCounter1] = fGetGrades(iCounter2++);
} //end for loop
fOutputGPA(cStudentName, fGradeTotals);
return 0;
}//end main
//recursive function to get the grades and fill the array
float fGetGrades(int iGradeCounter)
{
char cGradeInput;
if (iGradeCounter < 5) //only gets 5 valid grades
{
cout << "Enter grade #" << (iGradeCounter + 1) << ": " << flush;
cGradeInput = getch();
cout << cGradeInput << " " << flush;
//the case to add up the grades
switch (cGradeInput)
{
case 'A':
case 'a':
return fGetGrades(iGradeCounter + 1) + 4.0;
break;
case 'B':
case 'b':
return fGetGrades(iGradeCounter + 1) + 3.0;
break;
case 'C':
case 'c':
return fGetGrades(iGradeCounter + 1) + 2.0;
break;
case 'D':
case 'd':
return fGetGrades(iGradeCounter + 1) + 1.0;
break;
case 'F':
case 'f':
return fGetGrades(iGradeCounter + 1) + 0.0;
break;
default: //the catch all for error checking
cout << "You must enter an A, B, C, D or F." << " " << flush;
return fGetGrades(iGradeCounter); //calls the function with out changing counter
} //end switch
}//end if
else
{
return 0.0; //returns a 0.0 when it is called again and has 5 valid grades
}//end else
}//end fGetGrades
//function for output
void fOutputGPA(const char *cNameStudent[], float fGPAs[])
{
//the begining of the output
cout << " The following are the GPA's for each of the students. " <<
setw(12) << setiosflags(ios::fixed | ios::left) << "Student"
<< "GPA " << "-------- ----- " << flush;
//output for loop. it's to go thru the arrays
for (int iOutCounter = 0; iOutCounter < 4; iOutCounter++)
{
cout << setw(12) << setiosflags(ios::fixed | ios::left) << cNameStudent[iOutCounter] <<
setiosflags(ios::fixed | ios::showpoint) <<
setprecision (3) << fCalculateGPA(fGPAs[iOutCounter]) << " " << flush;
} //end of output for loop
cout << " Press any key to continue..." << flush;
getch();
}
Explanation / Answer
//you can find my changes with keyword , CheggEA
#include <iostream> //cin & cout
#include <conio.h> //getch
#include <iomanip> //setiosflags & setprecision
using namespace std;
//declarations for the functions
float fGetGrades(int);
void fOutputGPA(const char *[], float[]);
inline float fCalculateGPA(float fGrades) { return fGrades / 5.0; };
inline void ProgramHeader() {
cout << "Programmed by Hugh Schuett. " <<
"Student GPA Calculator. " << "Enter five (5) alphabetical (ABCDF) " <<
"grades for each student. " << flush;
};
//the main
int main()
{
const char *cStudentName[4] = { "Freddie", "Jane", "Jonathan", "Mary" };
//CheggEA, commented out below line to declare fGradeTotals as dynamic array of size 4
//float fGradeTotals[4] = { 0.0, 0.0, 0.0, 0.0 };
float *fGradeTotals;
//Allocate memory for dynamic array
fGradeTotals = new float[4];
//now assign value 0 to dynamic array
for (int i = 0; i < 4; i++)
fGradeTotals[i] = 0.0;
//End of Modifying, CheggEA
int iCounter2;
ProgramHeader(); //function call for the program header
//the loop to fill the GradeTotals
for (int iCounter1 = 0; iCounter1 < 4; iCounter1++)
{
cout << " The students name is " << cStudentName[iCounter1] << " " << flush;
iCounter2 = 0;
fGradeTotals[iCounter1] = fGetGrades(iCounter2++);
} //end for loop
fOutputGPA(cStudentName, fGradeTotals);
//CheggEA, delete dynamic array
delete[]fGradeTotals;
return 0;
}//end main
//recursive function to get the grades and fill the array
float fGetGrades(int iGradeCounter)
{
char cGradeInput;
if (iGradeCounter < 5) //only gets 5 valid grades
{
cout << "Enter grade #" << (iGradeCounter + 1) << ": " << flush;
cGradeInput = getch();
cout << cGradeInput << " " << flush;
//the case to add up the grades
switch (cGradeInput)
{
case 'A':
case 'a':
return fGetGrades(iGradeCounter + 1) + 4.0;
break;
case 'B':
case 'b':
return fGetGrades(iGradeCounter + 1) + 3.0;
break;
case 'C':
case 'c':
return fGetGrades(iGradeCounter + 1) + 2.0;
break;
case 'D':
case 'd':
return fGetGrades(iGradeCounter + 1) + 1.0;
break;
case 'F':
case 'f':
return fGetGrades(iGradeCounter + 1) + 0.0;
break;
default: //the catch all for error checking
cout << "You must enter an A, B, C, D or F." << " " << flush;
return fGetGrades(iGradeCounter); //calls the function with out changing counter
} //end switch
}//end if
else
{
return 0.0; //returns a 0.0 when it is called again and has 5 valid grades
}//end else
}//end fGetGrades
//function for output
void fOutputGPA(const char *cNameStudent[], float fGPAs[])
{
//the begining of the output
cout << " The following are the GPA's for each of the students. " <<
setw(12) << setiosflags(ios::fixed | ios::left) << "Student"
<< "GPA " << "-------- ----- " << flush;
//output for loop. it's to go thru the arrays
for (int iOutCounter = 0; iOutCounter < 4; iOutCounter++)
{
cout << setw(12) << setiosflags(ios::fixed | ios::left) << cNameStudent[iOutCounter] <<
setiosflags(ios::fixed | ios::showpoint) <<
setprecision(3) << fCalculateGPA(fGPAs[iOutCounter]) << " " << flush;
} //end of output for loop
cout << " Press any key to continue..." << flush;
getch();
}
-----------------------------------------------------------------------
//output After making change,,it works as before
Programmed by Hugh Schuett.
Student GPA Calculator.
Enter five (5) alphabetical (ABCDF) grades for each student.
The students name is Freddie
Enter grade #1: A
Enter grade #2:
You must enter an A, B, C, D or F.
Enter grade #2: B
Enter grade #3:
You must enter an A, B, C, D or F.
Enter grade #3: C
Enter grade #4: D
Enter grade #5: A
The students name is Jane
Enter grade #1: A
Enter grade #2: A
Enter grade #3: A
Enter grade #4: B
Enter grade #5: A
The students name is Jonathan
Enter grade #1: A
Enter grade #2: D
Enter grade #3: B
Enter grade #4: C
Enter grade #5: A
The students name is Mary
Enter grade #1: A
Enter grade #2: A
Enter grade #3: B
Enter grade #4: B
Enter grade #5: B
The following are the GPA's for each of the students.
Student GPA
-------- -----
Freddie 2.800
Jane 3.800
Jonathan 2.800
Mary 3.400
Press any key to continue...