Thread: How to find average?

  1. #16
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You either need to turn up your warnings or actually listen to them:

    Code:
    beach.c:41:25: warning: format specifies type 'int' but the argument has type
          'double' [-Wformat]
      ...printf("%0d\n", mean_level);
                 ~~~     ^~~~~~~~~~
                 %0f
    Also, you need to listen to the advice you've been given about indentation. Here's what your code *should* look like:

    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main (void)
    {
        int lake_ID, beach_number, number_of_samples, i;
        double number_of_organisms, mean_level=0, sum=0;
    
        FILE *in;
    
        in =fopen("july15", "r");
    
        while (fscanf (in, "%d" "%d" "%d", &lake_ID, &beach_number,&number_of_samples) != EOF)
        {
            {
                for (i=0; i<number_of_samples; i++)
                    fscanf (in, "%lf", &number_of_organisms);
                sum = sum + number_of_organisms;
                mean_level = sum/number_of_samples;
            }
    
            if (number_of_samples < 3)
                printf ("%d Insufficient Data\n", beach_number );
            else if (mean_level > 50)
                printf ("%d closed\n", beach_number);
            else if (mean_level < 50)
                printf ("open\n");
            else if (beach_number<=0)
                printf("%0d\n", mean_level);
    
            if (lake_ID == 1 )
                printf ("Ontario:\n");
            else if (lake_ID == 2)
                printf ("Erie:\n");
            else if (lake_ID == 3)
                printf ("Huron:\n");
            else if (lake_ID == 4)
                printf ("Muskoka: \n");
            else if (lake_ID == 5)
                printf ("Simcone:\n");
    
            if (beach_number == 100)
                printf ("Kew Beach:\n");
            else if (beach_number ==101)
                printf ("Sunnyside Beach:\n");
            else if (beach_number ==103)
                printf ("Sandbanks:\n");
            else if (beach_number ==201)
                printf ("Port Dover:\n");
            else if (beach_number ==202)
                printf ("Port Burwell:\n");
            else if (beach_number ==203)
                printf ("Crystal Beach:\n");
            else if (beach_number ==301)
                printf ("Goderich:\n");
            else if (beach_number ==302)
                printf ("Sauble Beach:\n");
            else if (beach_number ==303)
                printf ("Kincardine:\n");
            else if (beach_number ==401)
                printf ("Muskoka beach:\n");
            else if (beach_number ==501)
                printf ("Sibbald Point:\n");
        }
        fclose(in);
        return(0);
    }

  2. #17
    Registered User
    Join Date
    Feb 2015
    Posts
    14
    did that but still same thing, mean-level is not showing

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How often is beach_number equal to or less than zero?


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find the average of random numbers:
    By jocdrew21 in forum C Programming
    Replies: 9
    Last Post: 08-18-2013, 03:29 PM
  2. program to find the range and average
    By narendrav in forum C Programming
    Replies: 7
    Last Post: 06-10-2011, 12:20 AM
  3. find & print the average of the odd numbers
    By zozo995 in forum C++ Programming
    Replies: 1
    Last Post: 10-27-2010, 10:31 PM
  4. function to find average
    By nynicue in forum C Programming
    Replies: 4
    Last Post: 03-24-2009, 05:30 PM
  5. How can I find the average in the ARRAYS?
    By Nate2430 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2001, 11:21 AM