Breaking News
Home > Tutorials > C Tutorials > Programming In C: Arrays and Loops
c functions

Programming In C: Arrays and Loops

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

 

About 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.

Check Also

c functions

Programming In C: Functions with Viewer Choice

More posts ↓ This is post #25 of 25 in the series “Programming In C” ...

  • Matrix

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

  • Matrix

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

  • Matrix

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

  • Matrix

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

Like every other website, this site uses cookies to deliver its services, to manage your account and to analyse traffic. By using this website you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close