0% found this document useful (0 votes)
8 views31 pages

Topic 6 - Arrays

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views31 pages

Topic 6 - Arrays

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

CSC402

PROGRAMMING 1
LECTURE: 04 – ARRAYS AND STRINGS
2

Learning Objectives:
After completing this chapter, you will be able to:
• learn about arrays
• explore how to declare and manipulate data into arrays
• understand the meaning of “array index out of bounds”
• become familiar with the restrictions on array processing
• learn about C-strings
• examine the use of string functions to process C-strings
• discover how to input data into—and output data from—a C-string
• learn about parallel arrays
Data Types 3

• A data type is called simple if variables of that


type can store only one value at a time
Arrays 4

• Array - a collection of a fixed number of


components wherein all of the components
have the same data type
• One-dimensional array - an array in which the
components are arranged in a list form
• The general form of declaring a one-
dimensional array is:
dataType arrayName[intExp];

where intExp is any expression that evaluates to a


positive integer
Declaring an array 5

• The statement
int num[5];
declares an array num of 5 components of the
type int
• The components are num[0], num[1],
num[2], num[3], and num[4]
6
Accessing Array Components 7

• The general form (syntax) of accessing an array


component is:
arrayName[indexExp]
where indexExp, called index, is any expression whose
value is a nonnegative integer
• Index value specifies the position of the
component in the array
• The [] operator is called the array subscripting
operator
• The array index always starts at 0
8
10
11
Processing One-Dimensional Arrays 12

• Some basic operations performed on a one-


dimensional array are:
− Initialize
− Input data
− Output data stored in an array
− Find the largest and/or smallest element
• Each operation requires ability to step through
the elements of the array
• Easily accomplished by a loop
Accessing Array Components 13

• Consider the declaration

int list[100]; //list is an array


//of the size 100
int i;

• This for loop steps-through each element of the


array list starting at the first element

for (i = 0; i < 100; i++) //Line 1


//process list[i] //Line 2
Accessing Array Components
14
(continued)
• If processing list requires inputting data into
list
− the statement in Line 2 takes the from of an
input statement, such as the cin statement

for (i = 0; i < 100; i++) //Line 1


cin >> list[i];
16
Array Index Out of Bounds 17

• If we have the statements:


double num[10];
int i;

• The component num[i] is a valid index if i =


0, 1, 2, 3, 4, 5, 6, 7, 8, or 9
• The index of an array is in bounds if the index
>=0 and the index <= ARRAY_SIZE-1
Array Index Out of Bounds
(continued) 18

• If either the index < 0 or the index >


ARRAY_SIZE-1
− then we say that the index is out of bounds

• There is no guard against indices that are out


of bounds
− C++ does not check if the index value is within
range
Array Initialization 19

• As with simple variables


− Arrays can be initialized while they are being declared
• When initializing arrays while declaring them
− Not necessary to specify the size of the array
• Size of array is determined by the number of initial
values in the braces
• For example:
double sales[] = {12.25, 32.50, 16.90, 23,
45.68};
Partial Initialization 20

The statement
int list[10] = {0};
declares list to be an array of 10 components
and initializes all components to zero
The statement
int list[10] = {8, 5, 12};
declares list to be an array of 10 components,
initializes list[0] to 8, list[1] to 5, list[2] to 1
2 and all other components are initialized to 0
Partial Initialization (continued)
21

• The statement
int list[] = {5, 6, 3};
declares list to be an array of 3 components and
initializes list[0] to 5, list[1] to 6, and list[2] to 3
• The statement
int list[25]= {4, 7};
declares list to be an array of 25 components
− The first two components are initialized to 4 and 7
respectively
− All other components are initialized to 0
Restrictions on Array Processing
22

Assignment does not work with arrays

In order to copy one array into another array we must


copy component-wise
Restrictions on Array Processing
(continued) 23
C Strings (Character Arrays) 24

• Character array - an array whose


components are of type char

• String - a sequence of zero or more


characters enclosed in double quote marks

• C stings are null terminated (‘\0’)

• The last character in a string is the null


character
C Strings (Character Arrays)
25
(continued)
• Consider the statement
char name[16];
• Because C strings are null terminated and
name has sixteen components
− The largest string that can be stored in name
is 15
• If you store a string of length, say 10 in name
− The first 11 components of name are used
and the last 5 are left unused
C Strings (Character Arrays)
26
(continued)
• The statement
char name[16] = "John";
declares a string variable name of length 16
and stores "John" in it
• The statement
char name[] = "John";
declares a string variable name of length 5
and stores "John" in it
27
String Comparison 28

• C-strings are compared character by


character using the collating sequence of the
system
• If we are using the ASCII character set
1. The string "Air" is smaller than the string
"Boat"
2. The string "Air" is smaller than the string
"An"
3. The string "Bill" is smaller than the string
"Billy"
4. The string "Hello" is smaller than "hello"
29
Parallel Arrays 30

• Two (or more) arrays are called parallel if


their corresponding components hold related
information

• For example:
int studentId[50];

char courseGrade[50];
THANK YOU…

You might also like