Thread: Pointers and multidimensional arrays question

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    42

    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. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    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. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    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. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    [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. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    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. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    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. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by d2rover View Post
    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. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    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. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > pointer = &rain[0][0];
    > pointer+=month;
    Great, now try to do it properly using rain[year][month] to subscript the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Pointers to Multidimensional Arrays
    By shepz87 in forum C Programming
    Replies: 12
    Last Post: 11-15-2009, 01:33 PM
  2. pointers to multidimensional arrays
    By Luciferek in forum C++ Programming
    Replies: 37
    Last Post: 10-02-2008, 12:57 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  5. Array pointers to Multi-Dimensional Arrays
    By MethodMan in forum C Programming
    Replies: 3
    Last Post: 03-18-2003, 09:53 PM