Lecture 7 - Arrays
Lecture 7 - Arrays
BIL111 – Programming
Arrays
Feyza M. Hafızoğlu
[email protected]
Bilgisayar Mühendisliği
İstanbul Ticaret Üniversitesi
1
11/13/2023
Arrays
• array: object that stores many values of the same type.
• element: One value in an array.
• index: A 0-based integer to access an element from an array.
index 0 1 2 3 4 5 6 7 8 9
value 12 49 -2 26 5 17 -6 84 72 3
2
11/13/2023
Array declaration
type name[size];
• Example:
int numbers[10];
index 0 1 2 3 4 5 6 7 8 9
value
3
11/13/2023
Accessing elements
name[index] // access
name[index] = value; // modify
• Example:
numbers[0] = 27;
numbers[3] = -6;
printf("%d", numbers[0]);
if (numbers[3] < 0) {
printf("Element 3 is negative.");
}
index 0 1 2 3 4 5 6 7 8 9
value 27 -6
4
11/13/2023
5
11/13/2023
index 0 1 2 3 4
value 12 34 51 0 0
6
11/13/2023
int main() {
int numbers[SIZE];
for (int i = 0; i < SIZE; ++i)
number[i] = 0;
}
Out-of-bounds
• Legal indices: between 0 and the array's size - 1.
• Reading or writing any index outside this range will read garbage
values
• Example:
int data[10] = {0};
printf("%d", data[0]); // okay
printf("%d", data[9]); // okay
printf("%d", data[-1]); // wrong
printf("%d", data[10]); // wrong
index 0 1 2 3 4 5 6 7 8 9
value 0 0 0 0 0 0 0 0 0 0
7
11/13/2023
x 3
index 0 1 2 3 4 5 6 7
numbers value 0 4 11 42 99 0 2 0
8
11/13/2023
Weather question
• Use an array to solve the weather problem:
How many days' temperatures? 7
Day 1's high temp: 45
Day 2's high temp: 44
Day 3's high temp: 39
Day 4's high temp: 48
Day 5's high temp: 37
Day 6's high temp: 46
Day 7's high temp: 53
Average temp = 44.6
4 days were above average.
Weather answer
// Reads temperatures from the user, computes average and # days above average.
int main() {
printf("How many days' temperatures? ");
int days;
scanf("%d", &days);
9
11/13/2023
Limitations of arrays
• You cannot compare arrays with == or equals:
int a1[] = {42, -7, 1, 15};
int a2[] = {42, -7, 1, 15};
if (a1 == a2) { ... } // false!
10
11/13/2023
Arrays as parameters
Swapping values
İnt main() {
int a = 7;
int b = 35;
// swap a with b?
a = b;
b = a;
printf("%d %d", a, b);
}
• What is wrong with this code? What is its output?
11
11/13/2023
Algorithm idea
• Swap pairs of elements from the edges; work inwards:
index 0 1 2 3 4 5
value 11
89 42
0 27
-5 27
-5 42
0 11
89
12
11/13/2023
Flawed algorithm
• What's wrong with this code?
• The loop goes too far and un-reverses the array! Fixed version:
13
11/13/2023
• Example:
// Returns the average of the given array of numbers.
double average(int numbers[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += numbers[i];
}
return (double) sum / n;
}
• Example:
int main() {
// figure out the average TA IQ
int iq[] = {126, 84, 149, 167, 95};
double avg = average(iq, 5);
printf("Average IQ = %lf", avg);
}
...
• Notice that you don't write the [] when passing the array.
14
11/13/2023
Reference semantics
A swap function?
• Does the following swap function work? Why or why not?
int main() {
int a = 7;
int b = 35;
// swap a with b?
swap(a, b);
printf("%d %d", a, b);
}
15
11/13/2023
Value semantics
• value semantics: Behavior where values are copied when
assigned, passed as parameters, or returned.
• All primitive types in C use value semantics.
• When one variable is assigned to another, its value is copied.
• Modifying the value of one variable does not affect others.
int x = 5;
int y = x; // x = 5, y = 5
y = 17; // x = 5, y = 17
x = 8; // x = 8, y = 17
Reference semantics
• reference semantics: Behavior where variables actually
store the address of a variable in memory.
• When one array is assigned to another, the array is
not copied; both variables refer to the same array.
• Modifying the value of one variable will affect others.
16
11/13/2023
17
11/13/2023
• Solution:
void reverse(int numbers[], int n) {
for (int i = 0; i < n / 2; i++) {
int temp = numbers[i];
numbers[i] = numbers[n - 1 - i];
numbers[n - 1 - i] = temp;
}
}
18
11/13/2023
19
11/13/2023
A multi-counter problem
• Problem: Write a function mostFrequentDigit that
returns the digit value that occurs most frequently in a
number.
20
11/13/2023
A multi-counter problem
• We could declare 10 counter variables ...
int counter0, counter1, counter2, counter3, counter4,
counter5, counter6, counter7, counter8, counter9;
index 0 1 2 3 4 5 6 7 8 9
value 1 0 2 0 0 0 4 1 0 1
index 0 1 2 3 4 5 6 7 8 9
value 1 0 2 0 0 0 4 1 0 0
21
11/13/2023
Tally solution
// Returns the digit value that occurs most frequently in n.
// Breaks ties by choosing the smaller value.
int mostFrequentDigit(int n) {
int counts[10] = {0};
while (n > 0) {
int digit = n % 10; // pluck off a digit and tally it
counts[digit]++;
n = n / 10;
}
// find the most frequently occurring digit
int bestIndex = 0;
for (int i = 1; i < 10; i++) {
if (counts[i] > counts[bestIndex]) {
bestIndex = i;
}
}
return bestIndex;
}
22
11/13/2023
#define SIZE 20
. . .
// output strings
printf("string1 is: %s\nstring2 is: %s\n", string1,
string2);
23
11/13/2023
Multi-dimensional arrays
• An array of arrays, the elements of which are accessed with
multiple integer indexes.
• double: one double
• double[]: a one-dimensional array of doubles
• double[][]: a two-dimensional grid of doubles
• double[][][]: a three-dimensional collection of doubles
• Example:
double temps[3][5];
24
11/13/2023
• Example:
temps[0][3] = 98.3;
temps[2][0] = 99.4;
25