Cs1010e Lecture05 PDF
Cs1010e Lecture05 PDF
Arrays
Semester 1 2015 / 2016
Lecture Outline
Motivation
What is an Array
1D array and 2D arrays:
Declaration and access
Initialization
Problem solving with arrays
Motivation
Motivation
I have a set of daily temperature readings
collected over 3 days and I want to write a
program to find the median temperature
(assuming all the readings are unique)
How to represent these set of temperature
readings in my program?
Motivation
Write a program using 3 floating point variables
#include <stdio.h>
int main(void) {
double r1, r2, r3;
scanf("%lf",&r1);
scanf("%lf",&r2);
scanf("%lf",&r3);
return 0;
Motivation
But how to find the median after I have
captured the readings?
Motivation
But how to find the median after I have
captured the readings?
int main(void) {
double r1, r2, r3;
scanf("%lf",&r1);
scanf("%lf",&r2);
scanf("%lf",&r3);
return 0;
}
Motivation
What if I have the readings for a week?
What if I have the readings for a month?
What if I have the readings for a year?
My program is not scalable!
Motivation
Need a way to represent the multiple
temperature readings as a collection in my
program and be able to manipulate the
collection efficiently
This is where arrays come in !
What is an Array?
10
What is an Array?
An array is a fixed-sized sequential collection of
data of the same type.
E.g array of integers, array of floating points etc.
11
What is an Array?
First element
(at lowest memory address)
100
Last element
(at highest memory address)
231
500
12
13
Declaring 1D Array
Declaration format
Type arrayName[arraySize];
Type int, double
arrayName variable name of 1D array
arraySize An expression that evaluates to an
integer indicating the size of the array
14
Declaring 1D Array
Fixed-sized Array
arraySize is a constant expression, and is known
during compile time
Example
#define M 10;
int main(void) {
double array1[7];
double array2[M];
15
16
32.0
25.8
27.3
28.1
25.0
33.3
30.9
int curIndex = 3;
double t = 10.0;
printf("%f\n",tempReading[0]);
printf("%f\n",tempReading[1]);
printf("%f\n",tempReading[6]);
printf("%f\n",tempReading[curIndex+2]);
t = t + tempReading[curIndex+2]; /* t==43.3 */
Used in an expression
like any other variable
18
32.0
25.8
27.3
28.1
25.0
33.3
30.9
43.3
int curIndex = 3;
double t = 10.0;
tempReading[curIndex+2] = t + tempReading[curIndex+2];
Assign the value of tempReading[curIndex+2] just like any other variable
19
1D Array Initialization
20
Initializing 1D Array
In most cases, an array is created with unknown
values in it
Usually we want the array to be filled with some
valid starting value(s)
Initialization is the process of specifying the
starting values in an array
21
Initializing 1D Array
Using initialization lists
Can only be used during declaration of the array
Initialization list
tempReading
32.0
25.8
27.3
28.1
25.0
33.3
30.9
22
Initializing 1D Array
Cannot perform declaration style initialization
after the declaration of an array
double tempReading[7];
tempReading = {32.0, 25.8, 27.3, 28.1, 25.0, 33.3, 30.9};
23
Initializing 1D Array
Initialization list may be shorter than the size of
the array (rest of entries initialized to 0)
double tempReading[7] = {32.0, 25.8};
tempReading
32.0
25.8
0.0
0.0
0.0
0.0
0.0
25
26
27
29
Home Work:
Try and fill it from
right to left !
30
31
}
32
Alternative
return 0;
}
33
return 0;
}
34
35
scanf("%d",&numReadings);
for (i = 0; i < numReadings; i++) {
scanf("%lf",&tempReading[i]);
}
medianPos = (numReadings+1)/2;
i = -1;
do {
numBiggerEq = 0;
i = i + 1;
for (j = 0; j < numReadings; j++)
if (tempReading[i] >= tempReading[j])
numBiggerEq += 1;
} while (numBiggerEq != medianPos)
printf("median = %f \n",tempReading[i]);
return 0;
}
36
37
2D Array
declaration and access
38
2D Array
2D arrays are useful when representing a
table/matrix of data
Each element is stored in a particular cell
referenced by row followed by column
(row,col)
0
Columns
1
Rows 1
4 is at cell (1,2)
39
Declaring 2D array
Declaration
Type arrayName[rowSize][colSize];
40
Declaring 2D Array
E.g
Can store a total of 4*3 = 12 integers
int t[4][3];
41
42
2-by-3 array
2nd row
45
2D Array
Initialization
46
2D Array Initialization
Initialization using initialization list
Can only be done during declaration
Initializer/Initializer sequence
47
2D Array Initialization
If the initializer sequence is shorter than the
array, the rest is initialized to zero
To zero the entire array
int t[4][3] = {{0}};
48
49
51
52
Home Work:
Try and fill it from
right to left,
bottom to top !
}
54
Original array
55
Original array
56
Original array
57
Original array
58
Original array
59
Original array
60
Original array
61
Original array
62
63
64
65
Pitfalls in [] usage
Differentiate the use of [] in
Declarations
Statements
66
Lecture Summary
1D arrays
Declaration/initialization with pre-determined size
Subscripting to assess individual elements
Loops to access array elements
67