Title: Arrays
1Arrays
Mar. 4, 2008
2News
- Midterm Exam
- Mid-semester Grades
- Tutoring
- Spring break next week!
3Arrays
42 Kinds of Data Types
'h'
'e'
'a'
'l'
'l'
Simple (char)?
'o'
\0
Complex (string)?
5Complex Data Type
This complex data type can be thought of as a
list or array of simple data elements. This
particular complex data type is a string an
array of characters.
'h'
'e'
'l'
'l'
'o'
\0
6Other Arrays
- We saw that arrays of characters are called
strings - We can also have arrays of other simple data
types - Arrays of ints, floats, doubles, etc.
- These are arrays of numbers (the string functions
won't work on them)?
7Integer Arrays
Define the length of the array of integers. Each
slot in the array will store a single student's
age.
define ClassSize 10 ... int i 0 int
studentAgesClassSize for( i 0 i lt
ClassSize i )? printf( Enter age of
student d , i 1) scanf( d,
studentAgesi )
8Integer Arrays
Give the data type of the array here. Remember
we used char for strings.
define ClassSize 10 ... int i 0 int
studentAgesClassSize for( i 0 i lt
ClassSize i )? printf( Enter age of
student d , i 1) scanf( d,
studentAgesi )
9Integer Arrays
Here is the name of the array. I like to make
these variable names plural to indicate that a
lot of values are being stored.
define ClassSize 10 ... int i 0 int
studentAgesClassSize for( i 0 i lt
ClassSize i )? printf( Enter age of
student d , i 1) scanf( d,
studentAgesi )
10Integer Arrays
Then we have to define the size of the array
inside . I use a constant to make it easy.
define ClassSize 10 ... int i 0 int
studentAgesClassSize for( i 0 i lt
ClassSize i )? printf( Enter age of
student d , i 1) scanf( d,
studentAgesi )
11Integer Arrays
Only strings have null terminators. They are
required for the string functions to work.
Arrays of numbers have a real value in the last
index slot.
define ClassSize 10 ... int i 0 int
studentAgesClassSize for( i 0 i lt
ClassSize i )? printf( Enter age of
student d , i 1) scanf( d,
studentAgesi )
12Integer Arrays
define ClassSize 10 ... int i 0 int
studentAgesClassSize for( i 0 i lt
ClassSize i )? printf( Enter age of
student d , i 1) scanf( d,
studentAgesi )
A for loop is a convenient way to work with
arrays. We can define the for loop header to go
from 0 (the first index) to 1 less than the size
of the array (the last index).
13Integer Arrays
define ClassSize 10 ... int i 0 int
studentAgesClassSize for( i 0 i lt
ClassSize i )? printf( Enter age of
student d , i 1) scanf( d,
studentAgesi )
To scan a value into the array, you have to give
the index of where you want to put the scanned
value. Using a loop, the current element that
we're working with is denoted by studentAgesi.
14studentAges array
Index
19
0
21
1
20
2
20
...
18
20
15Accessing Array Values
for( i 0 i lt ClassSize i
)? printf( Student d is d\n , i 1,
studentAgesi )
16Accessing Array Values
for( i 0 i lt ClassSize i
)? printf( Student d is d\n , i 1,
studentAgesi )
Access a single integer in the array by using
and its index. Just like accessing a single
character in a string.
17Manually Changing a Value
studentAges4 21 studentAgesx
19
Set the age of student at index 4 (the 5th
student) to be 21.
18Manually Changing a Value
studentAges4 21 studentAgesx
19
Set the age of student at index x to be 19.
19Changing All Values
for( i 0 i lt ClassSize i
)? studentAgesi 0
Reset every student age to be 0.
20Comparing Array Values
for( i 0 i lt ClassSize i )? if(
studentAgesi gt 35 )? printf( Student
d can run for president.\n, i 1)
21Comparing Array Values
Compare each student age with the minimum age to
run for president.
for( i 0 i lt ClassSize i )? if(
studentAgesi gt 35 )? printf( Student
d can run for president.\n, i 1)
22Comparing Array Values
for( i 0 i lt ClassSize i )? if(
studentAgesi gt 35 )? printf( Student
d can run for president.\n, i 1)
We'll add 1 because humans like lists to start
with the value of 1 instead of 0.
23Initializing an Array
define ClassSize 4 int i 0
float studentGPAsClassSize 3.8, 4.0, 3.4,
2.9 for( i 0 i lt ClassSize i
)? printf( Student d has GPA of .2f\n,
i 1, studentGPAsi)
24Initializing an Array
define ClassSize 4 int i 0
float studentGPAsClassSize 3.8, 4.0, 3.4,
2.9 for( i 0 i lt ClassSize i
)? printf( Student d has GPA of .2f\n,
i 1, studentGPAsi)
Use to define the initial values of an array.
25Initializing an Array
If you leave this out, then the array is
automatically sized to fit all the initial values
(just like for strings when we give an initial
string value).
define ClassSize 4 int i 0
float studentGPAsClassSize 3.8, 4.0, 3.4,
2.9 for( i 0 i lt ClassSize i
)? printf( Student d has GPA of .2f\n,
i 1, studentGPAsi)
26Initializing an Array
define ClassSize 4 int i 0
float studentGPAsClassSize 3.8, 4.0, 3.4,
2.9 for( i 0 i lt ClassSize i
)? printf( Student d has GPA of .2f\n,
i 1, studentGPAsi)
The array already has values from the
initialization, so we can just print them out now.
27Multidimensional Arrays
28Storing Tables
Single Array/List
A Table Each row is an array. There are a bunch
of rows
29Storing Tables
Single Array/List
Multidimensional Array Each element of the array
is itself another array!
30Storing Tables
Multidimensional Array Two indexes are needed.
One indexes the row. The other indexes the
column.
Single Array/List
31Multidimensional Array
define TableSize 4 int i 0 int j
0 int multiplicationTableTableSizeTabl
eSize
32Multidimensional Array
define TableSize 4 int i 0 int j
0 int multiplicationTableTableSizeTabl
eSize
Start with the data type of the table elements.
33Multidimensional Array
define TableSize 4 int i 0 int j
0 int multiplicationTableTableSizeTabl
eSize
Then the name of the table variable.
34Multidimensional Array
define TableSize 4 int i 0 int j
0 int multiplicationTableTableSizeTabl
eSize
Now there are two sets of . The first define
the number of rows in the table.
35Multidimensional Array
The second is the number of columns in the table
(or the length of each row). For this example we
have a square table, so both dimensions are equal.
define TableSize 4 int i 0 int j
0 int multiplicationTableTableSizeTabl
eSize
36Multidimensional Array
for( i 0 i lt TableSize i )?
for( j 0 j lt TableSize j )?
multiplicationTableij i j
37Multidimensional Array
for( i 0 i lt TableSize i )?
for( j 0 j lt TableSize j )?
multiplicationTableij i j
Use nested for loops. The outer loop counts down
the rows.
38Multidimensional Array
for( i 0 i lt TableSize i )?
for( j 0 j lt TableSize j )?
multiplicationTableij i j
The inner loop counts across the columns.
39Multidimensional Array
for( i 0 i lt TableSize i )?
for( j 0 j lt TableSize j )?
multiplicationTableij i j
Use both indexes to say which row and which
column element you want to access.
40Arrays of Strings
41Array of Strings
- We can have an array of strings by using a
multidimensional array of characters - Each row is then a whole string by itself
- Since a row is an array of characters
- Arrays of strings are very common and useful
42Array of Strings
define MaxStringLength 32 define
ClassSize 4 char studentNamesClassSizeMa
xStringLength Tracy, Stacy, Macy
, Gracy
43Array of Strings
define MaxStringLength 32 define
ClassSize 4 char studentNamesClassSizeMa
xStringLength Tracy, Stacy, Macy
, Gracy
The first dimension is the number of students (or
student names). The second dimension is the
maximum length of each name.
44Array of Strings
define MaxStringLength 32 define
ClassSize 4 char studentNamesClassSizeMa
xStringLength Tracy, Stacy, Macy
, Gracy
We can initialize the array of student names like
this.
45Array of Strings
printf( The first student name is s\n,
studentNames0 )
Use s to print out a single string.
46Array of Strings
printf( The first student name is s\n,
studentNames0 )
Using studentNames0 accesses a single row of
the multidimensional array. A single row is a
string. In this case it's the first student's
name.
47Finding a String in an Array
char targetStudentMaxStringLength
printf( Who are you looking for?\n ) scanf(
s, targetStudent ) for( i 0 i lt
ClassSize i )? if( targetStudent
studentNamesi )? printf( Found the
student at index d!\n, i )
48Finding a String in an Array
char targetStudentMaxStringLength
printf( Who are you looking for?\n ) scanf(
s, targetStudent ) for( i 0 i lt
ClassSize i )? if( targetStudent
studentNamesi )? printf( Found the
student at index d!\n, i )
Does NOT work!
49Finding a String in an Array
char targetStudentMaxStringLength
printf( Who are you looking for?\n ) scanf(
s, targetStudent ) for( i 0 i lt
ClassSize i )? if( strcmp(targetStudent,
studentNamesi ) 0 )? printf( Found
the student at index d!\n, i )
Instead, use the strcmp function from string.h to
compare strings. The two parameters are just the
two strings you want to compare.
50Finding a String in an Array
char targetStudentMaxStringLength
printf( Who are you looking for?\n ) scanf(
s, targetStudent ) for( i 0 i lt
ClassSize i )? if( strcmp(targetStudent,
studentNamesi ) 0 )? printf( Found
the student at index d!\n, i )
You compare the value of strcmp to 0. If it is
0, then the strings were the same.
51Parallel Arrays
52Parallel Arrays
- Often we want to store lots of values that are
grouped together. - E.g. we might want to store a student's name,
gpa, and age. - One way to do this is by using parallel arrays.
- Each array stores one kind of value either
names, gpas, or ages - Index 0 in each array corresponds to a single
student
53Parallel Arrays
char studentNamesClassSizeMaxStringLength
Tracy, Stacy, Macy, Gracy
float studentGPAsClassSize 3.4, 3.5,
3.6, 3.9 int studentAgesClassSize 18,
19, 19, 21
54Parallel Arrays
char studentNamesClassSizeMaxStringLength
Tracy, Stacy, Macy, Gracy
float studentGPAsClassSize 3.4, 3.5,
3.6, 3.9 int studentAgesClassSize 18,
19, 19, 21
We have 3 parallel arrays. Tracy is the name of
the first student.
Tracy's GPA is 3.4.
Tracy is 18 years old.
55Parallel Arrays
for( i 0 i lt ClassSize i )?
printf( Student d is s\n, i1,
studentNamesi ) printf( \tGPA of .2f\n,
studentGPAsi ) printf( \td years
old.\n\n, studentAgesi )