Arrays and Loops

If you need to perform a function on each element in an array, then use a for loop. Set the counter at 0, so that the loop will start with the array element in index 0, and then loop through each element of the array:

for(i = 0; i < number of elements; i++)

In this case, the number of elements would be the size of the array.

For example, if we have an array with 4 elements, then the loop would be: for(i = 0; i < 4; i++).

To loop through each element of the array, we need to use the variable i as an index number in our print statment, like this:

printf(“%d”, index[i]);

Putting the loop and the printf statements together, we can print each element of the array:

for(i = 0; i < number of elements; i++)

printf(“%d”, index[i]);

Now let’s use this with a real array:

int numbers[4] = { 22, 33, 44, 55 };
for(i = 0; i < 4; i++) 

printf(“%d”, numbers[i]);

 

This would print:

22
33
44
55

Sample Array Loop

Let’s look at an example that loops through each element of an array. During the 20th century, geologists began drilling into the glaciers of Antarctica and Greenland, to extract long ice cores. These cores contain records of Earth’s climate history over the past millenia. Geologists can determine when ice ages, volcanic eruptions, and other climate changes occurred by studying these ice cores.

The northernmost ice core samples come from the GRIP (Greenland Ice Project). These samples reveal huge amounts of acid snow and dust during years of massive volcanic eruptions. Here is a script that tells about some of these eruptions:

#include<stdio.h>
void main( )
{
int i = 0;
int eruption[4] = { 536, 626, 934, 1258 };
printf(“\nThe GRIP ice cores suggest massive volcanic eruptions during these years:”);
for(i = 0; i < 4; i++)
printf(“\n%d AD”, eruption[i]);

 

Save your source code as erupt.c. Compile it. Link it. Run it.

This program loops through each element of the eruption array, and prints it with AD. Your output should be:

The GRIP ice cores suggest massive volcanic eruptions during these years:
536 AD
626 AD
934 AD
1258 AD

 

Published by Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Join the Conversation

4 Comments

  1. Hi guys, need some help: the purpose of the code below is to output two arrays; the first one initialized using a list, and the second one using a ‘for loop’.
    Problem: the output for the second array gives me unexpected values, insanely large numbers(e.g 1666697), while the output for the first array is fine.

    /*declares.c: declares two integer arrays of length 5*/

    #include
    #define SIZE 5

    int array1[5] = {1,2,3,4,5};
    int array2[SIZE];

    int main(void){

    int i;
    for(i = 0; i < SIZE; ++i){
    array2[i] = i*3;
    }

    printf("narray1 = %d, %d, %d, %d, %d", array1[0], array1[1], array1[2], array1[3], array1[4]);
    printf("narray2 = %d, %d, %d, %d, %dn", array2[i]);

    return 0;
    }

  2. Hi guys, need some help: the purpose of the code below is to output two arrays; the first one initialized using a list, and the second one using a ‘for loop’.
    Problem: the output for the second array gives me unexpected values, insanely large numbers(e.g 1666697), while the output for the first array is fine.

    /*declares.c: declares two integer arrays of length 5*/

    #include
    #define SIZE 5

    int array1[5] = {1,2,3,4,5};
    int array2[SIZE];

    int main(void){

    int i;
    for(i = 0; i < SIZE; ++i){
    array2[i] = i*3;
    }

    printf("narray1 = %d, %d, %d, %d, %d", array1[0], array1[1], array1[2], array1[3], array1[4]);
    printf("narray2 = %d, %d, %d, %d, %dn", array2[i]);

    return 0;
    }

  3. Hi guys, need some help: the purpose of the code below is to output two arrays; the first one initialized using a list, and the second one using a ‘for loop’.
    Problem: the output for the second array gives me unexpected values, insanely large numbers(e.g 1666697), while the output for the first array is fine.

    /*declares.c: declares two integer arrays of length 5*/

    #include
    #define SIZE 5

    int array1[5] = {1,2,3,4,5};
    int array2[SIZE];

    int main(void){

    int i;
    for(i = 0; i < SIZE; ++i){
    array2[i] = i*3;
    }

    printf("narray1 = %d, %d, %d, %d, %d", array1[0], array1[1], array1[2], array1[3], array1[4]);
    printf("narray2 = %d, %d, %d, %d, %dn", array2[i]);

    return 0;
    }

  4. Hi guys, need some help: the purpose of the code below is to output two arrays; the first one initialized using a list, and the second one using a ‘for loop’.
    Problem: the output for the second array gives me unexpected values, insanely large numbers(e.g 1666697), while the output for the first array is fine.

    /*declares.c: declares two integer arrays of length 5*/

    #include
    #define SIZE 5

    int array1[5] = {1,2,3,4,5};
    int array2[SIZE];

    int main(void){

    int i;
    for(i = 0; i < SIZE; ++i){
    array2[i] = i*3;
    }

    printf("narray1 = %d, %d, %d, %d, %d", array1[0], array1[1], array1[2], array1[3], array1[4]);
    printf("narray2 = %d, %d, %d, %d, %dn", array2[i]);

    return 0;
    }

Leave a comment

Your email address will not be published. Required fields are marked *