You are on page 1of 58

ARRAYS

Syed M Hamedoon (Assistant Professor)


Definition of Arrays
An array is a sequence of objects of same data
type. The objects in an array is called the
elements of array.
• An array is represented in the computer
memory by consecutive group of storage
locations
• Each element in the array is referenced by its
position in the array

Syed M Hamedoon (Assistant Professor)


Types of Array
There are two types of Array
• One dimensional array
• Multi dimensional array

Syed M Hamedoon (Assistant Professor)


One dimensional Array
• One dimensional array is also known as a list of
linear array. It consists of only one column or
one row.
Array Name Temp
Array Array Name Array size= 5
location Temp[5]
Number of elements
Temp [0] 22 array =5
Temp[1] 24
Tamp[0]= location 0 of
Temp[2] 18 array Temp
Temp[3] 6
Temp[0]=22;
Temp[4] 32 Element on the array
location 0 is 22
Syed M Hamedoon (Assistant Professor)
Syntax of Array representation
Data type array_name [array size]

Example
int abc [5]; // array name abc of size 5
abc[0]=2;
abc[1]=4;
float b[3];

Syed M Hamedoon (Assistant Professor)


Program 4.01
#include<iostream>
using namespace std;
main ( )
{
float a[5];
int i;
a[0]=9.9; // cout<<a[0];
a[1]=12.9; // cout<<a[1];
a[2]=13.1;
a[3]=8.9;
a[4]=10.6;
for(i=0;i<=4;i++)
cout<<“value in a[“<<i <<“]=“<<a[i]<<endl;// value in a[0]=9.9;
}

Syed M Hamedoon (Assistant Professor)


Program 4.2
Write a program to enter any five values from user in
an array and print the reverse values on the output
screen

#include<iostream>
using namespace std;
int main ()
{

Syed M Hamedoon (Assistant Professor)


{
int abc[5],i;
for(i=0;i<=4;i++)
{
cout<<“Enter the value in element”<<i<<endl;
cin>>abc[i];
}
cout<<Output in reverse order”<<endl;
for(i=4;i>=0;i--)
{
cout<<“Value in a[“<<i<<“]”<<abc[i]<<endl;
}
Syed M Hamedoon (Assistant Professor)
for(i=0;i<=4;i++)
{
cout<<“Enter the value in element”<<i<<endl; Program 4.2
cin>>abc[i]; (Understanding)
}
abc [i]

abc[0] 1

abc[1] 2

abc[2] 3

abc[3] 4

abc[4] 5

Syed M Hamedoon (Assistant Professor)


cout<<Output in reverse order”<<endl;
Program 4.2
for(i=4;i>=0;i--)
(Understanding)
{
cout<<“Value in a[“<<i<<“]”<<abc[i]<<endl;
}
abc [i]

abc[0] 1

abc[1] 2

abc[2] 3

abc[3] 4

abc[4] 5

Syed M Hamedoon (Assistant Professor)


Program 4.3
Write a program in C++ to input data in to an array of 5
elements. Calculate the sum and average of the elements
and then print the sum and average on the screen
#include<iostream>
using namespace std;
int main ()
{
float abc[5],sum, avg;
int i;

Syed M Hamedoon (Assistant Professor)


for(i=0;i<=4;i++)
{
cout<<“Enter value in element”<<i<<endl;
cin>>abc[i];
}
sum=avg=0.0;
for(i=4;i>=0;i--)
sum=sum+abc[i];
avg=sum/5.0;
Cout<<“Sum of array values=“<<sum<<endl;
Cout<<“Average of array values=“<<avg;
}

Syed M Hamedoon (Assistant Professor)


for(i=0;i<=4;i++)
{
cout<<“Enter value in element”<<i<<endl;
cin>>abc[i]; Program 4.3
} (Understanding)

abc[i] Value
abc[0] 1
abc[1] 2

abc[2] 3

abc[3] 4

abc[4] 5

Syed M Hamedoon (Assistant Professor)


sum=avg=0.0; Program 4.3
for(i=4;i>=0;i--) (Understanding)
i=4
sum=sum+abc[i]; Sum=0
Sum=sum + abc[4];
Sum =0+5=5

abc[5] i=3;
abc[0] 1 Sum=sum+abc[3];
Sum=5+4=9
abc[1] 2
i=2;
abc[2] 3 sum-=sum+abc[2];
Sum=9+3=12
abc[3] 4
i=1;
sum-=sum+abc[1];
abc[4] 5 Sum=12+2=14

i=0;
Sum=sum+abc[0];
Sum=14+1=15
Syed M Hamedoon (Assistant Professor)
Program 4.4
Write a program in C++ to find out and print
the maximum value in the array
#include<iostream>
using namespace std;
main ()
{
float abc[5],max;
int i;

Syed M Hamedoon (Assistant Professor)


float abc [ 5],max; abc[0] 6
for(i=0;i<=4;i++)
{ abc[1] 4
cout<<“Enter the value in element”<<i<<“=“;
cin>>abc[i]; abc[2] 8
}
max=abc[0]; abc[3] 9
for(i=0;i<=4;i++)
abc[4] 1
{
if(max<abc[i])
max=abc[i];
} max=abc[0]- max=6
cout<<“Maximum value is =“<<max;
} Program 4.4
(Understanding)

Syed M Hamedoon (Assistant Professor)


max=abc[0]-
for(i=0;i<=4;i++) max=689
{
if(max<abc[i]) abc[0] 6

max=abc[i]; abc[1] 4

} abc[2] 8

abc[3] 9
Program 4.4
(Understanding)
abc[4] 1

Syed M Hamedoon (Assistant Professor)


Program 4.5
Write a program in C to input data in to two different arrays and then add
two arrays and store the result in the 3rd array
#include<iostream>
using namespace std;
main ( )
{
float a[5],b[5], s[5];
int i;
cout<<“enter the value in the first array”<<endl;
for (i=0;i<=4;i++)
{
cout<<“enter value in element”<<i<<“=“;
cin>>>a[i];
}

Syed M Hamedoon (Assistant Professor)


cout<<“enter the value in the 2nd Array”<<endl;
for (i=0;i<=4;i++)
{
cout<<“enter value in element”<<i<<“=“;
cin>>>b[i];
}
cout<<“first array + second Array=sum”<<endl;
for (i=0;i<=4;i++)
{
s[i]=a[i]+b[i];
cout<<a[i]<<“+”<<b[i]<<“=“<<s[i]<<endl;
}
}

Syed M Hamedoon (Assistant Professor)


a[0] 3 b[0] 1 s[0] 4
a[1] 6 b[1] 2 s[1] 8

a[2] 9 b[2] 4 s[2] 13

a[3] 2 b[3] 6 s[3] 8

a[4] 4 b[4] 3 s[4] 7

Syed M Hamedoon (Assistant Professor)


Program 4.06
Write a program in C++, to input data in to the array.
Enter a value from the keyboard and find out the
location of the entered value in the array. If the
entered number is not found in the array, display the
message “ Number not found”

Syed M Hamedoon (Assistant Professor)


abc[0] 6
#include<iostream>
abc[1] 4
using namespace std;
abc[2] 8
main ( )
{ abc[3] 9

int abc[5],n,i,p; abc[4] 1

for(i=0;i<=4;i++)
{
cout<<“enter the value in element”<<i<<“=“;
cin>>abc[i];
}
Syed M Hamedoon (Assistant Professor)
p=0;
cout<<“Enter any integer value?”; abc[0] 6
cin>>n;
for(i=0;i<=4;i+) abc[1] 4
{
if(n==abc[i]) abc[2] 8
{
p=i+1; abc[3] 9
break;
} abc[4] 1
}
if (p==0)
cout<<“Number not found”;
else
cout<<“Number found at position =“<<p;
}

Syed M Hamedoon (Assistant Professor)


p=0;
P=0 cout<<“Enter any integer value?”;
cin>>n;
abc[0] 6 n=9

for(i=0;i<=4;i+)
abc[1] 4 {
if(n==abc[i])
abc[2] 8 {
p=i+1;
break;
abc[3] 9
}

abc[4] 1

Syed M Hamedoon (Assistant Professor)


Initializing one dimensional array
Like other variables, the values in the element of
an array can also be assigned when the array
is declared. The assigning values to the
elements of the array at the time of
declaration is called initializing of the array.
For example:
double temp [5]={66.3,77.7,99.2,63.9,59.3}

Syed M Hamedoon (Assistant Professor)


Program 4.7
• Write a program to initialize the values in an array
and then print these values on the screen.
#include<iostream.h>
main ( )
{
double temp [5]={66.3,77.7,99.2,63.9,59.3};
int i;
for(i=0;i<=4;i++)
cout<<temp[i]<<endl;
}

Syed M Hamedoon (Assistant Professor)


temp[0] 66.3
for(i=0;i<=4;i++)
temp[1] 77.7 cout<<temp[i]<<endl;

temp[2] 99.2

temp[3] 63.9

temp[4] 59.3

Syed M Hamedoon (Assistant Professor)


Program 4.8 (do it yourself)
Program 4.9
Write a program in C, to initialize values in a
character type array, copy these values in to
second array of the same type and then print
the values from the second array on the
screen

Syed M Hamedoon (Assistant Professor)


#include<iostream.h>
main ( )
{
char first[7]={‘M’,’A’, ‘R’, ‘R’, ‘I’, ‘A’, ‘M’}, second [7];
int c;
// copy values from first to second
for(c=0;c<=6;c++)
second[c]=first[c];
//print values from second array on screen
for(c=0;c<=6;c++)
cout<<second[c];

Syed M Hamedoon (Assistant Professor)


for(c=0;c<=6;c++)
second[c]=first[c];

char second[7]
char first[7]

first [0] M second [0] M


first [1] A second [1] A
first [2] R second [2] R
first [3] R second [3] R
first [4] I second [4] I
first [5] A second [5] A
first [6] M second [6] M

Syed M Hamedoon (Assistant Professor)


//print values from second array on
screen
for(c=0;c<=6;c++)
cout<<second[c];

second [0] M
second [1] A
second [2] R
second [3] R
second [4] I
second [5] A
second [6] M

Syed M Hamedoon (Assistant Professor)


String variables
String variables are declared in the same way as
character type variable. The length of the string is
the total number of elements of the array.
Syntax:
Char variable name[n]
Where
Char type of array
Variable nameany name
[n] size of the array

Syed M Hamedoon (Assistant Professor)


For example
char str[10]=“pakistan”;
In the above statement the word “pakistan” is assigned to
“str” at the time of decleration. The null character ‘\0’
is automatically appended at the end of the word
“pakistan”.

The above statement can also be written as


char str[10]={‘p’, ‘a’, ‘k’, ‘I’, ‘s’, ‘t’, ‘a’, ‘n’ , ‘\0’};
Null character is added because each string must have a
null character at its end. If the null character is not
added at the end of string then the variable of char
type is handled as an array (not as a string)

Syed M Hamedoon (Assistant Professor)


Program 4.10
• Write a program to initialize a string with the
word “Pakistan” then print on the screen
#include<iostream.h>
main ( )
{
char str[15]=“pakistan”;
cout<<str;
}

Syed M Hamedoon (Assistant Professor)


Program 4.11 (skip )
Program 4.12
Write a program to copy one string to another
string. Input the string in to the first string
variable and then copy this string to the
second string variable by copying characters
one by one.

Syed M Hamedoon (Assistant Professor)


#include<iostream.h>
main ( )
{
char str1[15], str2[15];
int I;
cout<<“enter any string?”<<endl;
cin>>str1;
for(i=0;str1[i]!=‘\0’;i++)
str2[i]=str1[i];
str2[i]=‘\0’;
cout<<str2;
}
Syed M Hamedoon (Assistant Professor)
cout<<“enter any string?”<<endl;
cin>>str1;

str1[0] H
str1[1] E
str1[2] L
str1[3] L
str1[4] O

Syed M Hamedoon (Assistant Professor)


for(i=0;str1[i]!=‘\0’;i++)
str2[i]=str1[i];
str2[i]=‘\0’;

str1[0] H str2[0] H

str1[1] E str2[1] E

str1[2] L str2[2] L

str1[3] L str2[3] L

str1[4] O str2[4] O

Syed M Hamedoon (Assistant Professor)


Sorting Arrays
The process of arranging data in a specified
order is called sorting. Numeric data can be
arranged in ascending order and descending
order.
Types of sorting
1) Bubble sort
2) Selection sort

Syed M Hamedoon (Assistant Professor)


Bubble sorting
The bubble sorting method is used to arrange
values of an array in ascending or descending
order
To arrange in ascending order , two neighboring
elements are compared . If one element is
greater than the other , the two are exchanged.
Number of iterations required= n-1
Where n is the number of elements inside the array.

Syed M Hamedoon (Assistant Professor)


ARR[4]={4, 19, 1,3}
4 19 1 3

Number of iterations required=N-1=4-1=3


for(u=3;u>=1;u--)
Iteration 1
for(i=0;i<u;i++)
4 19 1 3
if (ARR[i]>ARR[i+1]
4 19 1 3 {
t=ARR[i];
4 1 19 3 ARR[i]=ARR[i+1];
ARR[i+1]=t;
}
4 1 3 19

Syed M Hamedoon (Assistant Professor)


Iteration 2

for(u=3;u>=1;u--)
for(i=0;i<u;i++)

4 1 3 19 if (ARR[i]>ARR[i+1]
{
1 4 3 19 t=ARR[i];
ARR[i]=ARR[i+1];
ARR[i+1]=t;
1 3 4 19
}

1 3 4 19

Syed M Hamedoon (Assistant Professor)


Iteration 3

for(u=3;u>=1;u--)
1 3 4 19
for(i=0;i<u;i++)

if (ARR[i]>ARR[i+1]
1 3 4 19 {
t=ARR[i];
ARR[i]=ARR[i+1];
ARR[i+1]=t;
}

Syed M Hamedoon (Assistant Professor)


Program 4.13
#include<iostream.h>
main ()
{
int i,u,t,ARR[4]={4,19,1,3} ;
for(u=3;u>=1;u--)
for(i=0;i<u;i++)
if (ARR[i]>ARR[i+1]
{
t=ARR[i];
ARR[i]=ARR[i+1];
ARR[i+1]=t;
}
Syed M Hamedoon (Assistant Professor)
cout<<“sorted values”<<endl;
for(i=0;i<=3;i++)
cout<<ARR[i]<<endl;
}

Syed M Hamedoon (Assistant Professor)


Selection Sort
This method is used for sorting arrays in
ascending or in descending order. If an array
has n elements, n-1 iterations are required to
sort the array.

Syed M Hamedoon (Assistant Professor)


Example
(sort the array using selection sorting)
4 19 1 13

4 19 1 13

1 19 4 13
Iteration 1
Find out the smallest value from the list starting from first element to
the last element of the list. The smallest value is 1 in location 3.
interchange the value of the first element with the third element i.e
swap values of ARR[0] and ARR[2]

Syed M Hamedoon (Assistant Professor)


1 19 4 13

1 4 19 13

• Iteration 2
Find out the smallest value from the list starting
from the second element to the last element of
the list. The smallest value is 4 at location 3.
interchange the value of the first element with
the third element i.e swap values of ARR[1] and
ARR[2]
Syed M Hamedoon (Assistant Professor)
1 4 19 13

1 4 13 19

• Iteration 3
Find out the smallest value from the list starting
from the third element to the last element of the
list. The smallest value is 13 in location 4.
interchange the value of the first element with
the third element i.e swap values of ARR[2] and
ARR[3]

Syed M Hamedoon (Assistant Professor)


Program 4.14
Write a program to sort the given list of array
using selection sorting technique

4 19 1 13

Syed M Hamedoon (Assistant Professor)


#include<iostream.h>
main ()
{
int i,u,t,ARR[4]={4,19,1,3} ;
For(u=0;u<3;u++)
{
m=ARR[u];
for(i=u;i<=3;i++)
If(m>ARR[i];
{
m=ARR[i];
p=i;
}
t=ARR[p];
ARR[p]=ARR[u];
ARR[u]=t;
}

Syed M Hamedoon (Assistant Professor)


Cout<<“sorted values =“<<endl;
For(i=0;i<=3;i++)
Cout<<ARR[i]<<endl;
}

Syed M Hamedoon (Assistant Professor)


Multi dimensional Arrays
A multi dimensional array is an array of more
than one array.
For example
An array of 2 Dimensional array is called table or
matrix
A two dimensional array consists of columns and
rows .

Syed M Hamedoon (Assistant Professor)


Example (7 rows and 3 columns)
2D Array Temp[7][3]

Temp[0][0] Temp[0][1] Temp [0][2]


Temp[1][0]
Temp[2][0]
Temp[3][0]
Temp[4][0]
Temp[5][0]
Temp[6][0] Temp[6][2]

Syed M Hamedoon (Assistant Professor)


Declaration of two dimensional array
Syntax:
Type array_name[r][c];
Type array data type
Araay_name it can be any name
[r] represents the number of rows of table
[c]represents the number of columns of table
Example:
int abc [12][3];

Syed M Hamedoon (Assistant Professor)


Program 4.15
Write a program to print out the data from the
elements of a table
#include<iostream.h>
main ( )
{
int abc[2][3],r,c;
cout<<“input data in to the table”<<endl;
r=0;

Syed M Hamedoon (Assistant Professor)


while (r<=1)
{
c=0;
while(c<=2)
{
cout<<“enter value in a row=“;
cout<<r<<column=“<<c<<“ “;
cin>>abc[r][c];
c=c+1;
}
r=r+1;
}
Syed M Hamedoon (Assistant Professor)
cout<<“printing data from the table”<<endl;
r=0;
while(r<=1)
{
c=0;
while (c<=2)
{
cout<<abc[r][c]<<“\t”;
c=c+1;
}
cout<<endl;
r=r+1;
}
}
Syed M Hamedoon (Assistant Professor)

You might also like