You are on page 1of 41

Introduction To

Computer Programming
(CSC425)

by
PUAN AFIZA ISMAIL
Faculty of Computer & Mathematical Sciences
UiTM MALAYSIA
2013

1
Array
Chapter 7

2
Contents

7.1 Introduction
7.2 One dimensional array
- processing one dimensional array
- accessing array components
- arrays as parameters to function
7.3 C string (character arrays)
- string comparison
- reading and writing string

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 3


PROGRAMMING
7.1
Introduction
 Array :
is a collection of a fixed number of components
wherein all of the components have the same data
type

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 4


PROGRAMMING
7.2
One dimensional array
 Is used to store multiple value in a single identifier.
 General array declaration syntax:

data-type array name [number of items]

For example:
 float temp[5] = to store 5 float value in
temp.
 int max[4] = to store 4 integer
value in max
 char y[20] = to store a string
October 1, 2013 of value CSC425 : INTRODUCTION TO COMPUTER 5
PROGRAMMING
7.2
One dimensional array (cont.)

 Each item in an array is called an element or


component.
 Every item has its position in an array, this position is
called index or subscript; always start at 0.
 Example :
 int num[10] store 10 data.
Note : int num[] error!
 int num[]={1,2,3} not error.
num[0] refer to the first data
num[1] refer to the second data
num[2] refer to the third data

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 6


PROGRAMMING
7.2
One dimensional array (cont.)

 Input & output :

int num[100] // declaration


cin >> num[20]; // to enter the 21th value
cout << num[20]; // to display the 21th
value

//using looping:
for(int i=0; i<5; i++)
cout << num[i];

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 7


PROGRAMMING
7.2
One dimensional array (cont.)

 Input & output :

Array can be initialized within their declaration:


int temp[5]={1,2,3,4,5};
char codes[6]={‘s’,’a’,’m’,’p’,’l’,’e’};
or
char codes[6]=“sample”;
Note : a string is terminated with a special sentinel,“\0”
the null character.

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 8


PROGRAMMING
7.2
One dimensional array (cont.)

 NOTE:
 If what we declare doesn’t fulfill what the size of the array wants,
for example:
int max[6]={1,2,3}
 we only declare three but the array wants 6; the compiler will
assume the next three integer are zero :
“ 1,2,3,0,0,0 “
 If what we declare exceeds what the array wants, for example:
int max[3]={1,2,3,4,5,6,7,8}
 the array wants three but we declare 8” the compiler will only take
the first three integer :
“1,2,3”
October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 9
PROGRAMMING
7.2
One dimensional array (cont.)

Array list : int list[10];

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 10


PROGRAMMING
7.2
One dimensional array (cont.)

Accessing Array component : list[5];

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 11


PROGRAMMING
7.2
One dimensional array (cont.)

List[3] = 10;
List[6] = 35;
List[5] = list[3] + list[6];

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 12


PROGRAMMING
7.2
One dimensional array (cont.)

PROCESSING ONE DIMENSIONAL ARRAY :


 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

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 13


PROGRAMMING
7.2
One dimensional array (cont.)

ACCESSING ARRAY COMPONENTS :


 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

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 14


PROGRAMMING
7.2
One dimensional array (cont.)

ACCESSING ARRAY COMPONENTS :

 If processing list requires inputting data into list :

 the statement in Line 2 takes the form of an input


statement, such as the cin statement

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


cin >> list[i];

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 15


PROGRAMMING
7.2
One dimensional array (cont.)

ACCESSING ARRAY COMPONENTS :

 If processing list requires printing data from an array:

 the statement in Line 2 takes the form of an output


statement, such as the cout statement

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


cout << list[i];

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 16


PROGRAMMING
7.2
One dimensional array (cont.)

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 17


PROGRAMMING
7.2
One dimensional array (cont.)

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 18


PROGRAMMING
7.2
One dimensional array (cont.)

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 19


PROGRAMMING
7.2
One dimensional array (cont.)
ARRAYS AS PARAMETERS TO FUNCTION :

 Arrays are passed by reference only

 The symbol & is not used when declaring an array as a


formal parameter

 The size of the array is usually omitted

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 20


PROGRAMMING
7.2
One dimensional array (cont.)
ARRAYS AS PARAMETERS TO FUNCTION :

 If the size of one-dimensional array is specified when it is


declared as a formal parameter

 It is ignored by the compiler

 The reserved word const in the declaration of the


formal parameter can prevent the function from changing
the actual parameter

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 21


PROGRAMMING
7.2
One dimensional array (cont.)
Example :

Constant arrays as formal parameters :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 22


PROGRAMMING
7.2
One dimensional array (cont.)
Example :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 23


PROGRAMMING
7.2
One dimensional array (cont.)
Example :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 24


PROGRAMMING
7.2
One dimensional array (cont.)
Example :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 25


PROGRAMMING
7.2
One dimensional array (cont.)
Example :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 26


PROGRAMMING
7.2
One dimensional array (cont.)
Example :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 27


PROGRAMMING
7.2
One dimensional array (cont.)
Example :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 28


PROGRAMMING
7.2
One dimensional array (cont.)
Example :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 29


PROGRAMMING
7.2
One dimensional array (cont.)
Example :

October 1, 2013 CSC425 : INTRODUCTION TO COMPUTER 30


PROGRAMMING
7.3
C Strings (Character Arrays)
 Character array - an array whose
components are of type char

 String - a sequence of zero or more


characters enclosed in double quote marks

 C strings are null terminated (‘\0’)

 The last character in a string is the null


character
7.3
C Strings (Character Arrays)
 There is a difference between 'A' and "A"
 'A' is the character A

 "A" is the string A

 Because strings are null terminated, "A"


represents two characters, 'A' and '\0‘

 Similarly, "Hello" contains six characters,


'H', 'e', 'l', 'l', 'o', and '\0'
7.3
C Strings (Character Arrays)
 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
7.3
C Strings (Character Arrays)
 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
7.3
C Strings (Character Arrays)
7.3
String Comparison
 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"
Reading and Writing Strings
 String Input

 Aggregate operations are allowed for string


input

 cin >> name; stores the next input string in


name

 Strings that contain blanks cannot be read using


the extraction operator >>
Reading and Writing Strings
(continued)
 To read strings with blanks, use the get
function with an input stream variable, such as
cin:

cin.get(str, m+1);

 Stores the next m characters into str but the


newline character is not stored in str

 If the input string has fewer than m characters,


the reading stops at the newline character
Reading and Writing Strings
(continued)
 String Output
 The statement cout << name; outputs the
content of name on the screen
 The insertion operator << continues to write the
contents of name until it finds the null character
 If name does not contain the null character, then
we will see strange output: << continues to output
data from memory adjacent to name until '\0' is
found
Reading and Writing Strings
(continued)

You might also like