You are on page 1of 45

ARRAYS

Eman Meslhy Mohamed


Last section

+
Control
Structure:
Loop
Control Structure: Loop

 Continue Control Structures:


 Loops
1. While statement

2. do while statement

3. for statement

 The break and continue statements


+
Today section
Arrays
Introduction to arrays

 An Array data structures consisting of related items of the


same type .
 An Array is collection of data storage location , which holds
the same data type .
Array element
Array
name
a 2
0
4
1
6 8
2 3 4
7
Array
index
Array size
 Each storage location is called an element of the array.

 There are tow type of array :


 1-one dimension
rows
 2- tow dimensions
columns
The Purpose of using arrays:

 Some applications require several values in the


memory at the same time; let’s take the example of
counting the number of students whose grades are
above the average in a class of 30 students. This
involves processing the grades two times:
 First time: to compute the average of all grades, and
 Second time: to count the grades above average …
Array declaration

 Before using any array we must declare it .


 Declaration one dimension arrays:

Size of array must be


Positive integer
 Example:
int c [10] ; //array of 10 integers
float d[5] ; //array of 5 float
int x[2] , y[3] ; //declare tow arrays have the same type of int
int a[15.5] ; //ERROR Size
int abc [-3] ; //ERROR Size
int a[5] , a[3] ; // ERROR can not declare tow array with same name
Array Indexes

 The variable making up the array :


 index
 Element
 All array elements have the same name, and the same type.
 The first element in every array has location number 0. The ith element of
array a is referred to as a[i-1].
 A subscript must be an integer, or an integer expression. e.g., a[5] +=20; or
a[3+2] +=20; or if x=2, y=3 _ a[x+y] +=20; All of the previous ways are to
access array elements.
 Bounds of array are from 0 to SIZE-1.
 An array that uses a single subscript is called a one dimensional array
index
Example:
 int a[5]; 0 1 2 3 4
a
int 4 byte
Elements of array

 To refer to a particular location or element in the array, we specify the name of


the array and the position number in brackets [ ] (it is called subscript).

 Example:
a[0] , a[1] , … , a[n-1]

a
a[0] a[1] a[2] a[3] a[4] element

0 1 2 3 4 index
Elements of array

 Array elements like Other variables :


• Assignment 0 1 2 3 4
a[0] =3 ;
• Printing
a 3

cout<<a[0] ; //print 3
• Perform mathematical operation on array element
X=a[3] /2 ; // result is x=5
a
Y=a[0]*a[2] ; //result is y=15
3 2 5 10 1

0 1 2 3 4
• Print sum of three element
cout<<a[0]+a[1]+a[2] ; // result is 10
• Can perform operation inside subscript:
a[5-2] same as a[3]
Array initialization

1- initialization at the same time you declare the array :(At compile
time)
 int a[5]={10 ,20, 25,30 ,40}; a 0
10 20
1 2 3
25 30
4
40

10 20 25 30 40
int a[ ] ={10 ,20, 25,30 ,40};

Compiler will be determine the size from number of the value we give a0 1 2 3 4

int a[5 ] ={10 ,20};


a

10 20 0 0 0
 The first tow element get these value and the rest will get zero

 int a[5]={0} ; a0 0 0
1 2 3
0 0 0
4

 int a[5]={10 ,20, 25,30 ,40,22 ,33 ,55 , 50} ; Error too many elements

X
Cont

 2- give value for each element in array


(directly) :
 (At Run time)
int a[5] ;
a[0] =1;
a[1] =33; a0 1 33 0
1 2 3
4 2
4
a[2] =0;
a[3] =4;
a[4] =2;

OR
int x[3] ;
x[0] =30 ;
x[1] =3*2 ;
x[2] =x[0] – x[1] ;
x 30 6 24
0 1 2
Cont.

2- Reading elements value of declarations from


user:(At RUN time
int a[5] ;
for (int i=0 ; i<5 ; i++)
cin>> a[i] ;

OR
int a[5] ;
for (int i=0 ; i<5 ; i++)
{
cout<<“Enter a [“ << i <<“ ] : “ ;
cin>> a[i] ;
} //end for
Checking bound

 C++ at the runtime doesn't track the size of an array.

output output

garbage value
Cont…

a 0
0 3
1 2
2 7
3 4
8 4
5
2
6

a 0
0
3
1 2
2 7
3 4
8 4
5
2
6

output
Example1

 Find size of array.

output
Example2

 Print array vertically .

output

a 2
0
4
1
6 8
2 3 4
7
Example3

 Print array element Horizontally ” 1”.

output

a 2
0
4
1
6 8
2 3 4
7
Example3

 Print array element Horizontally ” 2”.

output

a 2
0
4
1
6 8
2 3 4
7
Example4

 Print array element Reversed.

output

a 2
0
4
1
6 8
2 3 4
7
Example5

 Sum array element and calculate average .

user

a 0 1 2 3 4
Cont...
Cont…

• Output example5 :
Example6

 Find the maximum and minimum element in array.

user

a 0 1 2 3 4

a
Starting point 0 1 2 3 4

a ma
x
?
mi
n
?
0 1 2 3 4
Cont…
Cont…

• Output example6 :
Example 7

 Multiply array with constant number.

a 0
2
1
5 8
2 3
6
4
3

*K=2

a 4 10 18 12 6

0 1 2 3 4
Cont….

• Output example7 :
Example8

 Multiply tow arrays

a 0
2
1
5
2
8
3
6
4
3

b 0
4
1
3
2
2
3
0
4
10

c 0
2
1
5
2
8
3
6
4
3
Cont…

• Output example8 :
Example9

 Copy one array to other

a 0
7
1
7
2
7
3
7
4
7

b 0 1 2 3 4
Cont…

• Output example9 :
Example10

 Combine tow arrays in third array.

a 0
2
1
2
2
2
3
2 b 0
1 1
1 2
1
3
1
4
1

c 2
0
2
1 2
2 2
3 4
1 1
5
1
6
1
7
1
8
Con…
Cont…

• Output example10 :
Example11

 Create array with size entered from user:

user

a size
Cont…

• Output example11 :
Linear search

?
10 20 30 50 20 50 X 30
0 1 2 3 4 5

?
10 20 30 50 20 50 X
0 1 2 3 4 5

?
10 20 30 50 20 50

0 1 2 3 4 5
Complete program

Output1:

Output2:
Linear search

 Question:
What can we do , if we want to display all the position
of that key , if it occurs more than once
Example12

 Print sum of even element and number.


 Print sum of odd element and number.
 Print sum of positive element and number.
 Print sum of negative element and number.
Cont…
Cont…
Cont….

• Output example12 :
Questions? Comments!

You might also like