Initializing, Indexing, and Passing Arrays

First, let’s review how to declare and define arrays. An array must be declared and defined before it can be used. The size of the array is a constant.

#include <stdio.h>

int main(void){

    //each element in the array
    //must be of the same type
    int testScores[10];
    double gpa[30];

    char name[50];

    //use an index of zero
    //to access the first element
    testScores[0] = 1138;

    printf("%d\n", testScores[0]);

    return 0;
}

The declaration and definition of an array tells the compiler the name of the array, the type of each element, and the number of elements in the array. Since an array depends on having its elements occupy consecutive memory locations, once the array size has been set in cannot be resized, as memory immediately following the location of the last element may have already been allocated.

It is possible to use a variable or an expression as the index of an array.

#include <stdio.h>

int main(void){

int i = 0;
double nums[10];

for(i = 0; i < 10; i++){
nums[i] = i + 1;
nums[i] += i / 7.0;
}

i = 0;

while(i < 10){
printf("%f\t", nums[i++]);
}

return 0;

}//end main()

An array’s name is functionally a symbolic reference to the memory location of the first element in the array. The index represents an offset from the first element in the array to the element being referred to; thus, the first element is at index 0, not 1.

Initialization of all elements in an array can be done at the time of declaration, just as with variables. Our initialization values must be placed within curly braces and, if there is more than one, separated by commas.  If the number of values provided is less than the number of elements in the array, the unassigned elements are filled with zeros.

#include <stdio.h>

int main(void){

    int i = 0;
    int intExmp[10] = {5, 9, 14, 23, 37};

    for(i = 0; i < 10; i++){
        printf("%d\n", intExmp[i]);
    }

    return 0;

}

Note that while we can assign individual elements using the assignment operator, we cannot assign entire arrays in one step. Instead, we must copy arrays by looping through each element. It is also quite common to exchanged array elements; this is a major part of sorting algorithms.

We can pass individual elements to function like any other variable. Of course, the element will be passed by value, not by reference, unless we use pointers.

#include <stdio.h>


void switchValues(int *a, int *b);

int main(void){

    int array01[10];
    int array02[10];

    int i = 0;
    int j = 0;
    int k = 0;

    for(i = 0; i < 10; i++){
        array01[i] = i * 2;
    }

    for(i = 0; i < 10; i++){
        array02[i] = array01[i] + 1;
    }

    for(i = 0; i < 10; i++){
        printf("%3d %3d\n", array01[i], array02[i]);
    }

    //move value stored at beginning of the array
    //down the array to the last element
    for(i = 0; i < 9; i++){
        switchValues(&array02[i], &array02[i+1]);
    }

    for(i = 0; i < 10; i++){
        printf("%5d", array02[i]);
        putchar('\n');
    }

    //reverses the values in the array
    for(i = 0, j = 9; i < 9; i++, j--){
        //each loop we shift the value stored
        //in the first element, 0,
        //to the last element in the array
        //minus one for each time we have looped
        for(k = 0; k < j; k++){
            switchValues(&array01[k], &array01[k+1]);
        }
        int x = 0;
        //print out changes to array so far
        for(x = 0; x < 10; x++){
            printf("%5d", array01[x]);
        }
        putchar('\n');
    } //end outer for loop



    putchar('\n');

    return 0;

}//end main()

void switchValues(int *a, int *b){
    int temp; //temp value to store

    temp = *a;
    *a = *b;
    *b = temp;

}//end switchValues()

Bear in mind that the C language does not check the boundary of an array. Is is our job to ensure that all references to indexed elements are valid and within the boundaries of the array.

#include <stdio.h>

#define ARRAY_MAX 50

int main(void){
    
    int i = 0;
    int numToRead = 0;
    int numbers[ARRAY_MAX];
    
    
    printf("You can enter up to %d whole numbers:\n", ARRAY_MAX);
    printf("How many would you like to enter?\t");
    scanf(" %d", &numToRead);
    
    if(numToRead > 50 || numToRead < 0){
        numToRead = 50;
    }
    
    while(i < numToRead){
        printf("Enter value for element %d:\t", i);
        scanf(" %d", &numbers[i++]);
    }
    
    printf("Printing these numbers backwards:\n");
    
    for(i = numToRead-1; i >= 0; i--){
        printf("%5d ", numbers[i]);
    }
    
    
    
}//end main()

Unlike when passing an array element, when we pass the whole array we pass the array automatically by reference, due to the fact that the array name devolves to a pointer to the initial array element. All we need to do is use the array name as the function’s argument.

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s