You are on page 1of 18

Arrays

Unary scope resolution operator


Setw operator

Arrays

An array is a consecutive group of memory


locations that all have the same type.
These locations are referenced by a single
variable called array name.
Each element of an array is referenced by its
position in the array.
Position of an element in an array is
represented by an Index value or subscript
The first element in array has subscript 0 and
is called zeroth element i.e. a[0] - a sub zero

Arrays

For example;
int c[12];
If an array has n elements, the index values
will be from 0 to n-1.
The above array contains integer elements
and its name is c. It has 12 elements. First
element of array is a[0] and 12th element is
the last element and its reference is a[11].
Name of entire array is c. Its 12 elements are
referred to as c[0] to c[11].

Arrays

A subscript must be an integer or integer


expression.
For example; a=5, b=6
c[ a+b ]+=2;
To print sum of first three elements of array
cout<< c[0]+c[1]+c[2];

Declaring Arrays
Type array name[array size];
Using a loop to initialize arrays elements;
1. #include<iostream.h>
2. #include<conio.h>
3. int main()
4. {
5.
int i, c[12];
6.
for(i=0;i<12;i++)
7.
c[i]=0;
8.
for(i=0;i<12;i++)
9.
cout<<c[i]<<endl;
10.
getch();
11. }

Initializing an array in a declaration with


an initializer list
1.
2.
3.
4.
5.
6.
7.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int main()
{
int i;
int c[12]={0,1,2,3,4,5,6,7,8,9,10,11};

8.

for(i=0;i<12;i++)
cout<<setw(3)<<c[i]<<endl;
getch();

9.
10.
11.
12.

Specifying an arrays size with a constant


variable and setting array elements with
calculations
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int main()
{
const int arraysize=50;
int c[arraysize],i;
for(i=0;i<arraysize;i++)
c[i]=2+i*2;
cout<<"first 50 even numbers"<<endl;
for(i=0;i<arraysize;i++)
cout<<setw(10)<<c[i]<<endl;
getch();
}

Summing the elements of an array


1.
2.
3.
4.
5.
6.
7.
8.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
int main()
{
const int arraysize=10;
int c[arraysize]={10,20,30,40,50,60,70,80,90,100};
int total=0;

9.
10.
11.
12.
13.
14.
15.

for(int i=0;i<arraysize;i++)
{
cout<<setw(4)<<c[i]<<endl;
total+=c[i];
}
cout<<"total is :"<<total<<endl;
getch();

16.
17.

Searching array

#include<iostream.h>
#include<conio.h>
int main()
{
const int arraysize=10;
int c[arraysize];
int searchkey,k=0;
for(int i=0;i<arraysize;i++)
c[i]=i*2;

cout<<"enter search key :"<<endl;


cin>>searchkey;

for(int i=0;i<arraysize;i++)
{
if(searchkey==c[i])
{
k=i;
break;
}
}
if (k==(arraysize+1))
cout<<"key not found"<<endl;
else
cout<<"key found at c["<<k<<"]";
getch();

String Variable

It is an array of character type variables


Syntax is;
char varname[n];
The last character of every string variable is
Null character. Null character is represented
by /0.
For example;
char a[5];
In this variable max of 4 characters can be
entered because fifth character will be a Null
character. If 2 characters will be entered, 3 rd
will be Null. Null character is automatically
added at end of the data in the string.

String Variable

char str[9]="pakistan";
char
str[9]={'p','a','k','i','s','t','a','n','\0'};
#include<iostream.h>
#include<conio.h>
int main()
{
const int arraysize=8;
char c[arraysize],i;
for(i=0;i<arraysize;i++)
cin>>c[i];
for(i=0;i<arraysize;i++)
cout<<c[i]<<endl;
getch();
}

#include<iostream.h>
#include<conio.h>
int main()
{
const int arraysize=8;
char c[arraysize];
cin>>c;
cout<<c<<endl;
getch();
}

Sorting Arrays
The process of arranging data in a specified order
is called sorting.
1. Bubble Sort
2. Selection Sort
Bubble Sort:
To arrange values of an array in ascending or
descending order
3. Two neighboring elements are compared
4. If one element is larger than the other, the two
are exchanged.
It is a slow process and used for sorting only small
amount of data.

#include<iostream.h>
#include<conio.h>
int 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;
}
cout<<"sorted values are"<<endl;
for(i=0;i<=3;i++)
cout<<arr[i]<<endl;
getch();
}

Selection Sort
To arrange values of an array in ascending or
descending order
1. Find out the smallest value from the list
starting from first element to the last
element
2. Find out the smallest value from the list
starting from second element to the last
element
3. Find out the smallest value from the list
starting from third element to the last
element and so on.

#include<iostream.h>
#include<conio.h>
int main()
{
int i, u, t,p,m, arr[4]={4,19,1,13};

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;
}
cout<<"sorted values are"<<endl;
for(i=0;i<=3;i++)
cout<<arr[i]<<endl;
getch();
}

Passing arrays and individual array elements to functions

#include<iostream.h>
#include<conio.h>
void array(int [], int);
void array2(int);
int main()
{
const int as=5;
int a[as]={0,1,2,3,4};
for(int i=0; i<as;i++)
cout<<a[i];
cout<<endl;

array(a,as);

for(int j=0; j<as;j++)

cout<<a[j];
cout<<endl;
array2(a[3]);
cout<<a[3];
getch();
}
void array(int a[], int as)
{
for(int i=0; i<as;i++)
a[i]*=2;
}
void array2(int e)
{
cout<<(e*=2)<<endl;
}

You might also like