C CString and Twodimensional Array Use An input data file st

C++

C-String and Two-dimensional Array Use

An input data file starts with a student’s name (on one line). Then, for each course the student took last semester, the file has 2 data lines. The course name is on the first line. The second line has the student’s grade average (0 to 100) and the number of credits for the course.
Sample data:

Jon P. Washington, Jr.

Computer Science I

81 4

PreCalculus

75 3

Biology I
88 5

English II

100 3

Psychology I

68 3

Write a complete C++ program to compute the GPA for the student.
Array requirements: Use a two-dimensional char array for the course names, with one course name per row; a student may take at most 10 courses per semester. Use a parallel two- dimensional array with each row (which corresponds to a course) having three columns containing, respectively, the course grade average (input), the course credits (input) and the course GPA points (to be computed).

The GPA points for a course is the product of grade value (A is 4, B is 3, C is 2, D is 1, F is 0) times course credits.

Semester GPA is (sum of GPA points) / (sum of course credits)

Note: The grade average, number of course credits, and the GPA points can all be integers, but the GPA value must be decimal, and is to be output with 4 decimal places.

Function and output requirements: One function does input. One function computes the course GPA points in the array. One function computes the GPA. One function outputs to a file the student’s name, a table of the courses, grade received, course credits and GPA points earned, and finally, the student’s semester GPA.

Solution

#include<iostream>
#include<stdio.h>
#include<fstream>
#include<iostream>
#include<string>

using namespace std;

int number_of_crs = 0;

void input(char**, float**, char*);
void output(char**, float** , char* , float, char*);
int compute_gpa(float**, char*);

int main(){

    char* name_of_student;
    char* grades = new char[10];
    char **course_name = new char*[10];
    float **metrics = new float*[10];
    float sem_gpa;

    for(int i=0; i<10; i++){
        metrics[i] = new float[3];
    }

    input(course_name, metrics, name_of_student);
    sem_gpa = compute_gpa(metrics, grades);
    return 0;
}

void input(char **course_name, float **metrics, char* name_of_student){
    int i = 0;
    ifstream infile;
    infile.open(\"data.txt\");

    name_of_student = new char[256];
    infile.getline(name_of_student, 100);
    course_name[i] = new char[256];
    string line;
    while(infile.getline(course_name[i], 1000)){
        infile >> metrics[i][0] >> metrics[i][1];
        course_name[++i] = new char[256];
        infile.ignore();
    }

    number_of_crs = i;
}

int compute_gpa(float **metrics, char* grades){

    float sum_of_gpa = 0;
    float sum_of_cre = 0;

    for(int i=0; i< number_of_crs; i++){
        if(metrics[i][0] >= 90 && metrics[i][0] <= 100){
            metrics[i][2] = metrics[i][1]*4;
            grades[i] = \'A\';
        }
        else if(metrics[i][0] >= 80 && metrics[i][0] <= 89){
            metrics[i][2] = metrics[i][1]*3;
            grades[i] = \'B\';
        }
        else if(metrics[i][0] >= 70 && metrics[i][0] <= 79){
            metrics[i][2] = metrics[i][1]*2;
            grades[i] = \'C\';
        }
        else if(metrics[i][0] >= 60 && metrics[i][0] <= 69){
            metrics[i][2] = metrics[i][1];
            grades[i] = \'D\';
        }
        else if(metrics[i][0] >= 0 && metrics[i][0] <= 59){
            metrics[i][2] = 0;
            grades[i] = \'F\';
        }
        else{
            cout << \"Wrong Input\";
        }

        sum_of_gpa = sum_of_gpa + metrics[i][2];
        sum_of_cre = sum_of_cre + metrics[i][1];
    }

    return sum_of_gpa/sum_of_cre;
}

void output(char** course_name, float** metrics, char* name_of_student, float sem_gpa, char* grades){
    ofstream output_file(\"output.txt\");
    output_file << name_of_student << \"\ \";

    output_file.setf(ios::left, ios::adjustfield);
    output_file.width(10);
    output_file << \"Course\";
    output_file.setf(ios::left, ios::adjustfield);
    output_file.width(7);
    output_file << \"Grades\";
    output_file.setf(ios::left, ios::adjustfield);
    output_file.width(7);
    output_file << \"GPA\";
    output_file.setf(ios::left, ios::adjustfield);
    output_file.width(7);
    output_file << \"Credits\ \";


    for(int i=0; i< number_of_crs; i++){
        output_file.setf(ios::left, ios::adjustfield);
        output_file.width(10);
        output_file << course_name[i];
      
        output_file.setf(ios::left, ios::adjustfield);
        output_file.width(7);
        output_file << grades[i];

        output_file.setf(ios::left, ios::adjustfield);
        output_file.width(7);
        output_file << metrics[i][2];

        output_file.setf(ios::left, ios::adjustfield);
        output_file.width(7);
        output_file << metrics[i][1] << \"\ \";
    }


    output_file.precision(4);
    output_file << \"Semester GPA : \" << sem_gpa;
    output_file.close();

    return;
}

C++ C-String and Two-dimensional Array Use An input data file starts with a student’s name (on one line). Then, for each course the student took last semester,
C++ C-String and Two-dimensional Array Use An input data file starts with a student’s name (on one line). Then, for each course the student took last semester,
C++ C-String and Two-dimensional Array Use An input data file starts with a student’s name (on one line). Then, for each course the student took last semester,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site