How do i add up my array values and display it along side with my other values

Viewed 38

I am trying to add up all the values that have been stored into array b and have it display under the "total column" and don't know how to only have the scores add together.

#include <iostream>
#include <iomanip>
using namespace std;


int main()
{
    int array[5][4];
    int sum = 0;
    cout<<"Enter grades for 4 exams for the 5 students \n";
    for(int i=0;i<5;i++)
    {
        for(int b=1;b<=4;b++)
        {
            cout <<setw(8)<< "enter student "<< i << "'s grade for exam " << b << '\n';
            cin >> array[i][b];
        }
    }

    cout <<"ID"<<setw(11)<<"score 1"<<setw(11)<<"score 2"<<setw(11)<<"score 3"<<setw(11)<<"score 4"<<setw(11)<<"total"<<setw(11)<<"letter"<<endl;
    cout <<"--------------------------------------------------------------------------------------------------------------\n";
    for(int i=0;i<5;i++)
    {
        cout << i<< "  ";
        for(int b=1;b<=4;b++)
        {   
            sum = sum + array[b];
            cout <<setw(10)<<array[i][b]<<sum;
            
        }
        cout <<'\n';
    }
    cout <<"--------------------------------------------------------------------------------------------------------------\n";
    
    return 0;
}

To be more specific around line 28


for(int i=0;i<5;i++)
    {
        cout << i<< "  ";
        for(int b=1;b<=4;b++)
        {   
            sum = sum + array[b];
            cout <<setw(10)<<array[i][b]<<sum;
            
        }
        cout <<'\n';
1 Answers

Arrays indexes start at 0, not 1. You are correctly looping through your array's 1st dimension, but not its 2nd dimension. You need to change the inner for loops from for(int b=1;b<=4;b++) to for(int b=0;b<4;b++)

Also, to handle the total column, you simply need to reset sum to 0 on each iteration of the 1st dimension, and then print the result after the iteration of the 2nd dimension.

Try this:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int array[5][4];

    cout << "Enter grades for 4 exams for the 5 students \n";
    for(int i = 0; i < 5; i++)
    {
        for(int b = 0; b < 4; b++)
        {
            cout << "Enter student " << i+1 << "'s grade for exam " << b+1 << '\n';
            cin >> array[i][b];
        }
    }

    cout << "ID" << setw(11) << "score 1" << setw(11) << "score 2" << setw(11) << "score 3" << setw(11) << "score 4" << setw(11) << "total" << setw(11) << "letter" << endl;
    cout << "--------------------------------------------------------------------------------------------------------------\n";
    for(int i = 0; i < 5; i++)
    {
        cout << setw(2) << left << i+1 << right;
        int sum = 0;
        for(int b = 0; b < 4; b++)
        {   
            sum = sum + array[i][b];
            cout << setw(11) << array[i][b];
            
        }
        cout << setw(11) << sum << '\n';
    }
    cout << "--------------------------------------------------------------------------------------------------------------\n";
    
    return 0;
}

Online Demo

Related