Simple allocate and loop-through bug,
-
Anyone care to take a crack at this one? It's from a well-known company but no mentions of who till it's solved (in order to stop Googling)
1 #include
2 #define FIRST_YEAR 1901
3 #define LAST_YEAR 2006
4 #define NSTATIONS 100
5 extern double
6 temperatures[LAST_YEAR+1-FIRST_YEAR][NSTATIONS];
7
8 void print_temperatures()
9 {
10 double total;
11
12 for( int i = LAST_YEAR; i <= FIRST_YEAR; i-- )
13 {
14 total = 0.0;
15 for( int j = 0; j < NSTATIONS; j++ )
16 total += temperatures[i-FIRST_YEAR][j];
17 printf( "Ave. temp. for year %d is %g\n",
18 i, total / NSTATIONS );
19 }
20 }cheers, Chris Maunder
CodeProject.com : C++ MVP
-
Anyone care to take a crack at this one? It's from a well-known company but no mentions of who till it's solved (in order to stop Googling)
1 #include
2 #define FIRST_YEAR 1901
3 #define LAST_YEAR 2006
4 #define NSTATIONS 100
5 extern double
6 temperatures[LAST_YEAR+1-FIRST_YEAR][NSTATIONS];
7
8 void print_temperatures()
9 {
10 double total;
11
12 for( int i = LAST_YEAR; i <= FIRST_YEAR; i-- )
13 {
14 total = 0.0;
15 for( int j = 0; j < NSTATIONS; j++ )
16 total += temperatures[i-FIRST_YEAR][j];
17 printf( "Ave. temp. for year %d is %g\n",
18 i, total / NSTATIONS );
19 }
20 }cheers, Chris Maunder
CodeProject.com : C++ MVP
<= -> >= (You'll be looping through every year that's not in the interval LAST_YEAR ... FIRST_YEAR, which is probably not what you want) Nevermind, it'll never loop. :-O
-- A Stern Warning of Things to Come
-
Anyone care to take a crack at this one? It's from a well-known company but no mentions of who till it's solved (in order to stop Googling)
1 #include
2 #define FIRST_YEAR 1901
3 #define LAST_YEAR 2006
4 #define NSTATIONS 100
5 extern double
6 temperatures[LAST_YEAR+1-FIRST_YEAR][NSTATIONS];
7
8 void print_temperatures()
9 {
10 double total;
11
12 for( int i = LAST_YEAR; i <= FIRST_YEAR; i-- )
13 {
14 total = 0.0;
15 for( int j = 0; j < NSTATIONS; j++ )
16 total += temperatures[i-FIRST_YEAR][j];
17 printf( "Ave. temp. for year %d is %g\n",
18 i, total / NSTATIONS );
19 }
20 }cheers, Chris Maunder
CodeProject.com : C++ MVP
i >= FIRST_YEAR
but may be a typo.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
-
i >= FIRST_YEAR
but may be a typo.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
It took me a disturbingly long time to pick that one.
cheers, Chris Maunder
CodeProject.com : C++ MVP
-
It took me a disturbingly long time to pick that one.
cheers, Chris Maunder
CodeProject.com : C++ MVP
Chris Maunder wrote:
It took me a disturbingly long time to pick that one.
I spent the whole time looking for a fence-post problem. It wasn't until I googled "[LAST_YEAR+1-FIRST_YEAR][NSTATIONS]" that I found the answer :-O
-- Russell Morris Morbo: "WINDMILLS DO NOT WORK THAT WAY!"