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 create a program that stores contact info. The program must have the following features:...
In C create a program that stores contact info. The program must have the following features: Able to store First Name, Phone Number and Birthday (MM/DD/YYYY) Able to search for a specific contact using First Name, Phone Number and Birthday (find) Able to delete a specific contact using First Name, Phone Number and Birthday (delete) Print entire contact list Show number of entries in contact list Able to save Contact List to file (save) Able to load Contact List from...
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?
Using C++ Many software applications use comma-separated values to store and transfer data. In this program,...
Using C++ Many software applications use comma-separated values to store and transfer data. In this program, you are asked to implement the movie_call function that will parse a string of the above structure into the corresponding movie name, rating, and movie length. ex: In "movie name, rating, length", the movie name is "movie name", the rating is "rating", and the length of the movie is "lenght". This is the code: #include <iostream> #include <string> using namespace std; /* Replace the...
What is menopause and what makes it unique?
What is menopause and what makes it unique?
******Please don't use handwriting and i need unique answer ... Thank you Q3: Consider the following...
******Please don't use handwriting and i need unique answer ... Thank you Q3: Consider the following relation: Student-Dept = (Student-ID, Course, SportActivity, Dept-Name, Building) Having following multivalued dependencies:             F ={ Student-ID ®® Course                    Student-ID ®® SportsActivity                      Dept-Name ®® Building } Explain in your own words why the Student-Dept relation is not in 4NF. Then, convert the Student-Dept relation in 4NF. Also, provide the justification for each step you perform during normalization (4NF). Note: The SportActivity here...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT