Question

Program 5 Due 10/25 C-String and Two-dimensional Array Use An input data file starts with a students 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 students 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 68 3 Write a complete C++ program to compute the GPA for the student. uirements: Use a two-dimensional char array for the course names, with one course a student may take at most 10 courses per semester. Use a parallel two- name per row; 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. (sum of GPA points) (sum of course credits) Semester GPA is 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 students name, a table of the courses, grade rececived, course credits and GPA points earned. and finally, the students semester GPA.


0 0
Add a comment Improve this question Transcribed image text
Answer #1


//below is code written in C++ to perform as per the requirement.

//NOTE:1 Please note that the below programs have been tested on ubuntu 14.04 system and compiled under g++ compiler. This code will also work on other IDE's.

// C++ code
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;

int size = 0;

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

int main(){

char* studentName;
char* grades = new char[10];
char **nameCOurse = new char*[10];
float **metrics = new float*[10];
float GPA;

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

input(nameCOurse, metrics, studentName);
GPA = compute_gpa(metrics, grades);

output(nameCOurse, metrics,studentName, GPA, grades);

cout << "Results written to output.txt\n";
return 0;
}

void input(char **nameCOurse, float **metrics, char* studentName){
int i = 0;
ifstream inFile;
inFile.open("input.txt");

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

size = i;
}

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

float sum_of_gpa = 0;
float sum_of_cre = 0;

for(int i=0; i< size; 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** nameCOurse, float** metrics, char* studentName, float GPA, char* grades)
{
ofstream outFile("output.txt");

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


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

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

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


outFile.precision(4);
outFile << "Semester GPA : " << GPA << endl;
outFile.close();

return;
}


input.txt
Jon P. Washington, Jr.
Computer Science I
81 4
PreCalculus
75 3
Biology I
88 5
English II
100 3
Psychology I
68 3

output.txt
Course Grades GPA Credits
Computer Science IB 12 4
PreCalculusC 6 3
Biology I B 15 5
English IIA 12 3
Psychology ID 3 3
Semester GPA : 2



Terminal 四 1回(2:47, 72%) 11:16 AM ※ubuntu new. py new.cpP input.txt output.txt nums.txt songs.dat 2 I/below is code written i

Add a comment
Know the answer?
Add Answer to:
Program 5 Due 10/25 C-String and Two-dimensional Array Use An input data file starts with a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT