Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a program to calculate and show the grades for 4 students and 3 tests. I want to calculate the average for each student, the average of the test scores for each test, and the average of the student average. I also want the program to give a letter grade after calculating the average for the students. I'm not sure how to do that.

#include <iostream>
using namespace std;

void printArray(int [][3], int, int);

void main(){
//declare a 2-d aray
//each row is for a student and each column for a grade
int course [4][3];
int columns = sizeof(course[0])/sizeof(int);
int rows = (sizeof(course)/sizeof(int))/(sizeof(course[0])/sizeof(int));
int grade;

//enter the grades per student(by rows in a column)
for(int i = 0; i < columns; i++){
for(int j = 0; j < rows; j++){
cout << "\nEnter the grade for test number " << i + 1 << " for student number " << j +    1 << ":";
cin >> grade;
course[j][i] = grade;
}
}   

printArray(course, rows, columns);
//int prueba [][3] = {{1,2,3},{4,5,6},{7,8,9}};
//printArray(prueba,3,3);

system("pause");
}

void printArray( int anArray[][3], int rows, int cols){
cout << "Student\tTest 1\tTest 2\tTest 3\n";
for(int i = 0; i < rows; i++){
cout << i + 1 << "\t";
for(int j = 0; j < cols; j++){
cout << anArray[i][j] << "\t";
}
cout << endl;
}
cout << endl;
}



//the range for the letter avg are:
//A = 90 - 100
//B = 80 - 89
//C = 70 - 79
//D = 60 - 69
//F = 0 - 59

here is an example of the output of the program right now:

Student  Test1   Test2   Test3
1             100     98       97
2             100     97       98
3             100     98       99
4            100      97       99

here is an example of the output as i want it to be shown:

Student         Test1    Test2   Test3   Student Avg     Letter Grade
1               100       98      97         98           A
2               100       97      98         98           A
3               100       98      99         99           A
4               100       97      99         98           A
Test Avg.        100      97      98         98
share|improve this question
1  
Please format your primary code block so that it's easier to read. I also forgot to mention this in my answer: main() should be of type int, not void. More info about that here. –  Jamal Jul 8 '13 at 1:47
add comment

2 Answers

Your code, despite its formatting, works well against the example output. Assuming this is a homework assignment, you may not find all of this useful or even understandable at first. Since I've already started this, I'll keep the general idea the same. This code mostly serves to "replace" your multi-dimensional arrays, though I understand that you may need to keep them. Other than that, this is one way of improving your program.

  • Try not to use using namespace std. Read this for more information.

  • void main() may compile for you, but it's very wrong for main() in particular. It should be int main() instead. More information here.

  • Also try not to use system("PAUSE"). If you cannot set your IDE to pause, you may call std::cin.get() instead.

  • Although you're just trying this for a set number of students, I would recommend an std::vector of std::vectors (for adding any number of students and any number of tests). In order to use this, include <vector>. Your typedefs and function prototypes could then look like this:

    // rename these types for less typing later on
    typedef std::vector<double> NumVect;
    typedef std::vector<NumVect > VectOfNumVects;
    typedef std::pair<double, char> NumCharPair; // include <utility>
    
    VectOfNumVects getGrades(unsigned, unsigned); // new function
    NumCharPair calcTestAvg(const NumVect&); // new function
    void displayGrades(const VectOfNumVects&);
    
  • Your main() could then look like this:

    int main()
    {
        // get the number of students (4 for you)
        std::cout << "Number of students: ";
        unsigned numStudents;
        std::cin >> numStudents;
    
        // get the number of tests (3 for you)
        std::cout << "Number of tests: ";
        unsigned numTests;
        std::cin >> numTests;
    
        // get the grades and then display them
        VectOfNumVects grades = getGrades(numStudents, numTests);
        displayGrades(grades);
    }
    
  • getGrades():

    VectOfNumVects getGrades(unsigned numStudents, unsigned numTests)
    {
        VectOfNumVects students; // new "student" vector
    
        // get tests for each student
        for (unsigned i = 0; i < numStudents; ++i)
        {
             NumVect tests; // new "test" vector within "student" vector
             std::cout << "Student " << i+1;
    
             // get each test grade for current student
             for (unsigned j = 0; j < numTests; ++j)
             {
                 std::cout << " test " << j+1 << " grade: ";
                 double testGrade;
                 std::cin >> testGrade;
                 tests.push_back(testGrade);
             }
    
             students.push_back(tests);
        }
    
        return students;
    }
    
  • calcTestAvg():

    NumCharPair calcTestAvg(const NumVect &tests)
    {
        // new std::pair here for storing the average with its letter grade
        // first item is the average and second is the letter grade
        // first initialized with 0 average and "blank" grade
        NumCharPair avgPair = std::make_pair(0.0, ' ');
    
        // add up total test grades, then calculate average
        for (unsigned t = 0; t < tests.size(); ++t)
        {
            avgPair.first += tests[t];
        }
    
        avgPair.first /= tests.size();
    
        // determine the letter grade based on average
        if (avgPair.first >= 90)
            avgPair.second = 'A';
        else if (avgPair.first < 90 && avgPair.first >= 80)
           avgPair.second = 'B';
        else if (avgPair.first < 80 && avgPair.first >= 70)
            avgPair.second = 'C';
        else if (avgPair.first < 70)
            avgPair.second = 'F';
    
        return avgPair;
    }
    
  • displayGrades():

    // This won't display your desired output,
    // but you could always tweak the code however you please
    
    void displayGrades(const VectOfNumVects &students)
    {
        for (unsigned s = 0; s < students.size(); ++s)
        {
            std::cout << "Student " << s+1 << ": \n";
    
            for (unsigned t = 0; t < students[s].size(); ++t)
            {
                std::cout << "\nTest " << t+1 << ": " << students[s][t]; 
            }
    
            // get and display student average with respective letter grade
            NumCharPair result = calcTestAvg(students[s]);
            std::cout << "\n\nAverage: " << result.first;
            std::cout << "\nLetter grade: " << result.second;
        }
    }
    
share|improve this answer
1  
Reorder your first two typedefs. Define the second one in terms of the first. –  Loki Astari Jul 8 '13 at 13:28
1  
You should not define types that end in _t they are reserved by the system. See: stackoverflow.com/a/228797/14065 Personally all my types begin with an uppercase letter. All variables and functions begin with lowercase letter. This allows me to distinguish between objects and types. –  Loki Astari Jul 8 '13 at 13:31
1  
In: getGrades() you need to move the tests object inside the first loop so that it is reset after each student. Otherwise student 2 will get all the grades of student 1 and 2. Student 3 will get all the grades of student 1, 2 and 3 –  Loki Astari Jul 8 '13 at 13:34
1  
In: displayGrades() you are calling calcTestAvg() average twice. Call it once and store the value. Then print from the stored value. auto result = calcTestAvg(students[s]); –  Loki Astari Jul 8 '13 at 13:40
1  
I think you wrote too much code without explanation. The concept of this site is not to do their home work (though that can be fun and help clear your mind for other tasks). But to explain what they did wrong and provide examples of better usage. People will only learn if you make them do some work themselves. –  Loki Astari Jul 8 '13 at 14:13
show 4 more comments

I'm assuming you don't know what the previous commenter is talking about when he mentions "vectors." You're probably a beginner, right? Then I wouldn't worry about vectors for a little bit.

  • On returning an average:

What have you tried? Show us an attempt, and we can help you. But each row is a student, right? So to get a single student's average, just take the average the value in every column for that single row. If you want the student in row 0, it'd be like

int student = 0;
int sum = 0;
for(int i = 0; i < MAX_NUM_OF_COLUMNS; i++){
sum += gradeArray[student][i];
}
average = sum / MAX_NUM_OF_COLUMNS;

To convert that into a grade wouldn't be very complicated. Give it a try, and we'll help you with what you come up with.

share|improve this answer
    
I do realize that, but I want to at least get that concept out there (and I don't quite like multi-dimensional C-style arrays). Anyway, the OP can just use my method of getting the letter grade since I didn't make it too complicated. –  Jamal Jul 8 '13 at 12:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.