Arrays - PowerPoint PPT Presentation

1 / 55
About This Presentation
Title:

Arrays

Description:

'Macy', 'Gracy' Array of Strings #define MaxStringLength 32 #define ClassSize 4 ... 'Macy', 'Gracy' The first dimension is the number of students (or student names) ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 56
Provided by: csInd
Category:
Tags: arrays | marcy

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
Mar. 4, 2008
2
News
  • Midterm Exam
  • Mid-semester Grades
  • Tutoring
  • Spring break next week!

3
Arrays
4
2 Kinds of Data Types
'h'
'e'
'a'
'l'
'l'
Simple (char)?
'o'
\0
Complex (string)?
5
Complex 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
6
Other 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)?

7
Integer 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 )
8
Integer 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 )
9
Integer 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 )
10
Integer 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 )
11
Integer 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 )
12
Integer 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).
13
Integer 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.
14
studentAges array
Index
19
0
21
1
20
2
20
...
18
20
15
Accessing Array Values
for( i 0 i lt ClassSize i
)? printf( Student d is d\n , i 1,
studentAgesi )
16
Accessing 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.
17
Manually Changing a Value
studentAges4 21 studentAgesx
19
Set the age of student at index 4 (the 5th
student) to be 21.
18
Manually Changing a Value
studentAges4 21 studentAgesx
19
Set the age of student at index x to be 19.
19
Changing All Values
for( i 0 i lt ClassSize i
)? studentAgesi 0
Reset every student age to be 0.
20
Comparing Array Values
for( i 0 i lt ClassSize i )? if(
studentAgesi gt 35 )? printf( Student
d can run for president.\n, i 1)
21
Comparing 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)
22
Comparing 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.
23
Initializing 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)
24
Initializing 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.
25
Initializing 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)
26
Initializing 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.
27
Multidimensional Arrays
28
Storing Tables
Single Array/List
A Table Each row is an array. There are a bunch
of rows
29
Storing Tables
Single Array/List
Multidimensional Array Each element of the array
is itself another array!
30
Storing Tables
Multidimensional Array Two indexes are needed.
One indexes the row. The other indexes the
column.
Single Array/List
31
Multidimensional Array
define TableSize 4 int i 0 int j
0 int multiplicationTableTableSizeTabl
eSize
32
Multidimensional Array
define TableSize 4 int i 0 int j
0 int multiplicationTableTableSizeTabl
eSize
Start with the data type of the table elements.
33
Multidimensional Array
define TableSize 4 int i 0 int j
0 int multiplicationTableTableSizeTabl
eSize
Then the name of the table variable.
34
Multidimensional 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.
35
Multidimensional 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
36
Multidimensional Array
for( i 0 i lt TableSize i )?
for( j 0 j lt TableSize j )?
multiplicationTableij i j

37
Multidimensional 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.
38
Multidimensional 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.
39
Multidimensional 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.
40
Arrays of Strings
41
Array 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

42
Array of Strings
define MaxStringLength 32 define
ClassSize 4 char studentNamesClassSizeMa
xStringLength Tracy, Stacy, Macy
, Gracy
43
Array 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.
44
Array 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.
45
Array of Strings
printf( The first student name is s\n,
studentNames0 )
Use s to print out a single string.
46
Array 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.
47
Finding 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 )
48
Finding 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!
49
Finding 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.
50
Finding 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.
51
Parallel Arrays
52
Parallel 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

53
Parallel Arrays
char studentNamesClassSizeMaxStringLength
Tracy, Stacy, Macy, Gracy
float studentGPAsClassSize 3.4, 3.5,
3.6, 3.9 int studentAgesClassSize 18,
19, 19, 21
54
Parallel 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.
55
Parallel 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 )
Write a Comment
User Comments (0)
About PowerShow.com