Thread: arrays help

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    62

    arrays help

    printf("%f",avg);
    Last edited by joker_tony; 10-04-2008 at 10:53 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So the first time through the loop, you add tarray[0] to total. But what is total?

    Edit: Oh, and some other things the compiler found:
    Code:
    temp.c:24: warning: format ‘%.1f’ expects type ‘double’, but argument 2 has type ‘int’
    temp.c:25: warning: format ‘%.1f’ expects type ‘double’, but argument 2 has type ‘int’
    temp.c:31: warning: control reaches end of non-void function
    temp.c: At top level:
    temp.c:32: warning: unused parameter ‘num’
    Last edited by tabstop; 10-04-2008 at 06:50 PM.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I added comments. Read 'em
    Code:
    float average(int tarray[], int num);
    int main()
    {
     int day,i;
     float avgtemp,avghumid;
     int hum[31], temp[31];
     
     printf("How many days =>");
     scanf("%i",&day);
     printf("\n\n");
     if (0<day&&day<=31)
     {
         
        for (i=0; i<day; i++)
        {
          printf("Enter temperature and humidity for day %i =>",i+1);
          fflush(stdin);// See http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1052863818&id=1043284351
          scanf("%d %d",&temp[i], &hum[i]);
        }  
              
        avgtemp  = average(temp,day);
        avghumid = average(hum,day);
    
        printf("\t\n\nAverage temperature = %.1f degC",avgtemp); // f was right here but type was int
        printf("\t\nAverage Humidity = %.1f %%",avghumid);       // same
      }
      else
      {      
             printf("wrong input");
      }
       getch();// getch isn't a great way of stopping the conosle. 
       return 0;// Don't forget to return something either. 
    }
    float average(int tarray[], int num)// changed return type to float
    {     
      int i; // got rid of days
      float avg; // changed type
      int total=0;// assigned an initial value
      avg=0;
      for (i=0; i<num; i++) // was i<days, but num is passed into the function
      {
          total += tarray[i]; // can use the += operator. 
      } 
        
      avg=total/num;  // changed days to num
      
      // Might want to make this function return a float or double as division
      // may cause floating point arithmitic. 
      return(avg);
          
    }

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I guess the OP deleted his code from the first post.

    twomers, you said "as division may cause floating point arithmetic". No it don't. It either does it "integer" or "float", but not unpredictably so. avg = total / num will always yield an integer because both arguments of the division operation are integers.

    But I think I know what you meant to say.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM