Thread: How to find average?

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    14

    Please help

    Write a C program to
    (i) calculate the mean level of E. Coli bacteria for a series of lake beaches in Ontario,
    (ii) make a decision to open or close the beach, and
    (iii) produce a report for all the lakes and beaches contained in the data file.

    * For each beach, there is a line of data in the file with the following fields separated by
    blanks - lake ID (integer), beach number (integer), number of samplings (integer) and one real
    number (double) per sampling representing the number of organisms in 100ml of water.

    * Your program should process as many data lines as there are present.

    * For each beach, calculate the mean level of bacteria (average).

    * If the mean level is above 50, close the beach.

    * If there are less than 3 samplings, there is insufficient data.

    * The report will present for each beach on separate lines the name of the lake, the name of the
    beach, and the recommendation ("open", closed" or "insufficient data"). Use the table below to
    convert IDs and numbers into names.

    * Look at the data files to make sure you understand the data format: (july08.data, july15.data, aug15.data).
    Try to determine by hand the output that should be correct for each one of those files.

    * Produce a report for a file of your choice.

    * To read data from the file, you must use the file input protocol (fopen/fscanf/fclose).

    * Your name(s) must appear at the bottom of the printout with the file name.

    Your reports should look like this (lakes and beaches names, not numbers with column properly
    aligned with columns headers):

    Lake Beach Average E-Coli Level Recommendation
    ---- ----- -------------------- --------------
    Ontario Kew Beach 47.12 OPEN
    Huron Goderich 51.13 CLOSED
    Simcoe Sibbald Point 0 INSUFFICIENT DATA
    . . .

    Report from july01.data generated by Harry Potter and Hermione Granger.



    Lake table
    1: Ontario
    2: Erie
    3: Huron
    4: Muskoka
    5: Simcoe

    Beach table
    100: Kew Beach
    101: Sunnyside Beach
    103: Sandbanks
    201: Port Dover
    202: Port Burwell
    203: Crystal Beach
    301: Goderich
    302: Sauble Beach
    303: Kincardine
    401: Muskoka Beach
    501: Sibbald Point

    july08.data
    1 101 5 66.6 33.3 22.2 98.9 11.1
    5 501 2 33.3 44.3

    july15.data
    4 401 4 30.3 50.4 50.5 60.6
    3 301 2 50.0 51.9
    3 302 4 77.7 66.6 22.2 33.3
    3 303 3 55.5 55.5 44.0
    1 103 0
    1 100 10 47.7 52.2 45.5 78.7 45.5 60.2 48.9 43.3 49.9 50.6
    1 101 11 75.5 53.2 65.1 81.1 44.1 42.2 41.1 39.7 51.1 33.3 22.2
    2 201 3 56.6 49.7 45.5
    2 202 4 44.8 66.6 54.4 76.6
    5 501 4 55.5 34.4 66.6 22.2
    aug15.data1 100 12 47.7 52.2 45.5 78.7 45.5 33.2 50.4 60.2 48.9 43.3 49.9 50.6
    1 101 9 75.5 53.2 65.1 81.1 44.1 42.2 41.1 39.7 51.1
    2 201 3 56.6 49.7 45.5
    2 202 2 44.4 66.6
    5 501 4 55.5 34.4 66.6 22.2
    4 401 4 33.3 44.4 55.5 66.6
    3 301 3 50.0 51.1 49.8
    3 302 4 77.7 66.6 22.2 33.3
    3 303 3 55.5 55.5 44.0
    1 103 1 13.3


    This is what I did so far,

    Code:
    #include<stdio.h>
    int 
    main (void)
    {
    FILE *ontario;
    int beach-number, number-of-samples;
    double number-of-organisms, mean-level;
    define number-of-organisms;
    ontario = fopen ("july15.data", "r");
    scanf ("%d,%d,%d", &beach-number, &number-of-samples, &number-of-organisms, &mean-level);
    if (number-of-samples < 3);
    printf ("insufficient data\n"); 
    else if (number-of-samples>=3)
    fscanf (julyfile, "%d", &number-of-organisms);
    mean level = 
    {
    if (mean-level >=0.5);
    printf ("closed\n");
    else if (mean-level <=0.5);
    printf ("open\n");
    }
    print ("beach-number %d is) 
    return(0);
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    No offense, but you have some serious work to do. The quality of code you posted suggests either you have not made any serious effort in learning the material from class, lecture notes, textbook or the internet; or that you have not made any serious effort at attempting this problem, and are hoping that your token code will convince somebody here you tried and we'll do it for you.

    You should also learn How To Ask Questions The Smart Way (don't take the title too personally). You never actually say what is wrong or where you're having trouble, you just post an assignment and a small bit of code, assuming we'll magically figure out what you want from us.

    I really hope I'm wrong about the above points. I hope you take the following seriously and find it helpful.

    First things first, indent your code so we can see what you are trying to do. Use the auto-indent feature of your editor (any decent code editor -- there are many free ones available -- has this feature, learn to use it). Mine produced:
    Code:
    #include<stdio.h>
    
    int main (void)
    {
        FILE *ontario;
        int beach-number, number-of-samples;
        double number-of-organisms, mean-level;
        define number-of-organisms;
        ontario = fopen ("july15.data", "r");
        scanf ("%d,%d,%d", &beach-number, &number-of-samples, &number-of-organisms, &mean-level);
        if (number-of-samples < 3);
        printf ("insufficient data\n"); 
        else if (number-of-samples>=3)
            fscanf (julyfile, "%d", &number-of-organisms);
        mean level = 
        {
            if (mean-level >=0.5);
            printf ("closed\n");
            else if (mean-level <=0.5);
            printf ("open\n");
        }
        print ("beach-number %d is) 
            return(0);
    }
    That shows a few problems.
    1. Why is the printf on line 12 not indented (hint, something is wrong with the end of line 11)? You have similar issues with lines 17 and 19.
    2. What are the braces on line 16 and 21 for?
    3. Why is return(0); indented? What might be missing from line 22 (there are two things missing)? Also, print is not a known function, but printf is.

    Also, - is not a valid character in an identifier name. It has two purposes in C: subtracting numbers and negating numbers. Use the underscore _ character.

    Fixing all that, you should then compile at the maximum warning level (-Wall for gcc, else check your compiler docs). I get:
    Code:
    $ gcc -Wall -ggdb3 -pedantic -std=gnu99 -O0 -o foo foo.c -lm -lpthread -lrt
    foo.c: In function ‘main’:
    foo.c:8:5: error: unknown type name ‘define’
         define number_of_organisms;
         ^
    foo.c:8:12: error: conflicting types for ‘number_of_organisms’
         define number_of_organisms;
                ^
    foo.c:7:12: note: previous declaration of ‘number_of_organisms’ was here
         double number_of_organisms, mean_level;
                ^
    foo.c:10:5: warning: too many arguments for format [-Wformat-extra-args]
         scanf ("%d,%d,%d", &beach_number, &number_of_samples, &number_of_organisms, &mean_level);
         ^
    foo.c:14:17: error: ‘julyfile’ undeclared (first use in this function)
             fscanf (julyfile, "%d", &number_of_organisms);
                     ^
    foo.c:14:17: note: each undeclared identifier is reported only once for each function it appears in
    foo.c:22:5: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
         printf("beach_number %d is"); 
         ^
    foo.c:5:11: warning: variable ‘ontario’ set but not used [-Wunused-but-set-variable]
         FILE *ontario;
               ^
    Line 8: What is define? Why are you trying to redeclare it (see line 7)?
    Line 10: You ask for 3 things, but give scanf 4 variables to fill in. Also, %d is not for double, it's for a decimal integer. Try %lf instead.
    Line 14: What is julyfile? It's not declared anywhere.
    Line 22: You told printf you would print an integer, but you didn't actually give it one to print.

    Now that we have that out of the way, we can get down to really solving your problem. You never stated what topics you have covered in class, so it's hard for me to know exactly which path to point you down. For example, have you covered structs yet? What about arrays or functions?

    Regardless, get rid of the code you have. It's less than 20 lines, and it's not very good. Start over now, before it's too late.

    But don't write any code yet. Seriously, you need to plan this out on paper first. If you can't solve the problem yourself with paper and pencil, there's little hope you'll ever write a program to solve it for you.

    Once you have it planned out, begin implementing it little by little. Work on one small portion (possibly as little as 5 or 10 lines) at a time. Compile it (at max warning level), fix all errors and warnings, then test it. Test all the cases, not just the ones that should make it work (i.e. test bad input, opening a non-existent file, etc). Don't move on to the next section of code until everything you have written up to that point is perfect. No point in continuing to build on a broken base.

    Some good beginning steps might be to work on successfully reading in from the files. Just open the file, read it and print it back to the screen. Remember that most file operations (fopen and fscanf) return a value that tells you if there was an error. Check that value, and if it is not a successful operation, print a useful error message (look into perror() or sterror() and the errno variable) and act accordingly (you decide: terminate the program immediately, return early from the function, continue on as though nothing happened). It's likely that when your teacher grades this, those error cases will not exist, but they very well may exist when you're developing and testing your program, and having good error handling makes it easy to find and fix problems.

    A few more notes:
    1. Use descriptive names for constants, variables and functions. You are already doing this, but just a reminder.
    2. Use constants. What is the significance of 3 or 0.5? Why not define constants called MIN_SAMPLES_REQUIRED and BEACH_CLOSURE_THRESHOLD? Your code becomes more readable and it's easier to change the behavior later if you want, and even make these parameters that can be set by the user.

    Whew! That's a lot of info. I hope it's helpful though. Good luck, and post back if you run into more trouble (remember, format your code nicely and ask smart, specific questions).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,056
    Is this the first ever C program you've written?

    All your homework is based on knowledge gained from previous assignments, so you should be able at a minimum to at least write syntactically valid variable declarations.

    I mean, even basic knowledge like "-" is not valid in an identifier would go a long way to helping you solve the problem.
    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.

  4. #4
    Registered User
    Join Date
    Feb 2015
    Posts
    14

    How to find average?

    Hi,
    please see image.
    how do you find the average like the one shown in the report and how to get the report exactly like the one shown in the picture with heading and columns.
    Here is what i've done so far, thanks in advance.

    Code:
     
    #include<stdio.h>
    
    int 
    main (void)
    
    {
    
    int lake, beach, sampling,sum, count; 
    double organisms, average; 
    FILE *in;
    
    in =fopen("july15.text", "r");
    sum = 0;
    count = 0;
    while (fscanf (in, "%d", &beach) != EOF)
        {
            sum = sum + beach;
            count = count + 1;
        }
        fclose (in);
    
        printf ("There are %d numbers in the file.\n", count);
        printf ("The sum of all the numbers is %d.\n", sum);
        average = (double)sum / count;
        printf ("There average of all the numbers is %5.2lf.\n", average);
    
    printf ("\n Input lake ID >>"); 
    scanf ("%d", &lake); 
    printf ("\n Input beach number >>"); 
    scanf ("%d", &beach); 
    printf ("\n Input number of samples >>"); 
    scanf ("%d", &sampling); 
    printf ("\n Input number of organisms in 100ml of water>>"); 
    scanf ("%IF", &organisms); 
    
    if (sampling <3)
    printf ("Insufficient Data:\n"); 
    else
    if (sampling>=3)
    
    if (average> 50 )
    printf ("closed\n");
    else 
    if (average <= 50)
    printf ("open\n");
            
    if (lake == 1 )
    printf ("Ontario:\n"); 
    else 
    if (lake == 2) 
    printf ("Erie:\n");
    else
    if (lake == 3) 
    printf ("Huron:\n");
    else 
    if (lake == 4) 
    printf ("Muskoka: \n");
    else 
    if (lake == 5)
    printf ("Simcone:\n");
    
    if (beach == 100)
    printf ("Kew Beach:\n"); 
    else 
    if (beach ==101)
    printf ("Sunnyside Beach:\n");
    else 
    if (beach ==103)
    printf ("Sandbanks:\n"); 
    else 
    if (beach ==201)
    printf ("Port Dover:\n"); 
    else 
    if (beach ==202)
    printf ("Port Burwell:\n"); 
    else 
    if (beach ==203)
    printf ("Crystal Beach:\n"); 
    else 
    if (beach ==301)
    printf ("Goderich:\n"); 
    else 
    if (beach ==302)
    printf ("Sauble Beach:\n");
    else 
    if (beach ==303)
    printf ("Kincardine:\n"); 
    else 
    if (beach ==401)
    printf ("Muskoka beach:\n");  
    else 
    if (beach ==501)
    printf ("Sibbald Point:\n"); 
    
    return(0); 
    
    }








    How to find average?-capture-pngHow to find average?-capture1-png

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    No need to start a new thread, your old one (http://cboard.cprogramming.com/c-pro...ease-help.html) was working just fine :P.

  6. #6
    Registered User
    Join Date
    Feb 2015
    Posts
    3
    Okay, I basically have the same exact assignment. I am choosing to use the july08 data. What we learned so far in class is: basic c programming concepts, how to use if statements, and how to use loops (we havent learned functions or arrays yet). I havent struggled with my c programming assignments until now. I am a bit confused as to how to tackle this problem. Like i want to assign that the first column is the lakeids, the second column is the beachids, the 3rd columns are the number of samples, and the last couple of columns are the actual samples. It's just im really confused as to how to go about assigning these variables to each of these specific values. Please can u help me?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    @hello989
    Please start your own thread. Post any code you have in [code][/code] tags and make sure it's properly formatted and indented. Then, ask your question, and be as specific as possible about what exactly you're having trouble with. Calculating the mean? Looking up a lake/beach name? etc, etc.

  8. #8
    Registered User
    Join Date
    Feb 2015
    Posts
    14
    I don't know how to edit my first thread, that's why I started new thread. I took your advice from yesterday ( Thank you ) and started from scratch. I did everything I got but got stuck when calculating mean level.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by Danny.Hoshi View Post
    I don't know how to edit my first thread, that's why I started new thread. I took your advice from yesterday ( Thank you ) and started from scratch. I did everything I got but got stuck when calculating mean level.
    You don't "edit" your previous post (there is a time limit, 1 or 2 hours IIIRC, where you can edit a previous post). You add a new post to the bottom, just like you added the above post to this thread. In fact, editing a previous post is generally bad etiquette since it can make the flow of the thread difficult (later posts that reference the original version might not make sense after the changes). We allow it for a short time, in case you need to fix minor stuff like typos or formatting, or add some small parts.

    I'm not a mod, so I can't merge the two threads, but I'll move your code above, over to the original thread (and fix the formatting).

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    From the OP's other thread (How to find average?

    OP:
    There's a lot of stuff here again, but don't despair; you're making good progress. Keep it up!

    Quote Originally Posted by Danny.Hoshi View Post
    I don't know how to edit my first thread, that's why I started new thread. I took your advice from yesterday ( Thank you ) and started from scratch. I did everything I got but got stuck when calculating mean level.
    Mean is the proper term for what most people call average*. You calculate it by summing all the values, and dividing by the number of items. So you correctly calculate sum and count, and divide. Your problem is that you declared sum as an int. When you put float/double values in an int, the fractional portion is truncated ("chopped off"). So 3.14159 will become just 3. 0.1234 will become just 0. So if the value in beach is always less than 1, then sum = sum + beach; will effectively be sum = sum + 0; and sum will always be 0. Try declaring sum as a double.
    Code:
    #include<stdio.h>
    
    
    int main (void)
    {
        int lake, beach, sampling,sum, count;
        double organisms, average;
        FILE *in;
    
    
        in = fopen("july15.text", "r");
        sum = 0;
        count = 0;
        while (fscanf (in, "%d", &beach) != EOF)
        {
            sum = sum + beach;
            count = count + 1;
        }
        fclose (in);
    
    
        printf ("There are %d numbers in the file.\n", count);
        printf ("The sum of all the numbers is %d.\n", sum);
        average = (double)sum / count;
        printf ("There average of all the numbers is %5.2lf.\n", average);
    
    
        printf ("\n Input lake ID >>");
        scanf ("%d", &lake);
        printf ("\n Input beach number >>");
        scanf ("%d", &beach);
        printf ("\n Input number of samples >>");
        scanf ("%d", &sampling);
        printf ("\n Input number of organisms in 100ml of water>>");
        scanf ("%IF", &organisms);
    
    
        if (sampling < 3)
            printf ("Insufficient Data:\n");
        else if (sampling >= 3)
            if (average > 50)
                printf ("closed\n");
            else if (average <= 50)
                printf ("open\n");
    
    
        if (lake == 1)
            printf ("Ontario:\n");
        else if (lake == 2)
            printf ("Erie:\n");
        else if (lake == 3)
            printf ("Huron:\n");
        else if (lake == 4)
            printf ("Muskoka: \n");
        else if (lake == 5)
            printf ("Simcone:\n");
    
        if (beach == 100)
            printf ("Kew Beach:\n");
        else if (beach == 101)
            printf ("Sunnyside Beach:\n");
        else if (beach == 103)
            printf ("Sandbanks:\n");
        else if (beach == 201)
            printf ("Port Dover:\n");
        else if (beach == 202)
            printf ("Port Burwell:\n");
        else if (beach == 203)
            printf ("Crystal Beach:\n");
        else if (beach == 301)
            printf ("Goderich:\n");
        else if (beach == 302)
            printf ("Sauble Beach:\n");
        else if (beach == 303)
            printf ("Kincardine:\n");
        else if (beach == 401)
            printf ("Muskoka beach:\n");
        else if (beach == 501)
            printf ("Sibbald Point:\n");
    
    
        return(0);
    }
    Now, we compile at max warning level:
    Code:
    $ make foo
    gcc -Wall -ggdb3 -pedantic -std=gnu99 -O0 -o foo foo.c -lm -lpthread -lrt
    foo.c: In function ‘main’:
    foo.c:31:5: warning: 'I' flag used with ‘%F’ gnu_scanf format [-Wformat=]
         scanf ("%IF", &organisms);
         ^
    foo.c:31:5: warning: format ‘%F’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat=]
    foo.c:35:13: warning: suggest explicit braces to avoid ambiguous ‘else’ [-Wparentheses]
         else if (sampling >= 3)
                 ^
    "%IF" is not a proper format string, you probably meant "%lf" (lower case ell).
    The other error, on line 35, is a bit trickier. It wants to make sure you realize that the else if on line 38 belongs to the if on line 36, not the else if on line 35. That's why it suggests explicit braces. I recommend always using braces, especially for newbies, to help ensure their logic is correct, and so they don't have any problems if they decide to add extra statements to an if/else or loop. So something like:
    Code:
    else if (sampling >= 3) {
        if (average > 50) {
            printf ("closed\n");
        }
        else if (average <= 50) {
            printf ("open\n");
        }
    }
    One thing to note about your checks for number of samples and average level is that the "else if" part can just become an else. You are only considering two states in each case, if it's not one, it must be the other; no need for the second check:
    Code:
    if (sampling < 3)
        printf ("Insufficient Data:\n");
    else  // to get here, the if must be false, so sampling is not < 3, it must be >= 3 and we have enough samples.  This is implicit, no need for "else if"
        if (average > 50)
            printf ("closed\n");
        else  // Similar here, if it's not > 50, it must be <= and thus open
            printf ("open\n");
    Let's add braces
    Code:
    if (sampling < 3) {
        printf ("Insufficient Data:\n");
    } else {
        if (average > 50) {
            printf ("closed\n");
        } else {
            printf ("open\n");
        }
    }
    You could probably stand to use more descriptive names. How about things like:
    number_of_samples
    mean_bacterial_level
    lake_number
    beach_number
    num_organisms_per_100ml
    sum and count are fine

    Two notes on file IO
    1. You should also check your file operations. If fopen returns NULL, you don't have a file to read. Print a message and exit.
    2. Note that fscanf returns the number of items successfully scanned. It returns EOF on a more serious failure, but may return 0 if the file simply has bad data; your loop version would miss that condition. Since you ask for one item (in your loop condition), you should only do the loop body if the return value is 1. while (fscanf(...) == 1).

    Lastly, you should not have a giant if/else construct for lake and beach names. Store the names in an array to look up or look them up in the file at the time you process a line from july08.txt (or whatever bacteria sampling data file).

    * Mean is just one type of average, median and mode are two other common types.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,776
    The threads have been merged.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Feb 2015
    Posts
    14
    this is what i have done so far. still not working.
    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.text", "r");
    
    while (fscanf (in, "%d" "%d" "%d", &i, &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);
    }
    sum = 0; 
    
    
        }
    
    
    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); 
    }

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,056
    You need to read and apply this, so your code is much easier to follow.
    Indent style - Wikipedia, the free encyclopedia

    With consistent indentation, many bugs such as "why is this happening now" become instantly apparent simply from the way the code presents itself.
    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.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    Why doesn't it work?

    Have you studied functions, arrays and structures yet?

    These look like good candidates for arrays/arrays of structures.:
    Code:
    Lake table
    1: Ontario
    2: Erie
    3: Huron
    4: Muskoka
    5: Simcoe
    
    Beach table
    100: Kew Beach
    101: Sunnyside Beach
    103: Sandbanks
    201: Port Dover
    202: Port Burwell
    203: Crystal Beach
    301: Goderich
    302: Sauble Beach
    303: Kincardine
    401: Muskoka Beach
    501: Sibbald Point
    You really shouldn't be hard coding this information into your print statements.

    Lastly you really need to find and use consistently an indentation style you like. It'll make following your program logic much easier. Indentation combined with the proper use of functions will make you program much easier to read and troubleshoot.

    By the way the following line is wrong, the number of samples is the third entry in the line, not the fourth.

    Code:
    while (fscanf (in, "%d" "%d" "%d", &i, &beach_number,&number_of_samples) != EOF)
    Jim

  15. #15
    Registered User
    Join Date
    Feb 2015
    Posts
    14
    This is what I did so far. It works but it doesn;t show mean level, it only shows lake name and open or close.

    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);
    }

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