In: Computer Science
Consider the following C++ program that makes use of many
features that are unique to C++ and did not
exist in C.
#include <iostream>
#include <string>
#include <cstdint>
#include <vector>
using namespace std;
enum LetterGrade {
A = 4,
B = 3,
C = 2,
D = 1,
F = 0
};
// type T must be castable into a double
template<class T>
double getArrayAverage(vector<T>& vec) {
double sum = 0;
for (const auto& value : vec) {
sum += static_cast<double>(value);
}
const auto avg = sum / vec.size();
return avg;
}
void convertCharToLetterGrade(char& grade) {
switch (grade) {
case 'A': case 'a':
grade = 4;
return;
case 'B': case 'b':
grade = 3;
return;
case 'C': case 'c':
grade = 2;
return;
case 'D': case 'd':
grade = 1;
return;
case 'F': case 'f':
grade = 0;
return;
default:
cout << "Warning... Invalid Character... Recording an
F.\n";
return;
}
}
LetterGrade getLetterGradeFromAverage(const double avg) {
if (avg >= 90)
return LetterGrade::A;
else if (avg >= 80)
return LetterGrade::B;
else if (avg >= 70)
return LetterGrade::C;
else if (avg >= 60)
return LetterGrade::D;
else
return LetterGrade::F;
}
int main()
{
string firstName;
cout << "Please enter your first name: ";
cin >> firstName;
string lastName;
cout << "Please enter your last name: ";
cin >> lastName;
int32_t numPrevCourses;
cout << "Enter number of previous courses: ";
cin >> numPrevCourses;
cin.ignore();
vector<LetterGrade> prevGrades(numPrevCourses);
for (int32_t courseIx = 0; courseIx < numPrevCourses;
++courseIx) {
cout << "Enter letter grade for course " << courseIx
<< ": ";
char letterGrade;
cin.get(letterGrade);
cin.ignore();
convertCharToLetterGrade(letterGrade);
prevGrades.at(courseIx) =
static_cast<LetterGrade>(letterGrade);
}
int32_t numExams;
cout << "Enter number of exams this semester: ";
cin >> numExams;
cin.ignore();
vector<int32_t> examGrades(numExams);
for (int32_t examIx = 0; examIx < numExams; ++examIx) {
cout << "Enter grade for exam " << examIx << " as
an integer: ";
cin >> examGrades.at(examIx) ;
cin.ignore();
}
const auto fullName = firstName + " " + lastName;
cout << "Grade Report For " << fullName <<
":\n";
const auto examAverage = getArrayAverage(examGrades);
cout << "Your exam average is: " << examAverage
<< "\n";
// get GPA with newest course added:
const auto newLetterGrade =
getLetterGradeFromAverage(examAverage);
prevGrades.push_back(newLetterGrade);
const auto gpa = getArrayAverage(prevGrades);
cout << "Your latest GPA is: " << gpa <<
"\n";
return 0;
}
Your Task: Please rewrite this program in pure
C and without any C++ elements. You may use any
compiler that you would like, but your program cannot have any C++
features .
Once you finish writing your program, please write a brief
report (no more than a few paragraphs)
describing features in the above program that are in C++ and not in
C and the different work-arounds you
had to come up with in order to achieve the same functionality.
Please feel free to elaborate on the
aspects of the C program that were difficult to implement.
Please submit your program (as a .c file) as well as your report
(any text format will suffice)
If you have any doubts, please give me comment...
#include <stdio.h>
#include <string.h>
#include <stdint.h>
typedef enum LetterGrade
{
A = 4,
B = 3,
C = 2,
D = 1,
F = 0
} LetterGrade;
double getArrayAverage(int32_t arr[], int n)
{
double sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
double avg = sum / n;
return avg;
}
int convertCharToLetterGrade(char grade)
{
switch (grade)
{
case 'A':
case 'a':
{
return 4;
}
case 'B':
case 'b':
{
return 3;
}
case 'C':
case 'c':
{
return 2;
}
case 'D':
case 'd':
{
return 1;
}
case 'F':
case 'f':
{
return 0;
}
default:
printf("Warning... Invalid Character... Recording an F.\n");
return 0;
}
}
LetterGrade getLetterGradeFromAverage(const double avg)
{
if (avg >= 90)
return A;
else if (avg >= 80)
return B;
else if (avg >= 70)
return C;
else if (avg >= 60)
return D;
else
return F;
}
int main()
{
char firstName[50];
printf("Please enter your first name: ");
scanf("%s", firstName);
char lastName[50];
printf("Please enter your last name: ");
scanf("%s", lastName);
int32_t numPrevCourses;
printf("Enter number of previous courses: ");
scanf("%d", &numPrevCourses);
int32_t prevGrades[numPrevCourses+1];
for (int32_t courseIx = 0; courseIx < numPrevCourses; ++courseIx)
{
printf("Enter letter grade for course %d: ", courseIx);
char letterGrade;
scanf("\n%c", &letterGrade);
int32_t grade = convertCharToLetterGrade(letterGrade);
prevGrades[courseIx] = grade;
}
int32_t numExams;
printf("Enter number of exams this semester: ");
scanf("%d", &numExams);
int32_t examGrades[numExams];
for (int32_t examIx = 0; examIx < numExams; ++examIx)
{
printf("Enter grade for exam %d as an integer: ", examIx);
scanf("%d", &examGrades[examIx]);
}
char fullName[100];
strcat(fullName, firstName);
strcat(fullName, " ");
strcat(fullName, lastName);
printf("Grade Report For %s:\n", fullName);
double examAverage = getArrayAverage(examGrades, numExams);
printf("Your exam average is: %lf\n", examAverage);
// get GPA with newest course added:
LetterGrade newLetterGrade = getLetterGradeFromAverage(examAverage);
prevGrades[numPrevCourses] = newLetterGrade;
numPrevCourses++;
double gpa = getArrayAverage(prevGrades, numPrevCourses);
printf("Your latest GPA is: %lf\n", gpa);
return 0;
}
Features that are unique to C++ and did not exist in C.