Thread: alotta Array troubles

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    alotta Array troubles

    I have a logic error in this code. I believe it is in the last two blocks of code. I keep getting the same output regardless of the values I put in.
    Code:
    /* scores.c >>>>> using loops to process arrays */
    
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 10
    
    int main(void)
    
    {
    
        int index, score[SIZE];
        int sum = 0;
        float average;
    
        printf("Enter %d scores:\n", SIZE);
        for (index = 0; index < SIZE; index++)
           scanf("%d", &score[index]); /* accepts ten scores regardless of whitespace */
    
        printf("The scores read in are as follows:\n");
        for (index = 0; index < SIZE; index++);
           printf("%5d", score[index]);
        printf("\n");
    
        for (index = 0; index < SIZE; index++);
          sum += score[index];
        average = (float) sum/SIZE;
        printf("Sum of scores = %d, average = %.2f\n", sum, average);
    
        system("pause");
        return (0);
    
    
    
    }
    This is always the output.
    Code:
    Enter 10 scores:
    10
    10
    10
    10
    10
    10
    101
    10
    10
    10
    The scores read in are as follows:
    37814124
    Sum of scores = 37814124, average = 3781412.50
    Press any key to continue . . .

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for (index = 0; index < SIZE; index++);
    See that ; at the end of the line?
    It means this loop does nothing for SIZE iterations of the loop.

    Always use braces to say what you mean
    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.

  3. #3
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Here

    Code:
    /* scores.c >>>>> using loops to process arrays */
    
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 10
    
    int main(void)
    {
        int index, score[SIZE];
        int sum = 0;
        float average;
    
        printf("Enter %d scores:\n", SIZE);
        for (index = 0; index < SIZE; index++){
           scanf("%d", &score[index]); 
           }
           
        printf("The scores read in are as follows:\n");
        for (index = 0; index < SIZE; index++){
           printf("%5d", score[index]);
           printf("\n");
           }
    
        for (index = 0; index < SIZE; index++){
            sum += score[index];
            average = (float) sum/SIZE;
            }
            
        printf("Sum of scores = %d, average = %.2f\n\n", sum, average);
            
        system("pause");
        return (0);
    }

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    shouldn't that have given me a syntax error?

  5. #5
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Originally posted by caroundw5h
    shouldn't that have given me a syntax error?
    What???

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Originally posted by Salem
    > for (index = 0; index < SIZE; index++);
    See that ; at the end of the line?
    It means this loop does nothing for SIZE iterations of the loop.

    Always use braces to say what you mean
    Talking about this.

    Btw like your slot machine. Can I refactor it?

  7. #7
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    My code:
    Code:
       for (index = 0; index < SIZE; index++){
            sum += score[index];
            average = (float) sum/SIZE;
            }
            
        printf("Sum of scores = %d, average = %.2f\n\n", sum, average);
    Your code:
    Code:
        for (index = 0; index < SIZE; index++);
          sum += score[index];
        average = (float) sum/SIZE;
        printf("Sum of scores = %d, average = %.2f\n", sum, average);
    Notice how i have used the braces....

    Yes you cant change SIZE coz its a defined constant....your compiler will give you a warning...

    Btw....is refactor a word???

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >shouldn't that have given me a syntax error?
    No, an empty statement is a valid loop body:
    Code:
    for ( i = 0; i < 10; i++ )
      ;
    If you have problems with writing code like this by accident:
    Code:
    for ( i = 0; i < 10; i++ );
    then you should put braces around every loop and conditional statement. It's a slippery bug, and the compiler probably won't help you with it.
    My best code is written with the delete key.

  9. #9
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thanks, I should have remembered that regarding the empty statement. Code errors are usally syntactic or logic, this one lead me to believe it truly was logic. slippery indeed - for C.

  10. #10
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Originally posted by caroundw5h
    Thanks, I should have remembered that regarding the empty statement. Code errors are usally syntactic or logic, this one lead me to believe it truly was logic. slippery indeed - for C.
    I believe "Although quite powerful, C is a pain in the ass."

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Compare the results of these two loops
    Code:
    int main ( void ) {
        int i;
        for ( i = 0 ; i < 10 ; i++ ) {
            printf( "Loop value=%d\n", i );
        }
        printf( "Final value=%d\n", i );
    
        printf( "now with mis-placed ;\n" );
        for ( i = 0 ; i < 10 ; i++ ) /* LOOK */ ; /* AT ME */ {
            printf( "Loop value=%d\n", i );
        }
        printf( "Final value=%d\n", i );
        return 0;
    }
    See what one ; does for you
    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.

  12. #12
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    I see what your saying.

    Is the second for loop even executed though
    Code:
     printf( "now with mis-placed ;\n" );
        for ( i = 0 ; i < 10 ; i++ ) /* LOOK */ ; /* AT ME */ {
            printf( "Loop value=%d\n", i );
        }
    or is it ignored by the compiler or results in undefined behaviour, in which case the last statements, prints the value of i when it finished iterating the first time.
    Code:
    printf( "Loop value=%d\n", i );
     printf( "Final value=%d\n", i );

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Is the second for loop even executed though
    Yes, it does the ; 10 times (ie nothing)
    Don't let the formatting and braces fool you, they're not part of the loop at all, even though they look like they are

    This is the 2nd 'loop' reformatted
    Code:
        for ( i = 0 ; i < 10 ; i++ )
            ;   /* Do nothing */
        {   /* redundant inner scope braces - they serve no purpose here */
            printf( "Loop value=%d\n", i );
        }
        printf( "Final value=%d\n", i );
    > or is it ignored by the compiler or results in undefined behaviour
    Oh it's well defined, just not what you want.
    The final value of i is still 10, since that's the value it has when it exits the loop.
    The fact that nothing else happened doesn't matter.
    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.

  14. #14
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thank you, I've learnt much today.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  3. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM