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

Filling an Array

So far, you've written all the elements in curly braces. Often, though, the program calculates the values itself. Then create an array of the right size without any values, and fill it in a loop:

int squares[5];
int count = sizeof(squares) / sizeof(squares[0]);
for (int i = 0; i < count; i++) {
    squares[i] = i * i;
}
for (int i = 0; i < count; i++) {
    printf("%d ", squares[i]);
}
printf("\n");

This prints:

0 1 4 9 16

int squares[5]; creates an array of five elements that don't have any values yet. sizeof works even now, because the size of an array is fixed from the start. The first loop gives each element its value, the square of its index: squares[0] gets 0, squares[1] gets 1, squares[2] gets 4, and so on. The second loop prints them.

Don't read an element before it has a value: C doesn't say what's in it. For a single variable without a value, this site reports an error, but for an element of an array, it doesn't, so this mistake is up to you to avoid.

If you want all the elements to start at zero, write = {0}. This is handy for counters. Here, each element counts the votes for one of four candidates:

int votes[4] = {0};
votes[2]++;
votes[2]++;
votes[0]++;
int count = sizeof(votes) / sizeof(votes[0]);
for (int i = 0; i < count; i++) {
    printf("%d ", votes[i]);
}
printf("\n");

This prints:

1 0 2 0

When the braces hold fewer values than the size of the array, C fills the remaining elements with zeros. {0} sets the first element to 0, and the others become 0 too. Watch out: = {1} doesn't fill the array with ones. It sets only the first element to 1, and all the others are 0.

The curly braces work only when you create an array. Later, you can't fill the whole array at once: votes = {0, 0, 0, 0}; ends with error: expected expression. You can't copy an array with = either. If copy and scores are two arrays of five ints, copy = scores; doesn't compile:

main.c:6:10: error: array type 'int[5]' is not assignable

The message says that an array can't be assigned to as a whole. To copy an array, copy it element by element, in a loop:

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

This prints:

80 88

The copy must be at least as big as the original. In a smaller one, the loop would get past its end.

Instructions

  1. 1

    Below the comment, create an array multiples of 10 whole numbers, without values. Then fill it in a loop with the 7 times table: 7, 14, 21 and so on up to 70. The element with index i gets the value (i + 1) * 7.

  2. 2

    In a second loop, print all the elements on one line, separated by spaces: 7 14 21 28 35 42 49 56 63 70.

  3. 3

    Below that, create a second array copy of 10 whole numbers, and copy multiples into it in a loop.

  4. 4

    Finally, print the copy on one line too, with Copy: in front of it. The output should include the line Copy: 7 14 21 28 35 42 49 56 63 70.

Back

10/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.