# Multidimensional array pointer problem

Discussion in 'C Programming' started by c19h28o2, Sep 26, 2006.

1. ### c19h28o2Guest

Hi,

I'm learning to use pointers with multidemensional arrays so please
bear with me!

Here is the problem which is from c primer plus Ch10 Programming
excercise 1

Modify the rain program in listing 10.7 so that it does the
calculations using pointers
instead of subscripts (you still have to declare and initialize the
array)

The original code is:

/* rain.c -- finds yearly totals, yearly 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
const 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;

printf(" YEAR RAINFALL (inches)\n");
for (year = 0, total = 0; year < YEARS; year++)
{ // for each year, sum rainfall for each month
for (month = 0, subtot = 0; month < MONTHS; month++)
subtot += rain[year][month];
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 years
for (year = 0, subtot =0; year < YEARS; year++)
subtot += rain[year][month];
printf("%4.1f ", subtot/YEARS);
}
printf("\n");

return 0;
}

Output:

YEAR RAINFALL (inches)
2000 32.4
2001 37.9
2002 49.8
2003 44.0
2004 32.9

The yearly average is 39.4 inches.

MONTHLY AVERAGES:

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
7.3 7.3 4.9 3.0 2.3 0.6 1.2 0.3 0.5 1.7 3.6 6.7

------------------------------------

As I understand the problem I need to change the rain[years][months]
calculations to use a pointer....

So I changed the first calculation to this.....

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
const 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;

/*my pointer to rain[YEARS][MONTHS]*/
const float (*myRain)[2];
/*assign rain[][] to myRain pointer*/
myRain = rain;

printf(" YEAR RAINFALL (inches)\n");
for (year = 0, total = 0; year < YEARS; year++)
{ // for each year, sum rainfall for each month
for (month = 0, subtot = 0; month < MONTHS; month++)
/*pointer to rain*/
subtot += *(*(myRain + year) + month);
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 years
for (year = 0, subtot =0; year < YEARS; year++)
subtot += rain[year][month];
printf("%4.1f ", subtot/YEARS);
}
printf("\n");

return 0;
}

output:

YEAR RAINFALL (inches)
2000 32.4
2001 40.5
2002 36.0
2003 35.2
2004 40.9

The yearly average is 37.0 inches.

MONTHLY AVERAGES:

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
7.3 7.3 4.9 3.0 2.3 0.6 1.2 0.3 0.5 1.7 3.6 6.7

------------------------

The output is slightly different, i'm compiling this on a mac with gdp
and i'm getting a warning on the line myRain = rain; (assignment from
incompatible pointer type) myRain is declared as a const float.

Any ideas what I have done wrong?

c19h28o2, Sep 26, 2006

2. ### jmcgillGuest

Change the two lines:
/* subtot += rain[year][month]; */
subtot += *(*(rain+year)+month);

Do you also want to initialize the array in pointer-style?
Am I missing something about the question?

jmcgill, Sep 26, 2006

3. ### Barry SchwarzGuest

snip original code
You should use MONTHS as the dimension just to avoid the typo here.
You need 12, not 2.
They way you coded it, this had to generate a diagnostic. myRain is a
pointer to a const array of two float. In this context, rain
evaluates to a pointer to a const array of twelve float. The two
pointer types are incompatible.
And because myRain was misdeclared, you are not processing the same
numbers as the original code.
Never ignore a diagnostic unless your REALLY know why you are ignoring
it.

Remove del for email

Barry Schwarz, Sep 27, 2006
4. ### c19h28o2Guest

jmcgill,

Thanks for the input. My original thought was to code it like your
suggestion however the chapter is about learning to use pointers as
multidimensional arrays so I take it they want you to learn how to
declare and use pointers in that way.....

c19h28o2, Sep 27, 2006
5. ### c19h28o2Guest

jmcgill,

Thanks for the input. My original thought was to code it like your
suggestion however the chapter is about learning to use pointers as
multidimensional arrays so I take it they want you to learn how to
declare and use pointers in that way..... Hence the approach I have
taken!

c19h28o2, Sep 27, 2006
6. ### c19h28o2Guest

Barry,

Thats great, works as it should now.

Michael

c19h28o2, Sep 27, 2006
7. ### Barry SchwarzGuest

snip 130+ lines of my message
You are welcome but you need to trim the material you quote to what is
germane. It doesn't take 130 lines to say thanks.

Remove del for email

Barry Schwarz, Sep 28, 2006