Help with C Program

This is a discussion on Help with C Program within the C Programming forums, part of the General Programming Boards category; Hello Everyone, I'm tying to make a C program which determines the level of bacteria for some beaches. The program ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    6

    Help with C Program

    Hello Everyone,

    I'm tying to make a C program which determines the level of bacteria for some beaches.
    The program is supposed to access the data from a txt file and give the result.

    Here's what I've been able to do so far.
    I have no idea where to go from here, so please help!!

    The text file is attached. The first number in the txt file refers to the lake ID, the second number to the beach number, the third number refers to the number of samplings and the rest of the numbers are the samplings of the number of organisms in 100ml of water.

    Code:
    #include <stdio.h>
    
    int
    main (void)
    { 
    	FILE * july01;
    	
    	int lake_id, beach_num, num_samplings, num;
    	double num_organisms, tot_organisms;
    		
    	july01 = fopen ("july01.txt", "r"); 
    	
    	tot_organisms = 0;	
    		
    	while (fscanf (july01, "&#37;d %d %d", &lake_id, &beach_num, &num_samplings) != EOF) 
    
      {
    	 tot_organisms = 0; 
    	
    	for (num = 1; num <= num_samplings; num++)
    	  	{			
    		fscanf(july01, "%d",  &num_organisms);		
    		tot_organisms = tot_organisms + num_organisms;
    		}
    
    	if  (((tot_organisms / num_samplings) / 100) > 0.5)
    		printf ("Close");
    
    	else if  (num_samplings < 3)
    		printf ("Insufficient Data");
    
    	else if  (((tot_organisms / num_samplings) / 100) <= 0.5)
    		printf ("Open");
    			
      }
      
     fclose (july01);
    }

    Here are the conditions for the program

    If the average level is above 50/100ml, close the beach.
    If there are less than 3 samplings, there is insufficient data.

    The program should show 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.

    Lakes
    1: Ontario
    2: Erie
    3: Huron

    Beaches
    101: Kew Beach
    102: Sunnyside Beach
    201: Port Dover
    202: Port Burwell
    301: Goderich
    302: Sauble Beach
    Attached Files Attached Files
    Last edited by anjaan_101; 10-20-2007 at 02:00 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > for (num = 1; num <= num_samplings; num++)
    > fscanf(july01, "&#37;d", &num_organisms);
    > tot_organisms = tot_organisms + num_organisms;
    Add more curly braces to your code to indicate what you really mean.
    Despite your indentation, the red code is NOT in the loop


    > fscanf(july01, "%d", &num_organisms);
    The format and the data type don't match.

    Also, it's not a good idea to mix spaces and TABS when writing code, especially if you intend to post that code on a forum, in an email, in another editor.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    OK.
    I added the curly brackets. But i dont know what u mean by "the format and the data type don't match"???

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    &#37;d is for an int, you've suppied a double.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    i changed the %d to %if. still the program is nowhere near completion.
    Can i get some more help please.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Should be "&#37;lf", not "%if" (in case that wasn't a typo). Better yet, make num_organisms an int, and only use double for those variables that actually need to be floating point (you can use a cast to force floating-point division where necessary).
    Last edited by robatino; 10-20-2007 at 05:17 AM.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    ((tot_organisms / num_samplings) / 100)
    This will provide you with the normal integer value. It would be better if you change the 100 to 100.0 or you will have to tpye cast.

    ssharish

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by ssharish2005 View Post
    Code:
    ((tot_organisms / num_samplings) / 100)
    This will provide you with the normal integer value. It would be better if you change the 100 to 100.0 or you will have to tpye cast.

    ssharish
    If either tot_organisms or num_samplings is a floating-point type, the whole calculation will be done that way. If not, then tot_organisms/num_samplings will be computed as an integer, and truncated accordingly, and making 100 into a double by using 100. won't fix that. If both tot_organisms and num_samplings are integer types, then one of them needs to be cast to a floating-point type to make the division work as intended. Alternatively, the whole expression could be modified to make it work with integers. For example,
    Code:
    ((tot_organisms / num_samplings) / 100) > 0.5
    could be replaced by
    Code:
    tot_organisms > 50*num_samplings
    Last edited by robatino; 10-20-2007 at 07:39 AM.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Well since the tot_organisms / num_samplings or both double, their wont have to be decimal points on 100. Sice the result of the tot_organisms / num_samplings will be anyway double and resulting value would be double.

    ssharish

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    Thanx for all ur help guys. But I know for sure that there is some problem in the for loop as the num_organisms are not getting added to the tot_organisms. Can someone please help me figure it out??

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Without seeing your latest code, not a chance.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    asdas
    Last edited by anjaan_101; 10-21-2007 at 07:21 PM.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 new.c
    new.c: In function `main':
    new.c:23: warning: float format, double arg (arg 3)
    new.c:83: warning: control reaches end of non-void function
    The first one relates to
    fscanf(july01, "&#37;f", &num_organisms);
    Use "%lf" to scan into a double.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    6
    It works!! Thanx a lot for ur help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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