Vous êtes sur la page 1sur 29

INTRODUCTION TO

COMPUTER
PROGRAMMING
(CSC425)

CHAPTER 7
arrays

CONTENTS
Introduction
Single-Dimensional Arrays

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

INTRODUCTION
Array :
is a collection of a fixed number of components
where in all of the components have the same data
type

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

SINGLE-DIMENSIONAL ARRAYS
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]
int max[4] =
char y[20] =

= to store 5 float value in temp.


to store 4 integer value in max
to store a string of value

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

SINGLE-DIMENSIONAL ARRAYS

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
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

SINGLE-DIMENSIONAL ARRAYS

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];

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

SINGLE-DIMENSIONAL ARRAYS

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.

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

SINGLE-DIMENSIONAL ARRAYS
NOTE:

If what we declare doesnt 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
CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

SINGLE-DIMENSIONAL ARRAYS
Example 3:
How arrays are stored in the computer memory
Array list : int list[10];

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

SINGLE-DIMENSIONAL ARRAYS
Accessing Array component : list[5]=34;

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

10

SINGLE-DIMENSIONAL ARRAYS

Adding components in an array

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

11

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

Processing single-dimensional arrays

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

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

12

Accessing array components

Consider the declaration


int list[100];
int i;

//list is an array of the size 100

This for loop steps-through each element of the array list starting at
the first element
for (i = 0; i < 100; i++)
//process list[i]

//Line 1
//Line 2

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

13

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++)
cin >> list[i];

//Line 1

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

14

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++)
cout << list[i];

//Line 1

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

15

#include<iostream.h>
int main ()
{
double myList[10], yourList[10];
int index;

Example 1

cout<<" Enter 10 numbers :";


for(index=0; index <10 ; index++)
{
cin>>myList[index];
//read component
}
cout<<"\n The components inside the myList array are >> ";
for (index=0; index <10; index++)
{
cout<<myList[index]<< " ";
}

//print component

cout<<"\n\n The numbers inside the yourList array are >> ";
for (index=0; index <10; index++)
{
yourList[index] = myList[index];
//copy component
cout<<yourList[index]<< " ";
}
cout<<"\n\n The numbers in yourList[5] is >> " <<yourList[5];
return 0;
}

Example 2 of Single-Dimensional Arrays

#include <iostream.h>

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

17

Example 2 - Answer

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

18

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

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

19

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

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

20

Arrays as parameters to function


Example 1 :

Constant arrays as formal parameters :

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

21

Arrays as parameters to function


Example 2 :

22

Arrays as parameters to function


Example 2 :

23

Arrays as parameters to function


Example 3 :

24

Arrays as parameters to function


Example 4 :

25

Arrays as parameters to function


Example 4 (contd) :

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

26

Arrays as parameters to function


Example 4 (contd) :

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

27

Arrays as parameters to function


Example 4 (contd) :

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

28

Arrays as parameters to function


Example 4 - Answer :

CSC425 :
INTRODUCTION TO COMPUTER PROGRAMMING

29

Vous aimerez peut-être aussi