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

In grading(), you declare hw1, hw2, and so on, but never assign any values to them before you use them. This means that they will be filled with whatever garbage happened to be in memory before the function was called.

Schol-R-Lea,

The problem is that those values: att, hw1, hw2, hw3, hw5 and so on are from a data file. I don't how to get those specific value and perform the appropriate calculations for each value and output the letter grade for every student in the data file. Below is the data file that I'm working with.

-------------------------------------------------------------------
firt    last    ID  att hw1 hw2 hw3 hw4 hw5 hw6 T1  T2  T3 Quiz
-------------------------------------------------------------------
cat     Dog     111111  80  90  80  90  90  100 99  90  89  99  100
Bat     Hog     111122  81  97  92  78  89  98  80  99  88  97  100
Tag     Tug     111133  67  92  90  50  70  100 79  90  99  94  87
Fat     What    111122  84  57  72  68  99  78  60  89  96  90  68
Hat     Where   111111  85  60  90  80  70  100 99  90  99  92  87
Tat     Who     111122  86  96  92  88  89  78  90  79  96  90  68
Jose    Jones   111111  90  93  50  70  90  100 99  90  99  93  90
Jane    Smith   111122  71  87  72  68  89  98  90  89  96  80  68
John    Hill    111111  70  60  90  90  90  100 99  60  86  89  87
Ann     Tor     111122  89  57  72  68  89  98  90  89  87  80  89
Anna    Why     111111  88  99  80  50  80  100 79  80  79  89  96
Ben     Good    111122  86  99  82  68  89  98  70  99  66  97  98
Benny   C++     111111  70  99  80  90  80  100 69  80  99  93  87
Will    PC      111122  91  99  92  68  89  78  80  99  96  96  99
Willy   Guess   111111  50  94  80  50  70  100 69  60  80  79  87
Cath    Fun     111122  41  94  92  98  89  98  80  89  87  60  68
Cathy   Now     111111  86  90  80  50  80  100 69  80  89  89  87
Dash    Road    111122  71  95  88  68  89  78  90  89  96  90  68
David   Texas   111111  60  95  80  90  70  100 99  90  79  99  89
Eric    McAllen 111122  89  87  92  68  89  98  90  89  86  90  88
Eerika  Edwin   111111  87  80  96  50  70  100 79  90  89  89  87
Frank   Wired   111122  71  87  82  88  99  78  90  69  86  66  68
Ford    Joker   111111  90  80  90  50  90  100 89  90  89  99  87
Greg    Wind    111122  21  77  87  68  99  98  60  99  89  90  68
George  Pang    111111  40  70  90  50  90  100 99  90  99  99  45
Hush    Ting    111122  87  97  82  98  99  88  60  99  96  90  68
Hate    Pada    111111  60  90  80  50  90  100 79  77  99  99  55
Isee    Radar   111122  87  97  82  68  89  78  60  90  96  70  68
Irish   Web     111111  83  70  90  90  90  100 99  70  89  89  87
Jeb     Food    111122  86  98  88  68  89  68  80  99  86  50  97
Joke    Webster 111111  89  91  90  50  70  100 89  70  89  89  84
Kurt    Lane    111122  91  90  92  98  99  78  80  89  80  98  88
Kevin   City    111122  71  96  92  68  89  98  90  90  96  95  98
Larry   Games   111111  84  80  92  90  70  100 99  70  89  99  87
Lucy    Park    111122  61  100 99  98  99  98  90  99  86  99  98

Just an unrelated question: am I correct in thinking that with this assignment, and most of the others you've been working on, the intructor has provided the main() function and a skeleton for the header file? I ask this to get a sense of how free you are to take different approaches than the one you've been using; for example, rather than have several indipendent arrays which would nee dto be coordinated between, I would use a struct type, but I am pretty sure you haven't covered structs yet so that would be of little help to you.

Schol-R-Lea,

But, the function void loadData already load the all information, so how can I work with att and multiply it by 5% so I can work my way from there to calculate the letter grade.

AH, it took me a bit to work out what you were saying, but I think I understand the issue now. What you need to do is assign the values in csci1370[][] to the different variables, in effect 'unrolling' the array. You want to get rid of the inner loop entirely, and have something like this inside what is now the outer loop:

    total = 0;
    double attendance, quiz, homework, tests;
    double hw1, hw2, hw3, hw4, hw5, hw6, T1, T2, T3;

    attendance = csci1370[row][0];
    hw1 = csci1370[row][1];
    hw2 = csci1370[row][2];
    hw3 = csci1370[row][3];
    hw4 = csci1370[row][4];
    hw5 = csci1370[row][5];
    hw6 = csci1370[row][6];
    T1 = csci1370[row][7];
    T2 = csci1370[row][8];
    T3 = csci1370[row][9];
    quiz = csci1370[row][10];

This will assign the correct values to each of the variables.

Schol-R-Lea,

When I run the program, it just give the letter grade of 'B' for all the students. This the code for the grading function

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
{

    for (int row =0; row < SIZE; row++)
    {
        double total = 0;
        double att, quiz, homework, tests, average;
        double hw1, hw2, hw3, hw4, hw5, hw6, T1, T2, T3;

        att = csci1370[row][0];
        hw1 = csci1370[row][1];
        hw2 = csci1370[row][2];
        hw3 = csci1370[row][3];
        hw4 = csci1370[row][4];
        hw5 = csci1370[row][5];
        hw6 = csci1370[row][6];
        T1 = csci1370[row][7];
        T2 = csci1370[row][8];
        T3 = csci1370[row][9];
        quiz = csci1370[row][10];

        att = att * .05;
        quiz = quiz * .05;
        homework = (hw1 + hw2 + hw3 + hw4 + hw5 + hw6) / 6;
        homework = homework * .30;
        tests = (T1 + T2 + T3) / 3;
        tests = tests * .60;
        average = (att + quiz + homework + tests) / 4;
        average += scores[row];

            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]<<"\t"
            <<lastNames[i]<<"\t" 
            <<IDs[i]<<"\t"
            <<grades[i]<<" "
            <<endl;
}

I just want know what I'm doing wrong for the program not outputting the correct letter grade for each student.

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.