446,159 Members | 956 Online
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

# Multidimensional array pointer problem

 P: n/a 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 #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 #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? Sep 26 '06 #1
6 Replies

 P: n/a c19h28o2 wrote: 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) 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? Sep 26 '06 #2

 P: n/a On 26 Sep 2006 14:16:34 -0700, "c19h28o2" Hi,I'm learning to use pointers with multidemensional arrays so pleasebear with me!Here is the problem which is from c primer plus Ch10 Programmingexcercise 1Modify the rain program in listing 10.7 so that it does thecalculations using pointersinstead of subscripts (you still have to declare and initialize thearray)The original code is: snip original code >Output: YEAR RAINFALL (inches) 2000 32.4 2001 37.9 2002 49.8 2003 44.0 2004 32.9The 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 #define MONTHS 12 // number of months in a year#define YEARS 5 // number of years of dataint 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]; You should use MONTHS as the dimension just to avoid the typo here. You need 12, not 2. > /*assign rain[][] to myRain pointer*/ myRain = rain; 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. > 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.9The 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 gdpand i'm getting a warning on the line myRain = rain; (assignment fromincompatible pointer type) myRain is declared as a const float. And because myRain was misdeclared, you are not processing the same numbers as the original code. >Any ideas what I have done wrong? Never ignore a diagnostic unless your REALLY know why you are ignoring it. Remove del for email Sep 27 '06 #3

 P: n/a jmcgill wrote: c19h28o2 wrote: 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) 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, 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..... Sep 27 '06 #4

 P: n/a jmcgill wrote: c19h28o2 wrote: 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) 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, 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! Sep 27 '06 #5

 P: n/a Barry Schwarz wrote: On 26 Sep 2006 14:16:34 -0700, "c19h28o2" #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]; You should use MONTHS as the dimension just to avoid the typo here. You need 12, not 2. /*assign rain[][] to myRain pointer*/ myRain = rain; 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. 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. And because myRain was misdeclared, you are not processing the same numbers as the original code. Any ideas what I have done wrong? Never ignore a diagnostic unless your REALLY know why you are ignoring it. Remove del for email Barry, Thats great, works as it should now. Thanks for your help. Michael Sep 27 '06 #6

 P: n/a On 27 Sep 2006 03:55:57 -0700, "c19h28o2" Barry Schwarz wrote: snip 130+ lines of my message >Barry,Thats great, works as it should now.Thanks for your help.Michael 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 Sep 28 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion.