Vous êtes sur la page 1sur 6

This week

Arrays

one-dimensional

CSE 1020 COMPUTER PROGRAMMING C PROGRAMMING


Week 7

Multi-dimensional

Arrays
3 4

Creating and using arrays


One-dimensional array of three ints:
int arr[3]; int sum; arr[0] = 1; arr[1] = 22; arr[2] = -35; sum = arr[0] + arr[1] + arr[2];

What is an "array"? A way to collect together data of a single type in a single object A linear sequence of data objects e.g.
array of ints array of chars (string)

One-dimensional arrays (1)


5 6

One-dimensional arrays (2)


Examples:
int my_array[10]; /* not initialized */ int my_array[5] = { 1, 2, 3, 4, 5 }; /* initialized */ int my_array[] = { 1, 2, 3, 4, 5 }; /* OK, initialized */ int my_array[4] = { 1, 2, 3, 4, 5 }; /* warning */ int my_array[10] = { 1, 2, 3, 4, 5 }; /* OK, partially initialized */

Arrays can be
initialized partially initialized not initialized

Uninitialized space contains?


"garbage"

One-dimensional arrays (3)


7 8

One-dimensional arrays (4)


Explicit initialization of arrays:
int i; int my_array[10]; for (i = 0; i < 10; i++) { my_array[i] = 2 * i; }

Note on partial initialization:


int my_array[10] = { 1, 2, 3, 4, 5 };

rest of array initialized to 0


int my_array[10];

entire array uninitialized contains garbage

Usually the best approach

One-dimensional arrays (5)


9 10

Using array, write a program that allows a user to enter 10 numbers and display the numbers in reverse order of entry. Using array, write a program that allows a user to enter 10 numbers and display even numbers in order of entry.

Some bad things that can happen...


int my_array[10]; /* What happens here? */ printf("%d\n", my_array[0]); /* What happens here? */ printf("%d\n", my_array[1000]);

No checking! C is an UNSAFE language!

Two-dimensional arrays (1)


11 12

Two-dimensional arrays (2)

int arr[2][3]; /* NOT arr[2, 3] */ int i, j; int sum = 0; arr[0][0] = 1; arr[0][1] = 23; arr[0][2] = -12; arr[1][0] = 85; arr[1][1] = 46; arr[1][2] = 99; /* continued on next slide */

for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) { sum += arr[i][j]; } } Cout<<"sum = <<sum;

Two-dimensional arrays (3)


13 14

Two-dimensional arrays (4)


Conceptual picture of arr:
1 23 -12

Two-dimensional arrays can be split into component one-dimensional arrays:


int arr[2][3]; /* initialize... */ /* arr[0] is array of 3 ints */ /* arr[1] is another array of 3 ints */

arr[0] arr[1]

85

46

99

Two-dimensional arrays (5)


15 16

Two-dimensional arrays (6)


Initializing two-dimensional arrays:

Actual picture of arr: arr[0] arr[1]

23

-12

85

46

99

int my_array[2][3]; /* not initialized */ int my_array[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; /* OK */ int my_array[2][3] = { 1, 2, 3, 4, 5, 6 }; /* warning */

Passing arrays to functions (1)


17 18

Using a 2D array, write a program to input the coursework mark (over 30) and exam mark (over 70) for the following 3 subjects: Maths, English and Computing.
Enter coursework for Maths (max 30): 15 Enter exam marks for Maths (max 70): _

What does this do?


void foo(int i) { i = 42; } /* later... */ int i = 10; foo(i); /* What is i now? */

Then the program displays the following:


Subject
Maths: English: Computing: SPA:

Ex
45 40 50 64%

Cw
15 17 25

Total
60 57 75

Passing arrays to functions (2)


19 20

Passing arrays to functions (3)


Arrays passed to functions can be modified
void foo(int arr[]) { arr[0] = 42; /* modifies array */ } /* later... */ int my_array[5] = { 1, 2, 3, 4, 5 }; foo(my_array); cout<< <<my_array[0]);

Value of i is copied into function Passing a value to a function as an argument doesn't change the value We say that C is a "call-by-value" language But arrays are "different"!
(actually, not really, but it seems like they are; need pointers for full explanation)

Passing arrays to functions (4)


21

Array dimension in declaration is ignored for onedimensional arrays:


void foo2(int arr[5]) /* same as arr[] */ { arr[0] = 42; }

Same as foo()

Vous aimerez peut-être aussi