# Thread: Dividing and EOF

1. ## Dividing and EOF

This program that I wrote is supposed to get 3 sets of 5 numbers from the user and get the average of each set, but it's not getting the correct average for some numbers.

Here's the code:

Code:
```#include <stdio.h>
#define ROWS 3
#define COLS 5
double store(double st[][5]);
double average(double av[][5]);

main()
{
double ar[3][5];
//10
printf("Please enter 3 sets of 5 double numbers:\n");
store(ar);
average(ar);

return 0;
}

double store(double st[][5])
{
int i, n;    //20
char c;

for (n = 0; n < ROWS; n++)
{
printf("Enter 5 numbers for row %d:\n", n + 1);
for (i = 0; i < COLS; i++)
scanf("%d", &st[n][i]);
c = getchar();
}

}

double average(double av[][5])
{
int i, n;
double total;

for (n = 0; n < ROWS; n++)
{
total = 0;
for (i = 0; i < COLS; i++)
total += av[n][i] / COLS;
printf("The average of the numbers in set %d is %d\n", n + 1, total);
}
}```
If, for instance, I enter all "1"s it says the average of them is 2 (which is obviously incorrect). What is wrong here? It doesn't seem like anything is wrong, and I've tried two other ways of getting and reporting the average.

And one more thing, what is EOF? I've seen it described a few times---in books and on the internet---but I just don't understand what it is. For example, how does a loop with a test condition of something being compared to EOF end, besides pressing Ctrl+D or Ctrl+Z or something like that? What exactly is the use of EOF?

2. %d does not mean double, it means decimal.
To read doubles you need %f

3. Changing the "%d"s to "%f"s (the ones that properly need to be changed) produces weird results of very large negative numbers. Is there something else in the code that is wrong?

4. >To read doubles you need %f

To scanf a double, use "%lf". To printf a double, use "%f".

[EDIT]
Originally posted by Tride
And one more thing, what is EOF? I've seen it described a few times---in books and on the internet---but I just don't understand what it is.
EOF is a macro "which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream".
Originally posted by Tride
For example, how does a loop with a test condition of something being compared to EOF end, besides pressing Ctrl+D or Ctrl+Z or something like that?
It doesn't.

Well, you might bail out of the program with a 'Ctrl+C' or such.

Or you might be piping an actual file to the program and thus encounter EOF to end the loop.
Originally posted by Tride
What exactly is the use of EOF?
Let's use getchar as an example. If it doesn't return EOF, then it returned "the next character from the input stream pointed to by stdin". It is advantageous to know that you actually got a character from the stdin before you might attempt to use it.

[/EDIT]

5. Originally posted by Dave_Sinkula
>To read doubles you need %f
I thought of that after I posted, but...
Sorry Tride.

6. Hmm... I had the idea that type double variables could have their value displayed using %d if the value was an integer. Thanks for clearing that up!

And about EOF, is it just an unseen character at the end of every input? If so, I can see how
Let's use getchar as an example. If it doesn't return EOF, then it returned "the next character from the input stream pointed to by stdin". It is advantageous to know that you actually got a character from the stdin before you might attempt to use it.
would work. Does it have many other uses besides in a case like you gave? But it's not like EOF is something I have to know a lot about to be a good programmer, right?

7. >Does it have many other uses besides in a case like you gave?

7.4 Character handling <ctype.h>
1 The header <ctype.h> declares several functions useful for classifying and mapping characters. In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined.
The fclose function returns zero if the stream was successfully closed, or EOF if any errors were detected.
The fflush function sets the error indicator for the stream and returns EOF if a write error occurs, otherwise it returns zero.
The fscanf function returns the value of the macro EOF if an input failure occurs before any conversion. Otherwise, the function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.
And so on. Many of the stdio functions return EOF when they were unsuccessful or an error occurred.
• fclose
• fflush
• fscanf, scanf, sscanf, fvscanf, fscanf, vsscanf
• fgetc, getc, getchar
• fputc, putc, putchar
• fputs, puts
• ungetc
>But it's not like EOF is something I have to know a lot about to be a good programmer, right?

The underlying specifics of EOF on a particular implementation? No.

That you should be aware of functions that will indicate they were not successful by returning EOF, and checking for success before continuing? Yes.

8. >>But it's not like EOF is something I have to know a lot about to be a good programmer, right?<<
To go with what's already mentioned:
http://faq.cprogramming.com/cgi-bin/...&id=1043284351

Popular pages Recent additions