Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Looping over an Array

You can't print a whole array at once. If you try printf("%d\n", scores);, the program doesn't compile:

main.c:5:20: error: format specifies type 'int' but the argument has type 'int *' [-Werror,-Wformat]

%d prints one whole number, but printf got the whole array scores. (C writes its type here as int *. You'll learn what the asterisk means in the lesson on pointers.) An array has to be printed element by element.

For five elements, five printfs would do. For a hundred, it would be a lot of typing. Luckily, an index doesn't have to be a number written in the code. It can also be a variable: if i is 2, then scores[i] is the same as scores[2]. And a for loop can take i through all the indexes, from 0 to the last one:

int scores[] = {80, 92, 75, 60, 88};
for (int i = 0; i < 5; i++) {
    printf("%d\n", scores[i]);
}

This prints:

80
92
75
60
88

In the first iteration of the loop, i is 0 and the loop prints scores[0]. In the second iteration, i is 1 and it prints scores[1], and so on up to scores[4]. The condition is i < 5, not i <= 5: the array has 5 elements, so the last index is 4. In the next exercise, you'll see what happens with index 5.

The number of elements often appears in several places in a program, for example in several loops. It's a good idea to store it in a constant, so that it's written only once:

int scores[] = {80, 92, 75, 60, 88};
const int COUNT = 5;
for (int i = 0; i < COUNT; i++) {
    printf("Test %d: %d points\n", i + 1, scores[i]);
}

This prints:

Test 1: 80 points
Test 2: 92 points
Test 3: 75 points
Test 4: 60 points
Test 5: 88 points

The test number is i + 1, because people count tests from 1, while indexes start at 0. The constant must match the array. If you add a sixth score to the array and forget to change COUNT, the loop still goes through only five elements, and the new score never gets printed.

A loop can also print the elements on one line, separated by commas. The comma goes before every element except the first one, the one with index 0:

int scores[] = {80, 92, 75, 60, 88};
const int COUNT = 5;
for (int i = 0; i < COUNT; i++) {
    if (i > 0) {
        printf(", ");
    }
    printf("%d", scores[i]);
}
printf("\n");

This prints:

80, 92, 75, 60, 88

The last printf("\n") is after the loop. It ends the line once all the elements are printed.

Instructions

  1. 1

    Below the line that prints the captain, create a constant COUNT with the number of players, 5. Then write a for loop whose variable goes through the indexes, starting at 0, while it's less than COUNT.

  2. 2

    In the loop, print each jersey number on its own line: 7, 10, 23, 4 and 11.

  3. 3

    Below that, write a second loop that prints all the numbers on one line, separated by a comma and a space: 7, 10, 23, 4, 11.

Back

4/12

  1. 1. Creating an Array
  2. 2. Reading Elements
  3. 3. Changing Elements
  4. 4. Looping over an Array
  5. 5. Past the End
  6. 6. The Size of an Array
  7. 7. Sum and Average
  8. 8. Counting
  9. 9. The Largest Value
  10. 10. Filling an Array
  11. 11. Two-Dimensional Arrays
  12. 12. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.