Array output

This is a discussion on Array output within the C Programming forums, part of the General Programming Boards category; on this program i don't get any error but after compiling it doesn't show the numeric answers or names. here ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    9

    Question Array output

    on this program i don't get any error but after compiling it doesn't
    show the numeric answers or names. here is what i have:
    Code:
    #include <stdio.h>			
    #define max_current 0.0
    #define max_previous 0.0
    #define max_month 12
    
    int main ()
    {
    	char *month []= {"Jan","Feb","Mar","Apr", "May",
    							"Jun","Jul","Aug","Sep", "Oct",
    							"Nov", "Dec"};
    	double current_rain[12]= {3.11,2.62,9.20, 1.48, 5.42, 7.14, 
    							3.64, 4.57,4.23, 1.79, 0.71, 2.43};
    	double previous_rain[12]= { 2.92, 1.71, 5.05, 4.16, 4.49,3.02,
    								 1.99,2.46, 5.30, 3.34, 5.76, 4.68};
    	double total_current = 0.0;
    	double total_previous = 0.0;
    	double ave_current = 0.0;
    	double ave_previous = 0.0;
    	
    	int i = 0;
    
    	printf("		Table for monthly rainfall		");
    	printf("\n");
    
    	/*for current year				*/
    	printf("         ");
    	for(i=0; i<max_month; i++)
    	{
    		printf(month[i]);printf("   ");
    	}
    		printf("\n");
    	
    	printf("This yr  ");
    	for(i=0; i<max_month; i++)
    	{
    		printf("%.2f",current_rain[i]);
    		printf("  ");
    	}
    		printf("\n");
    	
    	printf("Last yr  ");
    	for(i=0; i<max_month; i++)
    	{
    		printf("%.2f",previous_rain[i]);printf("  ");
    	}
    
    	
    		printf("\n");
    	
    
    	for(i=0; i>max_current; i++)
    	{
    		printf("		current_rain[i]   ");
    		if(current_rain[i]>max_current)
    		{
    			 current_rain[i] = max_current;
    		
    		}
    		total_current = current_rain[i] + total_current;
    	}
    
    	ave_current= total_current / 12.0;
    	
    	/* for previuos year*/
    
    	printf("\n");
    	
    	for(i=0; i>max_previous; i++)
    	{
    		printf("		previous_rain[i]   ");
    		if(previous_rain[i]>max_previous)
    		{
    			 previous_rain[i] =max_previous;
    			
    		}
    		total_previous = previous_rain[i] + total_previous;
    	}
    	ave_previous= total_previous / 12.0;
    
    
    
    	printf("\n");
    	printf("The rainfall for this year: %.2f",total_current);
    	printf("\n");
    	printf("The rainfall for previous year: %.2f",total_previous);
    	printf("\n");
    	printf("Average monthly rainfall for this year: %.2f", ave_current);
    	printf("\n");
    	printf("Average monthly rainfall for last year: %.2f", ave_previous);
    	printf("\n");
    	printf("Month of highest rainfall for this year: %s", month);
    	printf("\n");
    	printf("Month of highest rainfall for last year: %s", month);
    	printf("\n");
    
    	return 0;
    }
    the output lines at the end look like:
    ....................: 0.0
    .....................:0.0
    ......................:0.0
    ........................:0.0
    .....................: <bB
    .....................: <bB
    where the .........is whatever is inside the printf.
    is my program not reading something?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    Code:
    #define max_current 0.0
    #define max_previous 0.0
    That don't look right...

    gg

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    You need to use %0.2lf for the double data type in your printf statement I believe.

    month requires an index as well since it's a 2D array.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    ronin:
    for double, %f is used in printf, and %lf in scanf.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Try this version:
    Code:
    #include <stdio.h>			
    #define max_month 12
    
    int main ()
    {
      char *month []= {"Jan","Feb","Mar","Apr", "May",
                       "Jun","Jul","Aug","Sep", "Oct",
                       "Nov", "Dec"};
      double current_rain[12]= {3.11,2.62,9.20, 1.48, 5.42, 7.14, 
                                3.64, 4.57,4.23, 1.79, 0.71, 2.43};
      double previous_rain[12]= { 2.92, 1.71, 5.05, 4.16, 4.49,3.02,
                                  1.99,2.46, 5.30, 3.34, 5.76, 4.68};
      double total_current = 0.0;
      double total_previous = 0.0;
      double max_current = 0.0;
      int max_month_current = 0;
      double max_previous = 0.0;
      int max_month_previous = 0;
      double ave_current = 0.0;
      double ave_previous = 0.0;
    
      int i = 0;
    
      printf("		Table for monthly rainfall		");
      printf("\n");
    
      /*for current year				*/
      printf("         ");
      for(i=0; i<max_month; i++)
      {
        printf("%s   ",month[i]);
      }
      printf("\n");
    
      printf("This yr  ");
      for(i=0; i<max_month; i++)
      {
        printf("%.2f  ",current_rain[i]);
      }
      printf("\n");
    
      printf("Last yr  ");
      for(i=0; i<max_month; i++)
      {
        printf("%.2f  ",previous_rain[i]);
      }
      printf("\n");
    	
      /* for current year */
      for(i=0; i<max_month; i++)
      {
        if(current_rain[i]>max_current)
        {
          max_current = current_rain[i];
          max_month_current = i;
        }
        total_current = current_rain[i] + total_current;
      }
      ave_current= total_current / (double)max_month;
    	
      /* for previuos year*/
      for(i=0; i<max_month; i++)
      {
        if(previous_rain[i]>max_previous)
        {
          max_previous = previous_rain[i];
          max_month_previous = i;
        }
        total_previous = previous_rain[i] + total_previous;
      }
      ave_previous= total_previous / (double)max_month;
    
    
      /* print results */
      printf("\n\n");
      printf("The rainfall for this year: %.2f\n",total_current);
      printf("The rainfall for previous year: %.2f\n",total_previous);
      printf("Average monthly rainfall for this year: %.2f\n", ave_current);
      printf("Average monthly rainfall for last year: %.2f\n", ave_previous);
      printf("Month of highest rainfall for this year: %s\n", month[max_month_current]);
      printf("Month of highest rainfall for last year: %s\n", month[max_month_previous]);
    
      return 0;
    }
    The problems were in the maximum search parts of the code.
    DavT
    -----------------------------------------------

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    9
    thanks, that works. so, what u are saying is that the temporary values have to be stored in a variable and not declared in #define, right?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output an array in a textbox
    By Diablo02 in forum C# Programming
    Replies: 5
    Last Post: 10-18-2007, 03:56 AM
  2. Array output strange
    By swgh in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2006, 05:58 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Sorting array output problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 12:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21