Professional Documents
Culture Documents
An array is a collection of several data items of the same data type. It consists of
several contiguous memory locations storing data. It removes the cumbersome task
of defining separate variables for each data item. A single variable can be used to
store one or more data items. For example, if you want to store ages of 10 students
of a class, you can easily declare an array of integer type of size 10 instead of
declaring 10 variables of integer type each storing age of a student. The general
form of the single dimension array is:-
General format:
The type is the base type of the array and which is the type of each of the element
of the array. The size is the no of the elements in the array. The variable_name is
the name of the identifier holding the array. For example,
The age is an array of type integer whose size is 10. It can hold 10 data values. The specific
element in the array can be accessed using an index. An index is the offset from the first element
of the array. For example, first element has an index of 0 while the third element has an index of
2. The integer age has elements from age[0] to age[9]. Here is a program which illustrates the
working of arrays.
#include<iostream.h>
void main ()
int age[10];
int max,min;
cout << "Enter the age of the student " << i+1 <<endl;
max=min=age[0];
for(i=1;i<10;i++)
sum=sum+age[i];
if(age[i]>max)
max=age[i];
if(age[i]<min)
min=age[i];
avg=sum/10;
cout << "Average age of the students of the class : " << avg <<
endl;
cout << "Maximum age of the student of the class : " << max <<
endl;
Initializing Arrays
The arrays can be initialized by giving the initial values in a list and enclosed in
curly brackets, which is placed after the equal sign which is put after the declaration
of the array. For example,
int age[5]={12,13,10,24,15};
Elements of the array have values 12, 13, 10, 24, 15. The elements age[0] has
value 12 and age[4] will have 15. If the elements of the array are not initialized
then they take some garbage value before they are assigned to some specified
value. There is also another way of initializing all the elements of the array to be
zero. For example,
int age[5]={0};
int age[5]={12,13};
The elements age[0] and age[1] will be initialized to 12 and 13 respectively and rest
of the elements will be initialized to zero.
The most common use of one dimensional array is for character strings. The null
terminated string is a character array with a null character at the end. The
characters form the string with a null character indicating the termination of the
string. A null terminated string can be declared as
char name[10];
it will hold 10 characters with a null at the end. The size of the array can be more
than the length of the array. The array can be initialized as
char name[10]={'f','a','t','h','m',’a’};
The first 6 elements are initialized and rest elements are null characters. Here is a
program which calculates the length of the string.
#include<iostream.h>
void main()
char name[15];
int i=0;
i++;
OUTPUT:
ibrahim
• Linear search
#include<iostream.h>
void main ()
int array[10]={10,22,75,4,80,6,7,68,79,14};
int key,x;
cin>> key;
x=i;
else
//declaration
Example-I
#include<iostream.h>
void main ()
int i,j;
int sum=0,avg=0;
for(i=0;i<2;i++)
for(j=0;j<5;j++)
sum=sum+age[i][j];
avg=sum/5;
cout << "Average of the elements of the row " << i+1 << "
is " << avg << endl;
sum=0;
Example-II
#include<iostream.h>
#include<conio.h>
main()
clrscr();
char week[7][9] =
{“Sunday”,”Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday};
clrscr();
getch();
Output:
0 1 2 3 4 5 6 7 8
S U N D A Y
M O N D A Y
T U E S D A Y
W E D N E S D A Y
T H U R S D A Y
F R I D A Y
S A T U R D A Y
Example-III
#include <iostream.h>
int main()
char name[5][5]={"jane","fred","mary","henry","john"};
int i;
cout << name[i] << ": " << age[i] << "\n";
total_age += age[i];
return 0;
OUTPUT:
If the above program is compiled and run then it will give the following result:
Jane: 16
Fred: 2
Mary: 77
Henry: 40
John: 12
Average Age: 29
Example-IV
#include<iostream.h>
#include<string.h>
main( )
int FOUND=1;
int NOTFOUND=0;
int i, flag, a ;
cin>>yourname;
flag = NOTFOUND ;
if ( a == 0 )
flag = FOUND ;
break ;
if ( flag == NOTFOUND )
OUTPUT:
#include<conio.h>
main()
clrscr();
int temp[3][4] = {{26, 34, 22, 17},{24, 32, 19, 13},{28, 38, 25, 20}};
int highest = 0;
highest = temp[i][j];
• Program showing
– initialization
1 2 3
4 5 6
int main()
int sumDiag1 = 0;
sumDiag1 += array[ i ][ i ] ;
cout << "Sum of Values on first diagonal:" << sumDiag1 << endl;
int sumDiag2 = 0;
sumDiag2 += array[ i ][ 3 - i - 1 ] ;
cout << "Sum of Values on second diagonal:" << sumDiag2 << endl;
int sumAbove = 0;
if ( i < j )
sumAbove += array[ i ][ j ] ;
int sumBellow = 0;
if ( i > j )
sumBellow += array[ i ][ j ] ;
cout << "Sum of Values bellow diagonal:" << sumBellow << endl;
} // end main
OUTPUT:
#include<iostream.h>
#include<conio.h>
#include<string.h>
clrscr();
int pos,i,j;
j=0;
char s[20],s1[20];
cout<<"enter a string\n";
cin>>s;
pos=strlen(s);
for(i=pos-1;i>=0;i--)
s1[j]=s[i];
j+=1;
s1[j]='\0';
cout<<strlen(s)<<endl;
cout<<strlen(s1)<<endl;
cout<<s<<endl;
cout<<s1;
if(strcmp(s,s1)==0)
else
cout<<"not a palindrome";
getch();