Question

In: Computer Science

Consider the following C++ program that makes use of many features that are unique to C++...

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)

Solutions

Expert Solution

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.

  1. In C++, we use vectors to store the data, unlikely in C, there is no vectors, so need to use arrays.
  2. In C++, we have templates to accept any datatype, but in C there is no templates
  3. In C++, we have default string data type, but in C, there is no string datatype only character arrays are considered as string

Related Solutions

In C, write a program that makes use of the flags O_CREAT and O_EXCL, and observe...
In C, write a program that makes use of the flags O_CREAT and O_EXCL, and observe what does it do when you try to open an existing file
Consider the following program. // This program averages 3 test scores. It repeats as // many...
Consider the following program. // This program averages 3 test scores. It repeats as // many times as the user wishes. #include <iostream> using namespace std; int main() { int score1, score2, score3; // Three scores double average; // Average score char again; // To hold Y or N input do { // Get three scores. cout << "Enter 3 scores and I will average them: "; cin >> score1 >> score2 >> score3; // Calculate and display the average....
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an array. Print all unique elements of an array Enter the number of elements to be stored in the array: 4 Input 4 elements in the arrangement: element [0]: 3 element [1]: 2 element [2]: 2 element [3]: 5 Expected output: The only items found in the array are: 3 5
Create a C++ program that makes use of both concepts i.e. Array of structure and array...
Create a C++ program that makes use of both concepts i.e. Array of structure and array within the structure by using the following guidelines: 1. Create an Array of 5 Structures of Student Records 2. Each structure must contain: a. An array of Full Name of Student b. Registration Number in proper Format i.e 18-SE-24 c. An array of Marks of 3 subjects d. Display that information of all students in Ascending order using “Name” e. Search a particular student...
List three unique meteorological features of Superstorm Sandy
List three unique meteorological features of Superstorm Sandy
ASSIGNMENT: Answer the following questions below: List and describe the unique features and complexities of managing...
ASSIGNMENT: Answer the following questions below: List and describe the unique features and complexities of managing information systems projects. Of the unique features that make IT projects difficult to manage, which ones are most likely to prevail in the future? Which may become less important? Why? List and describe “soft skills” needed in managing projects. Why is each important?
What is menopause and what makes it unique?
What is menopause and what makes it unique?
Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
Consider the following program
Consider the following program:boolean blocked [2];int turn;void P (int id){while (true) {blocked[id] = true;while (turn != id) {while (blocked[1-id])/* do nothing */;turn = id;}/* critical section */blocked[id] = false;/* remainder */}}void main(){blocked[0] = false;blocked[1] = false;turn = 0;parbegin (P(0), P(1));}
( USE C++ ) The program prompts the user to enter a word. The program then...
( USE C++ ) The program prompts the user to enter a word. The program then prints out the word with letters in backward order. For example, if the user enter "hello" then the program would print "olleh" show that it works .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT