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!
Originally Posted by
Danny.Hoshi
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.