5.Arrays
5.Arrays
An array can be defined as an ordered list of homogeneous data elements. These element
may be of type int, float, char or double. All these elements are stored in consecutive
memory locations.
An array is described by a single name or an identifier. And each individual data
item in the array is referenced by a subscript (or index) enclosed in a pair of square
brackets.
Array is a collection of similar data types. In which each element is unique one
and located in separate memory locations.
Declaration of an Array
int a[5];
char ch[10];
float bc[5];
long num[5];
Array Initialization
Int a[5]={1,2,3,4,5};
Here, 5 elements are stored in an array ‘a’. The array elements are stored sequentially in
separate locations. Reading of array elements begins from ‘0’.
Classification of arrays
Arrays are classified into one-dimensional array and multi-dimensional arrays. Further,
the multi-dimensional arrays are classified into two-dimensional, three-dimensional and
so on n-dimensional arrays. The dimensionality is determined by the number of
subscripts in an array.
One-dimensional array
It is a linear list of fixed number of data items of the same type. All these data items are
accessed using the same name using a single subscript. It is similar to a row or column
matrix. It is also called as a single-dimensional array or one subscripted variable.
Syntax :
data_type arrayname[size];
where,
arrayname-> name of an array
size -> number of elements of data type. and the size must be an integer
constant specified within a pair of square brackets.
Example
int list[10];
char name[20];
float xyz[5];
double p[100];
The elements of an integer array a[5] are stored in continous memory locations. It is
assumed that the starting memory location is 2000. Each integer element requires 2 bytes.
Hence subsequent element appears after gap of 2 locations.
Character arrays are called strings. There is large difference between an integer array and
a character array. In character array NULL (‘\0’) character is automatically added at the
end, where as in integer or other types of arrays no NULL character is placed at the end.
#include<stdio.h>
#include<conio.h>
void main()
{
char name[10]={‘a’,’r’,’r’,’a’,’y’};
int i=0;
clrscr();
printf(“\n character memory location\n”);
while(name[i]!=’\0’)
{
printf(“\n[%c]\t\t[%u]”,name[i],&name[i]);
i++;
}
getch();
}
WAP accepts n integers and stores them in an array called num. It also prints them.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,num[20];
clrscr();
printf(“enter the size of an array\n”);
scanf(“%d”,&n);
printf(“enter the elements one by one\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&num[i]);
}
printf(“array is\n”);
for(i=0;i<n;i++)
{
printf(“num[%d]=%d\n”,i,num[i]);
}
getch();
}
2. WAP to computes the sum of even and odd numbers stored in an array of N
integers.
#include<stdio.h>
#include<conio.h>
void main()
{
int num[20];
int i, esum=0,odsum=0,n;
clrscr();
printf(“enter the size of an array\n”);
scanf(“%d”,&n);
printf(“enter the elements one by one\n”);
for(i=0;i<n;i++)
scanf(“%d”,&num[i]);
for(i=0;i<n;i++)
{
if((num[i]%2)==0)
esum+=num[i]);
else
odsum+=num[i]);
}
printf(“sum of even numbers=%d\n”,esum);
printf(“sum of odd numbers=%d\n”,odsum);
getch();
}
3. WAP to prints the largest element of an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int num[10],n,i,large;
printf(“\nhow many elements u want to be entered :”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nenter the element :”);
scanf(“%d”,&num[i]);
}
/* largest number */
large=num[0];
for(i=0;i<n;i++)
{
if(large<num[i])
large=num[i];
}
printf(“largest element is=%d\n”,large);
getch();
}
4. WAP to enter 5 student’s name, roll, age and print the same.
#include<stdio.h>
#include<conio.h>
void main()
{
char name[5][10];
int roll[5],age[5],i;
clrscr();
for(i=0;i<5;i++)
{
printf(“\n Enter name, roll and age :”);
scanf(“%s%d%d”,name[i],&roll[i],&age[i]);
}
printf(“\n Print student details :”);
for(i=0;i<5;i++)
{
printf(“\n name=%s”,name[i]);
printf(“\n roll=%d”,roll[i]);
printf(“\n age=%d”, age[i]);
}
getch();
}
WAP to print string in the reverse order.
#include<stdio.h>
#include<conio.h>
void main()
{
static char s[15];
int i;
clrscr();
puts(“enter a string :\n”);
gets(s);
puts(“reverse string :”);
for(i=15;i>=0;i--)
{
printf(“%c”,s[i]);
}
getch();
}
Two-dimensional Array
Two-dimensional array can be thought as a rectangular display of elements with rows and
columns.
For example elements of int x[3][3];
Array Initialization
int a[][]={{1,1,2},{3,4,4},{5,4,3}};
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
clrscr();
printf(“\n Elements of an array\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
getch();
}
2. Read the matrix of the order up to 10x10 elements and display the same in the
matrix form.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,row,col,a[10][10];
clrscr();
printf(“\n Enter order of matrix up to (10x10) :”);
scanf(“%d%d”,&row,&col);
printf(“\n Enter elements :”);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“\n The matrix is :\n”);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“%d\t”,a[i][j]);
}
}
getch();
}
The C program allows array of two or multi dimensions. The compiler determines the
restriction on it.
Syntax
Int mat1[3][3][3]={{1,2,3,4,5,6,7,8,9,
1,4,7,2,8,9,1,2,3};
{ 2,9,8,4,1,3,3,2,3}
A three dimensional array can be thought of as an array of arrays. The outer array
contains three elements. The inner array size is two dimensional with size[3][3].
#include<stdio.h>
#include<conio.h>
void main()
{
int array_3d[3][3][3];
int a,b,c;
clrscr();
for(a=0;a<3;a++)
{
for(b=0;b<3;b++)
{
for(c=0;c<3;c++)
{
array_3d[a][b][c]=a+b+c;
}
}
}
for(a=0;a<3;a++)
{
printf(“\n”);
for(b=0;b<3;b++)
{
for(c=0;c<3;c++)
{
printf(“%3d”,array_3d[a][b][c]);
}
}
}
getch();
}
Searching
1) Sequential searching (Linear searching)
Sequential searching is nothing but searching an element in linear way. This can be in
array or linked list. Its start the search from beginning and scan the elements one by one
until the end of array or linked list. If search is successful then it will return the location
of element, otherwise it will return the failure notification.
Algorithm
Step 1- INDEX=0
Step 2- Scan each element of array one by one.
Step 3- a) If match occurs then return the index value,
b) Otherwise INDEX=INDEX+1
Step 4- Repeat the same process until unique value comes in scanning.
Step 5- Return the failure notification.
//Linear search
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
int data,i,flag=0;
int array[100];
clrscr();
printf("\nHow many elements you want to be entered :");
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf("\nenter the number :");
scanf(“%d”,&array[i]);
}
printf("\nenter the number to be searched :");
scanf(“%d”,&data);
for(i=0;i<n;i++)
{
if(array[i]==data)
flag=1;
}
if(flag==1)
printf("\n Number is found");
else
printf("\n Number is not found");
getch();
}