Documente Academic
Documente Profesional
Documente Cultură
Arrays
one-dimensional
Multi-dimensional
Arrays
3 4
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)
Arrays can be
initialized partially initialized not initialized
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.
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;
arr[0] arr[1]
85
46
99
23
-12
85
46
99
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): _
Ex
45 40 50 64%
Cw
15 17 25
Total
60 57 75
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)
Same as foo()