Dividing and EOF

This is a discussion on Dividing and EOF within the C Programming forums, part of the General Programming Boards category; This program that I wrote is supposed to get 3 sets of 5 numbers from the user and get the ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    49

    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. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    %d does not mean double, it means decimal.
    To read doubles you need %f
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    49
    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. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >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]
    Last edited by Dave_Sinkula; 09-26-2003 at 09:38 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by Dave_Sinkula
    >To read doubles you need %f
    I thought of that after I posted, but...
    Sorry Tride.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    49
    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. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >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.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

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