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

Constants via #define

#define PI 3.1415926
printf("%f\n", PI);
3.141593
#define PI 3.1415926
PI = 3.0;
c.c: In function 'main':
c.c:2: error: lvalue required as left operand of assignment

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

#define MAX_NUM_STUDENTS 5
int grades[MAX_NUM_STUDENTS];
for (int i = 0; i < MAX_NUM_STUDENTS; i++)
    grades[i] = i*12;
for (int i = 0; i < MAX_NUM_STUDENTS; i++)
    printf("%d ", grades[i]);
0 12 24 36 48 


    ┌───────────┬───────────┬───────────┬───────────┬───────────┐
    │     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];
printf("sum=%d\n", sum);
sum=15

Array Example, 1/3: Read

    #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

    #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

    #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

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

Indices Out of Range

int scores[] = {1,2,3};
for (int i=0; i<20; i++)
    printf("%d ", scores[i]);
1 2 3 3 0 0 -1127013419 32719 0 0 1932948376 32766 0 1 4195629 0 0 0 -409635595 -1042723435 

Sometimes, you get away with it.

Sometimes, you get away with breaking the rules:

int a[10];
a[12] = 456;
printf("%d\n", a[12]);
456

Sometimes, you don't:

int a[10];
a[1000006] = 789;
printf("%d\n", a[1]);
SIGSEGV: Segmentation fault

Arrays as Function Parameters

Passing 1-D Arrays to Functions

Arrays as Function Parameters

#define MAXELEMENTS 5

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

int main() {
    double numbers[MAXELEMENTS] = {1.2, 3.4, 5.6, 7.8, 9.0};
    printf("%lf\n", average(numbers, MAXELEMENTS));
    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; 
}
5.400000

Passing 1-D Array: Examples

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

int main() {
    int a[] = {11, 22, 33, 44, 55};
    printf("total: %d\n", sum(a, 5));
    return 0;
}
total: 165

Passing 1-D Array: Examples

A recursive function to add an array:

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

int main() {
    int a[] = {11, 22, 33, 44, 55};
    printf("total: %d\n", sum(a, 5));
    return 0;
}
total: 165

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

Arrays as Function Parameter

Passing array elements

void foo(int oneval) {
    oneval = 99;
}

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

Passing array elements

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

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-03-08T14:04

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building