multi D array clear buffer?

Learning multi-D arrays. I have this menu program I'm trying to write that is returning some number I'm not expecting for case 3 and I'm not sure why. Do I need to clear a buffer? thoughts?

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
const int ROWS = 4;
const int COLS = 4;

using namespace std;

//prototypes
int Menu();
int GetTotal(int[][COLS]);
double GetAvg(int);
int GetRowTotal(int[][COLS],int);
int GetColTotal(int[][COLS],int);
int GetHighRow(int[][COLS],int);
int GetLowRow(int[][COLS],int);

int main()
{
    int choice,numtotal,rowchoice,colchoice;
    int myArray[ROWS][COLS] = {18,48,3,4,12,9,7,32,3,10,4,23,12,115,1,50};
    
    do
    {
        choice = Menu();
        
        switch(choice)
        {
             case 1: cout << "Total = ";
                     cout << GetTotal(myArray) << endl << endl;
                     break;
             case 2: cout << "Average = ";
                     numtotal = GetTotal(myArray);
                     cout << GetAvg(numtotal) << endl << endl;
                     break;
             case 3: cout << "Enter row number: ";
                     cin >> rowchoice;
                     if(rowchoice < 0 || rowchoice >= ROWS)
                     {
                          cout << "Invalid row #. Must be between 0 - ";
                          cout << ROWS << ". Please try again." << endl;
                          break;
                     }
                     else
                     {
                          cout << "Total for Row " << rowchoice << "= ";
                          cout << GetRowTotal(myArray,rowchoice) << endl;
                          break;
                     }
        }     
    cout << endl;
    }while (choice != 7); 
    
    system("PAUSE");
    return EXIT_SUCCESS;

}

int Menu()
{
    int mychoice;
    cout << "Choose a menu option: " << endl;
    cout << "1- Total of all numbers\n";
    cout << "2- Average of all numbers\n";
    cout << "3- Total of a specific row\n";
    cout << "4- Total of a specific column\n";
    cout << "5- Highest value in a specific row\n";
    cout << "6- Lowest value in a specific row\n";
    cout << "7- Exit\n\n";
    cout << "Enter your choice: ";
    cin >> mychoice;
    return mychoice;
}


int GetTotal(int myArray[][COLS])
{
    int total = 0;
    for(int i = 0; i < ROWS; i++)
            for(int j = 0; j < ROWS; j++)
            total += myArray[i][j];
    return total;
}


double GetAvg(int totalnum)
{
     double avg;
     avg = totalnum / (ROWS * COLS);
     return avg;
}


int GetRowTotal(int myArray[][COLS],int row)
{
    int rowtotal;
    for(int i = 0; i < ROWS; i++)
        rowtotal += myArray[row][i];
    return rowtotal;
}
You forgot to initialize rowtotal in GetRowTotal.
oh duh. thanks Peter87!

But... I'm getting 40, when I'm expecting 60. Shouldn't it read rows left to right? so my 2nd row is {12,9,7,32}, but I'm getting 40. Am I understanding the direction that it reads incorrectly?
Array indices starts at zero so if you input 2 (rowchoice = 2) you will get the sum of the third row.
Last edited on
oh right! thank you again!
Topic archived. No new replies allowed.