HELP!!

How do I fix this code so that when a user inputs the column they want to find the sum of they find the sum of only that column?

For example, if the user enters 1; the sum of column 1 only appears.

1
2
3
4
5
6
7
8
9
   cout << "Enter column index 0-3:" << endl;
        cin >> index;
        s=0;
        for(i=0;i<4;i++)
        {
            s=s+a[i][index];
        }
        cout<<"The sum of column "<< index <<" numbers is "<< s;
        cout<<endl;
closed account (SECMoG1T)
i think you are doing it right, are you getting any errors?

maybe this
 
cout<<"The sum of column "<< index+1 <<" numbers is "<< s;
Last edited on
s is coming out to be a crazy number.
closed account (SECMoG1T)
just post the whole code, so we can check.
It's a whole menu, so input choice 3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<iostream>
using namespace std;
int main()
{
    int a[3][4];
    int input;
    int i,j,index,s=0,sum=0;
    srand(((unsigned)time(0)));
    do{
    cout<<"Please select 1-7:\n1. Create 2d array \n2. Find row sum \n3. Find col sum \n4. Find total sum \n5. Find row with largest sum \n6. Find col with largest sum \n7. Quit" << endl;
    cin>> input;
    for(i=0;i<3;i++)
        for(j=0;j<4;j++)
            a[i][j] = 1 + rand() % 10;
    
    if (input==1){
        for(i=0;i<3;i++)
        {for(j=0;j<4;j++)
            cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }    if (input==2){
        cout << "Enter row index 0-2:" << endl;
        cin >> index;
        s=0;
        for(j=0;j<4;j++)
        {
            s=s+a[index][j];
        }
        cout<<"The sum of row "<<index <<" numbers is "<< s;
        cout<<endl;
        for(i=0;i<3;i++)
        {for(j=0;j<4;j++)
            cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }
    cout<<endl;
    if (input==3){
        cout << "Enter column index 0-3:" << endl;
        cin >> index;
        s=0;
        for(i=0;i<4;i++)
        {
            s=s+a[i][index];
        }
        cout<<"The sum of column "<< index <<" numbers is "<< s;
        cout<<endl;
        for(i=0;i<3;i++)
        {for(j=0;j<4;j++)
            cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }
    if (input==4){
    cout<<endl;
    
    for(i=0;i<3;i++)
    for(j=0;j<4;j++)
        sum=sum+a[i][j];
        cout<<"Total Sum is "<<sum << endl;
        for(i=0;i<3;i++)
        {for(j=0;j<4;j++)
            cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }
    if (input==5){
    int largestSum = 0;
        int largestRow= -1;
    for (int i = 0; i<3; i++)
    {
        int sum = 0;
        for (int j = 0; j<4; j++)
        {
            sum += a[i][j];
        }

        if (sum > largestSum)
        {
            largestSum = sum;
            largestRow = i;
        }
    }
    cout << "Row " <<largestRow <<" has the largest sum " << largestSum<< '\n';
        for(i=0;i<3;i++)
        {for(j=0;j<4;j++)
            cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }
    if (input==6){
    int largestColumn = 0;
        int largeColumn =0;
    for (int i = 0; i<4; i++)
    {
        int sum = 0;

        for (int j = 0; j<3; j++)
        {
            sum += a[j][i];
           
        }
        
        if (sum > largestColumn)
        {
            largestColumn = sum;
            largeColumn = i;
        }
    }
        cout << "Column " <<largeColumn <<" has the largest sum " << largestColumn<< '\n';
        for(i=0;i<3;i++)
        {for(j=0;j<4;j++)
            cout<<a[i][j]<<" ";
            cout<<endl;
        }
    }
    } while(input!=7); cout <<"Good by!" << endl;
    return 0;
} 
Hello cash,

Duplicate http://www.cplusplus.com/forum/beginner/224270/

You should stick to the same topic. It keeps the confusion down and all the answers in one place.

You have the code correct, but you have your subscripts backwards. This is the same code, but this may make it easier to understand:

1
2
3
4
5
6
7
8
9
10
11
12
13
   cout << "Enter row index 0-3:" << endl;
        cin >> row;
        s=0;
        for (col=0; col < 4; col++)
        {
            s=s+a[row][col];
            // or
            // s += a[row][col];
        }

        // you may want to revise the next statement to better explain what the output is.
        cout<<"The sum of column "<< row <<" numbers is "<< s;
        cout<<endl;


Unless your idea is to add together only part of the columns.

As you can see row never changes its value which is what you want to add each column.

Hope that helps,

Andy
Hello cash,

Now that I understand what you are tying to do you can disregard some of the above message, bur compare the two to see the difference:

1
2
3
4
5
6
7
8
9
   cout << "Enter column index 0-3:" << endl;
        cin >> col;
        s = 0;
        for (row = 0; row < 4; row++)
        {
            s=s+a[row][col];
        }
        cout<<"The sum of column "<< col <<" numbers is "<< s;
        cout<<endl;

where in this case "row" changes, but "col" does not.

Sorry for the confusion. Since the subject line is the same for two threads I was still focused on the other thread.

Hope that helps,

Andy
closed account (SECMoG1T)
Hey Andy, his problem comes from out of range indexing.

CASH: in memory this is how your array is stored

0 2 3 4
5 6 7 8
9 3 5 2


so there are 3 rows and 4 columns.

1
2
3
4
5
6
7
8
9
//for(i=0;i<4;i++) ///you should use the outer not the inner loop
//        {               ///when i==3 you are out of range
//            s=s+a[i][index];
//        }

for(int i=0; i<3; i++)///the correct loop, "each col contains 3 elements"
{
   s+=a[i][index];
}


also you create a space between line 8-9 and move line 12 - 14 there.
Last edited on
@ Yolanda,

Thanks for the catch. I had the other "row" sum in mind when I started and did not see everything until I had made two posts. Then I had time to really look at the whole code.

Andy
closed account (SECMoG1T)
@Andy, i agree it can be confusing the first time you look at it
Topic archived. No new replies allowed.