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);
}

Output

Your #1 score: 1000
Your #2 score: 995
Your #3 score: 980
Your #4 score: 860
Your #5 score: 810
Your #6 score: 799
Your #7 score: 787
Your #8 score: 715
Your #9 score: 699
Your #10 score: 0
Here are your high scores
#1 1000
#2 995
#3 980
#4 860
#5 810
#6 799
#7 787
#8 715
#9 699
#10 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 when if you forget to properly set the array size (in the square brackets). For example, you declare highscore[3] or just highscore[] instead of highscore[10]. In this situation, the extra elements are written into unknown storage, which is a serious security risk. It falls upon you, the programmer, to ensure that arrays are properly sized to contain the elements required.