Thread: rain

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    29

    rain

    noob here.Why is it I get big number for the three %2.1fmm instead of my expected 51.4mm etc..?

    Code:
    double rainTotal(double rainfall[12]) 
            {
    	  int i;
    	  double total = rainfall[0];
    	  for (i = 1 ; i < 12 ; i++)
    	  {
    	   total += rainfall[i];   
      	   return total;
    	  }	      
             }
        double lowestMonth(double rainfall[1])
            {
    	  return rainfall[1];
            }
        double highestMonth(double rainfall[9])   
            {
    	  return rainfall[9];
             }
    ...
    
    int main()
    {
        double rainfall[12] = {'48.5' , '47.2' , '52.4' , '58.7' , '57.6' , '50.1' , '47.6' , '50.3' , '59.4' , '67.1' , '60.0' , '58.9'};
    .......
    	 
    printf("The average monthly rainfall in Melbourne is %2.1fmm,\n" , total);
    printf("The lowest rainfall of %2.1fmm falls in %s,\n" , lowest , month[1]);
    printf("The highest rainfall of %2.1fmm falls in %s,\n" , highest , month[9]);
    }

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    Quote Originally Posted by Jasper View Post
    Code:
        double rainfall[12] = {'48.5' , '47.2' , '52.4' , '58.7' , '57.6' , '50.1' , '47.6' , '50.3' , '59.4' , '67.1' , '60.0' , '58.9'};
    here you are initializing array doubles with multi-characters rather than float-pointing constants.

    try something like this:
    Code:
        double rainfall[12] = {48.5 , 47.2 , 52.4 , 58.7 , 57.6 , 50.1 , 47.6 , 50.3 , 59.4 , 67.1 , 60.0 , 58.9};
    good point is to use -Wall -W options by compilation.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    29

    num sum

    Thkx. now its better. but why the num is not sum of my array elements? error wif my function?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Jasper View Post
    noob here.Why is it I get big number for the three %2.1fmm instead of my expected 51.4mm etc..?
    You're using the wrong syntax. Take a look at this example:

    Code:
    double lowestMonth( double rainfall[ ], size_t array_length )
    {
        size_t
            index = 0;
        double
            lowest;
        if( array_length < 1 )
            return 0.0;
        for( lowest = rainfall[ 0 ]; index < array_length; ++index )
        {
            if( rainfall[ index ] < lowest )
                lowest = rainfall[ index ];
        }
        return lowest;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a rain effect...
    By Raigne in forum Game Programming
    Replies: 11
    Last Post: 12-04-2007, 05:30 PM
  2. Weather in 2D tile engines
    By VirtualAce in forum Game Programming
    Replies: 7
    Last Post: 09-26-2006, 03:05 PM
  3. I need a rain coat
    By quzah in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 11-27-2004, 08:19 AM
  4. Dang Rain Storm
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 02-27-2004, 09:39 PM
  5. Rain - extreme cooling - extreme hunger
    By iain in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-13-2002, 10:00 PM