Professional Documents
Culture Documents
Arrays
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
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. }
#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.
#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();
}
#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;
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
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();
}
#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);
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;
}