CS156

CS156: Intro to C, Part I

Spring 2018

Arrays

See this page as a slide show

CS156 Arrays: Arrays

Some slides from Syracuse University

Multiple Variables of Same Type

Arrays

             homework scores
    ┌───────┬───────┬───────┬───────┐
    │ hw #1 │ hw #2 │ hw #3 │ hw #4 │
    └───────┴───────┴───────┴───────┘

Array Declaration

const

        #define PI 3.14159
        PI = 3.0;        // Wrong wrongity wrong!

Array Manipulation

Array Manipulation - Initialization

The last element in the array is at index N-1, where N is the number of elements in the array.

    #define MAX_NUM_STUDENTS 5
    int grades[MAX_NUM_STUDENTS];
    grades[0] = 98;
    grades[1] = 87;
    grades[2] = 92;
    grades[3] = 79;
    grades[4] = 85;

    ┌───────────┬───────────┬───────────┬───────────┬───────────┐
    │ grades[0] │ grades[1] │ grades[2] │ grades[3] │ grades[4] │
    └───────────┴───────────┴───────────┴───────────┴───────────┘

Array Manipulation - Initialization

Three ways to do the same thing:

    #define MAX_NUM_STUDENTS 5
    int grades[MAX_NUM_STUDENTS] = {98, 87, 92, 79, 85};

    int grades[] = {98, 87, 92, 79, 85};

    int grades[5];
    grades[0] = 98; grades[1] = 87;
    grades[2] = 92; grades[3] = 79;
    grades[4] = 85;

    ┌───────────┬───────────┬───────────┬───────────┬───────────┐
    │    98     │    87     │    92     │    79     │    85     │
    └───────────┴───────────┴───────────┴───────────┴───────────┘
      grades[0]   grades[1]   grades[2]   grades[3]   grades[4]

Array Manipulation

Uninitialized array elements contain unknown values, like all variables.

    #define MAX_NUM_STUDENTS 5
    int grades[MAX_NUM_STUDENTS];
    grades[3] = 10+6+42;

    ┌───────────┬───────────┬───────────┬───────────┬───────────┐
    │    ??     │    ??     │    ??     │    58     │    ??     │
    └───────────┴───────────┴───────────┴───────────┴───────────┘
      grades[0]   grades[1]   grades[2]   grades[3]   grades[4]

Array Manipulation

    // Initialize:
    for (int i = 0; i < MAX_NUM_STUDENTS; i++)
        grades[i] = i*12;
    // Print:
    for(int i = 0; i < MAX_NUM_STUDENTS; i++)
        printf("%d ", grades[i]);

    ┌───────────┬───────────┬───────────┬───────────┬───────────┐
    │     0     │    12     │    24     │    36     │    48     │
    └───────────┴───────────┴───────────┴───────────┴───────────┘
      grades[0]   grades[1]   grades[2]   grades[3]   grades[4]

for Loops and Arrays

    // sum the elements of an array
    #define ARRAY_SIZE 5
    int my_list[ARRAY_SIZE] = {1,2,3,4,5};
    int sum = 0;
    for (int counter=0; counter < ARRAY_SIZE; ++counter)
            sum += my_list[counter];

Array Example, 1/3: Read

    #include <stdio.h>
    #define SIZE 3
    int main() {
        double a[SIZE];
        printf("Enter numbers: ");
        for (int i = 0; i < SIZE; i++)
            scanf("%lf", &a[i]);
        return 0;
    }

    Enter numbers: 1.2 3.4 5.6

Array Example, 2/3: Print

    #include <stdio.h>
    #define SIZE 3
    int main() {
        double a[SIZE] = {1.2, 3.4, 5.6};
        /* Display array elements */
        for (int i = 0; i < SIZE; i++)
                printf("a[%d] = %.2lf\n", i, a[i]);
        return 0;
    }

    a[0] = 1.20
    a[1] = 3.40
    a[2] = 5.60

Array Example, 3/3: Max

    #include <stdio.h>
    #define SIZE 3
    int main() {
        double a[SIZE] = { 1.2, 3.4, 5.6 };
        double max = 0.0;
        /* Find max value in array */
        for (int i = 0; i < SIZE; i++)
            if (a[i] > max)
                    max = a[i];
        printf("max = %.2lf\n", max);
        return 0;
    }

    max = 5.60

Array Example, Combined

    #include <stdio.h>
    #define SIZE 3
    int main() {
        double a[SIZE];
        double max = 0.0;
        printf("Enter the %d array elements\n", SIZE);
        for(int i = 0; i < SIZE; i++)  /* Read the array elements */
            scanf("%lf", &a[i]);
        for(int i = 0; i < SIZE; i++)  /* Display array elements */
            printf("a[%d] = %.2lf\n", i, a[i]);
        for(int i = 0; i < SIZE; i++)  /* Find max value in array */
            if (a[i] > max)
                max = a[i];
        printf("max = %.2lf\n", max);
        return 0;
    }

Array Manipulation

int scores[ ] = { 92, 87, 95 };
for (int i=0; i<=9; i++)
        scanf("%d", &scores[i]);
int grades[3];
grades[53] = 98;
grades[5] = 98;

Arrays as Function Parameters

Passing 1-D Arrays to Functions

Arrays as Function Parameters

    #include <stdio.h>
    #define MAXELEMENTS 5

    double average(double a[], int numberOfElements) ;

    int main() {
        double numbers[MAXELEMENTS] = {1.1, 2.2, 3.3, 4.4, 5.5};
        double ave = average(numbers, MAXELEMENTS);
        printf("%lf\n", ave);
        return 0;
    }

    double average(double a[], int numberOfElements) {
        double sum = 0.0;
        for (int i=0; i<numberOfElements; i++) 
            sum += a[i]; 
        return sum / numberOfElements; 
    }

Passing 1-D Array: Examples

    int sum_list(int list[], int size) {
        int sum=0;
        for (int i=0; i<size; i++)
            sum += list[i];
        return sum;
    }

    int recursive_sum_list(int list[], int size);
        if (size == 1)
            return list[0];
        return list[size-1] + recursive_sum_list(list, size-1);
    }

Passing 1-D Array: Another Example

void change_list(int list[], int size) {
    list[0] = 10;
}
int main() {
    int foo[5] = {1,2,3,4,5};
    printf("before: %d\n", foo[0]);
    change_list(foo, 5);
    printf("after: %d\n", foo[0]);
    return 0;
}
before: 1
after: 10

What gives?

Arrays as Function Parameter

Passing array elements

#include <stdio.h>
void callFunc(int oneval) {
    oneval = 99;
}
int main(){
    int hula[] = {1,2,3,4,5};
    printf("before: %d\n", hula[0]);
    callFunc(hula[0]);
    printf("after: %d\n", hula[0]);
    return 0;
}
before: 1
after: 1

Passing array elements

#include <stdio.h>
void print(int list[], int size)  {
    for (int i=0; i<size; i++)
        printf("%d ", list[i]);
    printf("\n");
}
void  swapFirstLast(int list[], int size)  {
    // What goes here?
}
int main()  {
    #define LIST_SIZE 5
    int my_list[LIST_SIZE] = {1,2,3,4,5};
    print(my_list, LIST_SIZE);
    swapFirstLast(my_list, LIST_SIZE);
    print(my_list, LIST_SIZE);
    return 0;
}

Passing array elements

Array Tips

Array Tips

Modified: 2017-12-19T11:18

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building