Good Afternoon,
I'm having trouble process 2-D arrays to store and process data. I have problems with implementing the void grading function and void showgrades function
So far I have this code:
Cpp file:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include "lab_20_head.h"
using namespace std;
int main( )
{
//define a const array size
const int SIZE = 35; //class size
const int COLUMN = 11; //column size
//declare five parallel arrays for firstName, lastName, and ID
string firstNames[SIZE], //firstnames for students
lastNames[SIZE], //last names for students
IDs[SIZE]; //IDs for students
double scores[SIZE]; //scores for students
char grades[SIZE]; //letter grades
//declare two dim array for class info
double csci1370[SIZE][COLUMN]; //for att, quiz, hw, tests and score
//file var
ifstream inFile; //input file
int row, col; //loop var
//open file
inFile.open("lab_20_data.txt");
if(!inFile)
{
cout<<"Fail to open lab_20_data.txt, and stop ."<<endl;
return 1;
}
//initialize array letterStata
loadData(inFile, firstNames, lastNames, IDs, csci1370, SIZE, COLUMN);
//close inFile
inFile.close();
//compute score and grades
grading(csci1370, scores, grades, SIZE, COLUMN);
//show grades
showGrades(firstNames, lastNames, IDs, csci1370, scores, grades, SIZE, COLUMN);
//well done and exit
return 0;
}
Header file:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
#ifndef LAB_20_HEAD_H
#define LAB_20_HEAD_H
/********************************************************************
This function gets class information from the input file and store
the info to three parallel array and one two dim array.
inFile: input file
firstNames, lastNames, IDs: three output parallel arrays
csci1370: two dim output array
SIZE, COLUMN: two input parameters
*********************************************************************/
void loadData(ifstream & inFile,
string firstNames[], string lastNames[ ], string IDs[ ],
double csci1370[][11], const int SIZE, const int COLUMN)
{
char str[1024];
//skip the first three lines
inFile.getline(str, 1080, '\n');
cout<<str<<endl;
inFile.getline(str, 1080, '\n');
cout<<str <<endl;
inFile.getline(str, 1080, '\n');
cout<< str<<endl;
for (int row = 0; row < SIZE; row++)
{
inFile>> firstNames[row] //get first name
>> lastNames[row] //get last name
>> IDs[row]; //get ID
for (int col = 0; col <COLUMN; col++)
{
inFile >> csci1370[row][col]; //get a row for csci1370
}
}
}
/*********************************************************************
This function computes scores for each student and decides
his/her grade. The formula to calculate the scores of attendance 5%,
quiz 5%, homework 30%, tests 60%. The grade is decided as follows:
A if score >= 90, B if 80<= score <90, C if 70<= score < 80,
D if 60 <= score <70, and F otherwise.
*********************************************************************/
void grading(const double csci1370[][11], //input array
double scores[], //output array for scores
char grades[], //output array for grades
const int SIZE, const int COLUMN) //input parameters
{
int average,total;
for (int row =0; row < SIZE; row++)
{
total = 0;
for (int COLUMN = 0; COLUMN < SIZE; COLUMN++)
{
double attendance, quiz, homework, tests;
double hw1, hw2, hw3, hw4, hw5, hw6, T1, T2, T3;
attendance = attendance * .05;
quiz = quiz * .05;
homework = (hw1 + hw2 + hw3 + hw4 + hw5 + hw6) / 6;
homework = homework * .30;
tests = (T1 + T2 + T3) / 3;
tests = tests * .60;
average = (attendance + quiz + homework + tests) / 4;
average += scores[row][COLUMN];
if (scores[row] >= 90)
{
grades[row] = 'A';
}
else if (scores[row] <= 80)
{
grades[row] = 'B';
}
else if (scores[row] <= 70)
{
grades[row] = 'C';
}
else if (scores[row] <= 60)
{
grades[row] = 'D';
}
else
grades[row] = 'F';
}
}
}
/****************************************************************
This function displays class information for CSCI 1370
in some nice format.
****************************************************************/
void showGrades(const string firstNames[],
const string lastNames[],
const string IDs[],
const double csci1370[][11],
const double scores[],
const char grades[],
const int SIZE, const int COLUMN)
{
for (int i = 0; i < SIZE; i++)
{
cout<<firstNames[i]
<<lastNames[i]
<<IDs[i]
<<grades<<endl;
}
#endif