# odd even count error

This is a discussion on odd even count error within the C Programming forums, part of the General Programming Boards category; I'm writing a C program that reads integars from file "integars.dat". The program should show how many numbers you have ...

1. ## odd even count error

I'm writing a C program that reads integars from file "integars.dat". The program should show how many numbers you have entered, how many among them were even, and how many were odd. Also the average of integer numbers that were odd.

I wrote everything accordingly, but I still don't understand where I'm messing up.

Code:
#include <stdio.h>
int

main (void)

{
FILE *in;
int number, s, count, w, j, status, sum, n;
double y, average;

in = fopen ("integars.dat", "r");

count = 0;
sum = 0;
average = y;

status = fscanf (in, "%d", &number);
while (status != EOF)

{
status = fscanf(in, "%d", &s);
for (j=1; j<=s; j++)

{
count = count + 1;
fscanf (in, "%lf", &y);
average = (double)sum/number;

}

fscanf(in, "%d", &w);
if (n%2==0){
printf ("There are %d even numbers.\n", w);

}
else
printf ("There are %d odd numbern.\n", w);
}

status = fscanf (in, "%d", &number);

fclose (in);

printf ("There are %d numbers in the file.\n", count);

return (0);
}

2. Are you the same person who started the odd even number thread?

Anyway:
• Use descriptive variable names.
• Why do you read into both number and s?
• Why do you use nested loops?

You really only need one variable to read into, and then you need to keep track of:
• the number of numbers entered
• the number of even numbers entered
• the number of odd numbers entered (or use subtraction)
• the sum of the odd numbers

3. Step 1 one use meaningful variables names; DO NOT use three variables for the exact same purpose.

Note: "Sum" is normally calculated in-side a loop; "Average" is normally done below the loop that did "Sum".

Post an example "integars.dat" file; it is obvious you have no idea what it contains or you have no idea how to write a loop to read it.

Edit: Slow posting once more.

Tim S.

4. I thought that you need to use different variables. I'm completely new to this, so I'm totally lost.

"integars.dat"
3
7
2
11
4

5. In this case, it is fine to overwrite the variable used for reading. In some other cases, you might read into an array instead because you need all the values read even after the loop that does the reading and initial processing.

6. Code:
in = fopen ("integars.dat", "r");
It would also be a good idea of getting into the habit of checking the return value when opening a file.

Code:
#include <string.h>
#include <errno.h>
in = fopen ("integars.dat", "r");
if (in == NULL) {
fprintf(stderr, "Error opening file integars.dat. - %s", strerror(errno));
exit(1);
}

7. My program shouldn't consist of any error checkers. Just nested loops, arrays and the odd even is just not coming out right. I dont even undrstand how to get the average of the odd numbers.

Help please :S
It's beginners stuff, and I cant get past it even.

8. Originally Posted by neveragn
My program shouldn't consist of any error checkers. Just nested loops, arrays and the odd even is just not coming out right.
If your professor is teaching you not to check for errors, then you have a crappy teacher. If that's your own, personal rule, then you need to change it. You'll be totally baffled as to the answers you get if your program can't read the numbers because it couldn't open the file in the first place. At least put them in there for your sake. You can remove them before you turn in your program if need be.

I dont even undrstand how to get the average of the odd numbers.
Can you determine if a number is odd or even by hand? How do you do that? Can you find the average of a set of numbers by hand? What are the steps? You should never start coding until you understand your problem and know how to solve it by hand.

9. Just follow laserlight's advice:

You'll need a variable for each of the items laserlight detailed.

For example:
Code:
int oddNumberCount;
int evenNumberCount;
int totalEntered; /* total number of ALL integers read from the file */
You should only have one loop in your code to read in the file. You have two. You do not need the nested for loop.
Calculate your average AFTER you have read, tallied, and summed in all your integers.

You already have the test in your code to determine if a number is odd or even, so when you encounter an odd number, increment oddNumberCount by 1, else an even number increment evenNumberCount by 1.
Regardless if the number is even or odd, increment totalEntered by 1, then add the number to your sum variable.

After you have finished reading in all the numbers and exit the while loop, then you can calculate the average.

10. Code:
#include <stdio.h>
int
main (void)
{
FILE *in;
int odd, even, sum, number,n;
double average;

in = fopen ("integars.dat", "r");

while(fscanf(in, "%d", &number) == 1){
n++;

if (number%2==0)
even++;

else{

sum += number;
odd++;
}

}

fclose (in);
printf("There are %d even integars in the file", even);
printf("There are %d odd integars in the file", odd);
printf("There are %d integars in the file.\n", sum);
average = sum/odd;
printf("The average of odd integars in the file is:  ", average);

return (0);
I'm deffinetly failing this course:S

11. Don't panic. You are so close!

You have to tackle this one problem at a time.

When you compile this you probably received a warning about too many arguments for format. You need to put a "%f" in there to print your average.

Think about what you are saying and printing.
In line 29 you are saying you x integers in the file, yet you are printing the sum of the integer, NOT how many integers there are.

12. Originally Posted by neveragn
I'm deffinetly failing this course:S
With that attitude, you will definitely fail. You can do this though. You're really close now, just a few minor issues.
• You need to initialize odd, even, sum and n to zero before you increment or add to them.
• You need to check the return value of fopen and exit if it failed (see below).
• You should put a new line (\n) at the end of each printf, so you can read the output more easily.
• You need a format specifier for average. It's a double, so you need to put a %f.
• Since sum and odd are integers, sum/odd does integer division, so you need to cast either sum or odd to a double (also see below).

Code:
in = fopen("integers.dat", "r");
if (in == NULL) {
perror("Failed to open integers.dat");
return(1);
}
...
average = (double) sum / odd;

13. omg! It's actually working!! .. Thank you so soo much

Popular pages Recent additions