# Thread: Pointers and multidimensional arrays question

1. ## Pointers and multidimensional arrays question

Hello,
Yes its homework but I feel im very close. I am getting junk values for "subtot" in my last for statement. Everything else seems to be working well. I've searched the board and can't seem to get my last bit of code working. Any help guys and gals? TIA

Code:
```* rain.c  finds yearly totals, yealy average, and monthly
average for several years of rainfall data*/
#include <stdio.h>
#define MONTHS 12  //number of months in a year
#define YEARS 5   //number of years of data
int main(void)
{
//initializing rainfall data for 2000-2004
float rain[YEARS][MONTHS] =
{
{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}
};
int year, month;
float subtot, total;
float *pointer;
pointer = &rain[0][0];

printf(" YEAR   RAINFALL  (inches)\n");
for (year = 0, total = 0; year < YEARS; year++)
{				//for each year, sum the rainfall for each month
subtot = 0;
for (month = 0; month < MONTHS; month++)
subtot += *pointer++;
printf("%5d %15.1f \n", 2000 + year, subtot);
total += subtot; //total for all years
}
printf("\nThe yearly average is %.1f inches.\n\n", total/YEARS);
printf("MONTHLY AVERAGES:\n\n");
printf(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  ");
printf("Nov  Dec\n");

for (month = 0; month < MONTHS; month++)
{		//for each month sum rainfall over the years
subtot = 0;
for (year = 0; year < YEARS; year++)
subtot += *pointer++;
printf("%4.1f ", subtot/YEARS);
}
printf("\n");
getchar();

return 0;
}```

2. I don't know why you need to use pointer at all while you could just use array indexing.
In your for loop, you need to update pointer to point to &rain[year][0].

3. I agree! But the point of the assignment is to use pointers (no pun intended). I tried your suggestion with no luck, Is this the code you were refering to:
Code:
```for (month = 0; month < MONTHS; month++)
{		//for each month sum rainfall over the years
subtot = 0;
pointer = &rain[year][0];
for (year = 0; year < YEARS; year++)
subtot += *pointer++;
printf("%4.1f ", subtot/YEARS);
}```
Thanks for looking at it for me!

4. [QUOTE=d2rover;978988]Hello,
Yes its homework but I feel im very close. I am getting junk values for "subtot" in my last for statement. Everything else seems to be working well. I've searched the board and can't seem to get my last bit of code working. Any help guys and gals? TIA

There's no need for pointers to calculate your subtotal...

Code:
```float ThisYear = 0;
float Total = 0;

for (year = 0; year < YEARS;year++)
{ ThisYear = 0;
for (int month = 0; month < MONTHS; month++)
ThisYear += rain[year][month];

printf("%5d %15.1f \n", 2000 + year, ThisYear);
Total += ThisYear;  }

printf("%5d %15.1f \n", 2000 + year, Total);```

5. Thanks Tater. I know that seems like the best solution but the requrement is to use pointers and step through the arrays.

Here is my current code. The problem I am having (second set of for stmts) is my pointer is stepping through the MONTHS (left to right 5) when i need it to step down through the YEARS (top to bottom) so it will add up all 5 of the "Jan" then all 5 of the "Feb" ect.

Code:
```/* rain.c  finds yearly totals, yealy average, and monthly
average for several years of rainfall data*/
#include <stdio.h>
#define MONTHS 12  //number of months in a year
#define YEARS 5   //number of years of data
int main(void)
{
//initializing rainfall data for 2000-2004
float rain[YEARS][MONTHS] =
{
{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}
};
int year, month;
float subtot, total;
float *pointer;
pointer = &rain[0][0];

printf(" YEAR   RAINFALL  (inches)\n");
for (year = 0, total = 0; year < YEARS; year++)
{				//for each year, sum the rainfall for each month
subtot = 0;
for (month = 0; month < MONTHS; month++)
subtot += *pointer++;
printf("%5d %15.1f \n", 2000 + year, subtot);
total += subtot; //total for all years
}
printf("\nThe yearly average is %.1f inches.\n\n", total/YEARS);
printf("MONTHLY AVERAGES:\n\n");
printf(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  ");
printf("Nov  Dec\n");
pointer = &rain[0][0];

for (month = 0; month < MONTHS; month++)
{		//for each month sum rainfall over the years
subtot = 0;
for (year = 0; year < YEARS; year++)
subtot += *pointer++;
printf("%4.1f ", subtot/YEARS);
}
printf("\n");
getchar();

return 0;
}```

6. Code:
`	for (year = 0, total = 0; year < YEARS; year++)`
You should probably split the above into two statements... move the total=0 out of the loop header.

If you want it to do years by month instead of months by year... exchange the inner and outer loops...
But also expect pointer management to be a pain as you will now have to calculate an offset into each row of your array, for every iteration of the loop.

7. In the second loop, using a pointer to an "array of YEARS floats" would be a start to addressing your problem.

In other words, change the type of the pointer(s) used in the second loop.

8. I have gotten it to step over 12 and pick up that value. Maybe its my lack of algebra skills?
Code:
```pointer = &rain[0][0];

for (month = 0; month < MONTHS; month++)
{		//for each month sum rainfall over the years
subtot = 0;
for (year = 0; year < YEARS; year++)
subtot += *(pointer += 12);
//printf("%4.1f ", subtot/YEARS);```

9. Originally Posted by d2rover
I have gotten it to step over 12 and pick up that value. Maybe its my lack of algebra skills?
Code:
```pointer = &rain[0][0];

for (month = 0; month < MONTHS; month++)
{		//for each month sum rainfall over the years
subtot = 0;
for (year = 0; year < YEARS; year++)
subtot += *(pointer += 12);
//printf("%4.1f ", subtot/YEARS);```
Think about the way the arrays sit in memory...

y[0]m[0]-m[1]-m[2]-m[3]-m[4]-m[5]-m[6]-m[7]-m[8]-m[9]-m[10]-m[11]y[1]m[0]-m[1]-m[2]-m[3]...

To scan all m[3] you need first to calculate the location of the year (y[x]) then m[3] as an offset from the year... it worked the other way (scanning months by year) because, by coincidence, you were scanning the array in order.

My tactic would be to build an array totals[12], scan months by year and seed the month amounts into the new array. totals[m] += month[m]... I think (without actually coding it) this might be both faster and simpler in the long run and, as a bonus, you'd have individual totals for each month.

10. Got it working. Final code:
Code:
```for (month = 0; month < MONTHS; month++)
{		//for each month sum rainfall over the years
pointer = &rain[0][0];
pointer+=month;
subtot = 0;
for (year = 0; year < YEARS; year++)
{
subtot += *pointer;
pointer += 12;
}
printf("%4.1f ", subtot/YEARS);```

11. > pointer = &rain[0][0];
> pointer+=month;
Great, now try to do it properly using rain[year][month] to subscript the array.