Stumped...

Going blind trying to find the logic error, so if anyone wants to spare a second and see if it jumps out to them...

This is a stab at one of the fun exercises posted at http://www.cplusplus.com/forum/articles/12974/ .

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
#include <iostream>

using namespace std;

int main (int argc, char *argv[]) {

  int foo[10][2];
  int i;

  for (i = 0; i < 10; i++) {
    
    foo[i][0] = i;

    cout << "How many pancakes did person " << i << " eat? ";
    cin >> foo[i][1];
    cout << endl;

  }

  int temp[1][1];

  temp[0][0] = foo[0][0];
  temp[0][1] = foo[0][1];

  for (i = 0; i < 10; i++) {

    if (foo[i][1] > temp[0][1]) {

      temp[0][0] = foo[i][0];
      temp[0][1] = foo[i][1];

    } 

    // debug
    cout << "temp={" << temp[0][0] << ", " << temp[0][1] << "}" << endl; 
  
  }

  cout << "Person " << temp[0][0]
       << " ate " << temp[0][1] 
       << " pancakes - the most of anyone!" << endl;

  return 0;

}


Output looks like this:

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
lubyanka:my.projects exs$ ./a.out 
How many pancakes did person 0 eat? 2

How many pancakes did person 1 eat? 4

How many pancakes did person 2 eat? 6

How many pancakes did person 3 eat? 8

How many pancakes did person 4 eat? 10

How many pancakes did person 5 eat? 12

How many pancakes did person 6 eat? 14

How many pancakes did person 7 eat? 16

How many pancakes did person 8 eat? 18

How many pancakes did person 9 eat? 20

temp={0, 2}
temp={3, 8}
temp={9, 20}
Person 9 ate 21 pancakes - the most of anyone!


I can't see why the result is being incremented before dumping to cout...

Thanks!
This may help:
1
2
3
4
  int temp[1][1];

  temp[0][0] = foo[0][0];
  temp[0][1] = foo[0][1];


You have defined temp multi-demisional array with dimensions of 1, 1 and yet you use dimensions of 1, 2. Try changing your temp definition to int temp[1][2];
This is what 18 years since your last C class does to you...

Thanks!
Topic archived. No new replies allowed.