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? 6 3942
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?
On 26 Sep 2006 14:16:34 -0700, "c19h28o2"
<mi************@barclays.co.ukwrote:
>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:
snip original code
> 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];
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
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.....
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!
Barry Schwarz wrote:
On 26 Sep 2006 14:16:34 -0700, "c19h28o2"
<mi************@barclays.co.ukwrote:
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:
snip original code
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];
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
On 27 Sep 2006 03:55:57 -0700, "c19h28o2"
<mi************@barclays.co.ukwrote:
> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: shane |
last post by:
Ive searched a fair bit for the answer, but nothing has come up that
matches what i want to do. I'm having an issue with passing and
assigning pointers to multidimensional arrays.
The code:
...
|
by: TLOlczyk |
last post by:
I have a brain cramp and I need some help.
I have a chunk of code below which demonstrates
a problem I have with multidimensional arrays.
I want to keep it simple but something specific is...
|
by: Charles Banas |
last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to
get some opinions and comments on it, hopefully so i can gain some
sort of insight as to why this works.
at the top of the...
|
by: Kobu |
last post by:
I've read the FAQ and several posts on multidimensional arrays and how
their names decay to pointer to arrays (not pointer to pointers).
If this is so, why does the following code fragment...
|
by: Michel Rouzic |
last post by:
Hi,
I've recently met issues with my program which can only be explained
by heap corruption, so I've tried debugging my program with Valgrind,
and here's what I get with the following...
|
by: nitinm |
last post by:
hi
I want to make a program whose requirement are as following:
1) it has to create an NxN matrix after reading input (i.e. N) from a
file in the main() itself.
2) it has to send the array as...
|
by: ...vagrahb |
last post by:
I am having accessing individual rows from a multidimensional array
pass to a function as reference
CODE:
function Declaration
int Part_Buffer(char (*buffer),int Low, int High)
|
by: Szabolcs Borsanyi |
last post by:
Dear All,
there have been several threads on multidimensional arrays and pointers
to arrays, there is still something I could not fully understand.
(My point here I have raised already, but...
|
by: Slain |
last post by:
I need to convert a an array to a multidimensional one. Since I need
to wrok with existing code, I need to modify a declaration which looks
like this
In the .h file
int *x;
in a initialize...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |