Solution for Exercise 12-5

ex1205

#include <stdio.h>

int main()
{
    int highscore[10];
    int x;

    for(x=0;x<10;x++)
    {
        printf("Your #%d score: ",x+1);
        scanf("%d",&highscore[x]);
    }

    puts("Here are your high scores");
    for(x=0;x<10;x++)
        printf("#%d %d\n",x+1,highscore[x]);

    return(0);
}

Notes

* Give yourself a For Dummies bonus point if you used a constant in your answer to set the number of array elements as well as the looping values for the two for statements. That's pretty clever.

* The compiler doesn't flag a warning if you forget to reset the element number in the array's declaration at Line 5. What happens if, for example, you declare highscore[3], is that the extra elements are written into unknown storage. They could overwrite other variables, overwrite code, or screw things up royally. Therefore it falls upon you, the programmer, to ensure that arrays are properly sized to contain the elements they require.