Free Essay

Ecet 370 Hw1

In:

Submitted By eddyjrny
Words 673
Pages 3
========================================
DeVry College of New York
ECET 370
HW #1: Dynamic 2D-array

-------------------------------------------------

Objective: Write a program to calculate students’ average test scores and their grades.
You may assume the following file input data:
Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63
Use three arrays: a one-dimensional array to store the students’ names, a
(parallel) two-dimensional array to store the test scores, and a parallel one dimensional array to store grades. Your program must contain at least the following functions: a function to read and store data into two arrays, a function to calculate the average test score and grade, and a function to output the results. Have your program also output the class average.

Tools required: PC & Visual studio software

Code:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

bool readStudentData(ifstream &inFile, string &name, int &score1, int &score2, int &score3, int &score4, int &score5) {

string line;

if (getline(inFile, line)) {

string word;

istringstream iss(line, istringstream::in); int count = 0; try { while (iss >> word) { //cout << word << endl; if (count == 0) name = word; if (count == 1) { istringstream(word) >> score1; } if (count == 2) { istringstream(word) >> score1; } if (count == 1) { istringstream(word) >> score2; } if (count == 3) { istringstream(word) >> score3; } if (count == 4) { istringstream(word) >> score4; } if (count == 5) { istringstream(word) >> score5; } count++;

} //cout << name << " "<<score1 << " "<<score2 << " "<<score3 << " "<<score4 << " "<<score5 <<endl;

return true; } catch (int e) { return false; } } return false;
}

float getAverage(int score1, int score2, int score3, int score4, int score5) { float avg = (float)(score1 + score2 + score3 + score4 + score5) / 5; return avg;
}

char computeGrade(float average) { //90-100 A, 80-89 B, 70-79 C, 60-69 D, 0-59 F. if (average >= 90 && average <= 100) return 'A'; else if (average >= 80 && average < 90) return 'B'; else if (average >= 70 && average < 80) return 'C'; else if (average >= 60 && average < 70) return 'D'; else return 'F';
}

int main() { string line; ifstream myfile("C:/Users/eddyj/Desktop/StudentScores/data.txt");

if (myfile.is_open()) { string names[50]; int scores[50][5]; char grades[50]; bool isValid = false; string name; int score1 = 0, score2 = 0, score3 = 0, score4 = 0, score5 = 0; int count = 0; while (myfile.good()) { isValid = readStudentData(myfile, name, score1, score2, score3, score4, score5); if (isValid) { float avg = getAverage(score1, score2, score3, score4, score5); char chgrade = computeGrade(avg); names[count] = name; scores[count][0] = score1; scores[count][1] = score2; scores[count][2] = score3; scores[count][3] = score4; scores[count][4] = score5; grades[count] = chgrade; count++; cout << name << " " << score1 << " " << score2 << " " << score3 << " " << score4 << " " << score5 << " , Average: " << avg << " , Grade: " << chgrade << endl; }

} myfile.close(); }

else cout << "Unable to open file";

}

Output:

Answer the following questions:
Describe any problems encountered and how these problems were solved.
It has been over a year since I last used visual studio and created a C++ program. Trial and error with the help of previous lab works from COMP 220

Describe the results of your lab
With this lab I was able to calculate students’ average test scores and grades

What did you learn from this lab?
This lab helped refresh my memory on how to use Dynamic 2-D Arrays

Similar Documents