Solution for Exercise 12-3

ex1203

#include <stdio.h>

int main()
{
    int highscore[4];

    printf("Your highest score: ");
    scanf("%d",&highscore[0]);
    printf("Your second highest score: ");
    scanf("%d",&highscore[1]);
    printf("Your third highest score: ");
    scanf("%d",&highscore[2]);
    printf("Your fourth highest score: ");
    scanf("%d",&highscore[3]);

    puts("Here are your high scores");
    printf("#1 %d\n",highscore[0]);
    printf("#2 %d\n",highscore[1]);
    printf("#3 %d\n",highscore[2]);
    printf("#4 %d\n",highscore[3]);

    return(0);
}

Output

Your highest score: 750
Your second highest score: 699
Your third highest score: 675
Your fourth highest score: 666
Here are your high scores
#1 750
#2 699
#3 675
#4 666

Notes

* The array is declared at Line 5. It's named highscore and it has room for 4 elements.

* The scanf() statements fill each value of the highscore array. The elements are numbered in the square brackets, 0 through 3. Yes, that's four items, which explains the 4 on Line 5.

* Even though highscore is an array, its elements are integer values. Therefore, they must be prefixed by the & operator in the scanf() function.

* Lines 17 through 20 display the highscore array's elements, 0 through 3. Each array element is referenced just like an individual int variable.