Arrays
Arrays
ARRAYS
Definition of Array
Array is a collection of similar data types in which each element is unique one and located in
separate memory locations and it follows to store the array elements in continues memory
locations.
data_type variable_name[index_size];
ex: int a[10];
here ‘int’ is a data type which stores the integer value, and ‘a’ is a variable the name given to the
memory location, and ‘[10] is the size of the array variable, it can store the 10 values in different
memory locations.
Array initialization
data_type varible[index]={v1,v2,v3,------vn};
Characteristic of Array
1. The declaration int a[5] is nothing but creation of 5 variables of integer types in the memory.
Instead of declaring five variables for five values, the programmer can define them n an array.
2. All the elements of an array share the same name, and they are distinguished from one another
with the help of an index element.
3. The index element in an array plays major role for calling each element
4. Any particular element of an array can be modified separately without disturbing other
elements.
int a[5]={1,10,5,4,5};
If a programmer needs to replace 5 with 10, then need not require to change all other numbers
expect 5. To carry out this task the statement a[4]=10 can be used. Here there elements are not
disturbed.
5. Any element of an array a[ ] can be assigned/equated to other ordinary variable or array
variable of its type.
For example
x=a[3];
a[2]=b[2];
a[3]=y;
Page 21 of 39
Problem Solving and Computer Programming
Types of Arrays:
One-Dimensional Arrays
The elements of an integer array a[5] are stored in continuous memory locations. It is
assumed that the starting location is 4000. Each integer element requires 2 bytes. Hence
subsequent element appears after gap of 2 locations. Table shows the locations of elements f an
integer arrays.
Similarly, the elements of arrays of any data type are stored in continuous memory location. The
only difference is that numbers of locations are different for different data types.
p) write a program to initialize array and display the array element in the array?
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={10,15,12,18,25};
int i;
clrscr();
printf("\n initialized array elements are");
for(i=0;i<5;i++)
{
printf("\na[%d]-->%d",i,a[i]);
}
getch();
}
Output:
Initialized array elements are
a[0]-->10
a[1]-->15
a[2]-->12
a[3]-->18
a[4]-->25
Page 22 of 39
Problem Solving and Computer Programming
P) write a program to assign array elements and display array elements with address
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n;
clrscr();
printf("\n enter limitation of array n=");
scanf("%d",&n);
printf("\n enter array elements ");
for(i=0;i<n;i++)
{
printf("\na[%d]= ",i);
scanf("%d",&a[i]);
}
printf("\n array elements are ");
printf("\n array_index array_value array_address");
for(i=0;i<n;i++)
{
printf("\na[%d] = %d --> %d",i,a[i],&a[i]);
}
getch();
}
Output:
Page 23 of 39
Problem Solving and Computer Programming
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[10],i,n;
clrscr();
printf("\nenter limitation of array n=");
scanf("%d",&n);
printf("\nenter array a elemetns ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nenter array b elemetns ");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
printf("\nperfrom addition of arrays");
for(i=0;i<n;i++)
c[i]=a[i]+b[i];
printf("\nsum of array a and b elements are ");
for(i=0;i<n;i++)
{
printf("\n%d + %d = %d",a[i],b[i],c[i]);
}
getch();
}
Output:
enter limitation of array n=5
enter array a elemetns 20 12 -30 50 12
enter array b elemetns -10 25 50 -20 34
perfrom addition of arrays
sum of array a and b elements are
20 + -10 = 10
12 + 25 = 37
-30 + 50 = 20
50 + -20 = 30
12 + 34 = 46
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n;
Page 24 of 39
Problem Solving and Computer Programming
int sum=0;
clrscr();
printf("\n enter limitations of array n = ");
scanf("%d",&n);
printf("\n enter array elements : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n array elements are : ");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
printf("\n perform sum of array elements");
for(i=0;i<n;i++)
sum=sum+a[i];
printf("\n sum of array elements is : %d",sum);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],b[10],c[20],i,j,n,m,p;
static sum;
clrscr();
printf("\n enter limitations of array n = ");
scanf("%d",&n);
printf("\n enter array a elements : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n array a elements are : ");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
printf("\n enter limitations of array m = ");
scanf("%d",&m);
printf("\n enter array b elements : ");
for(i=0;i<m;i++)
Page 25 of 39
Problem Solving and Computer Programming
scanf("%d",&b[i]);
printf("\n array b elements are : ");
for(i=0;i<m;i++)
printf(" %d ",b[i]);
p=m+n;
printf("\n perform concatenation of arrays");
j=0;
for(i=0;i<p;i++)
{
if(i<n)
{
c[i]=a[i];
}
else
{
c[i]=b[j];
j++;
}
}
printf("\n concatenated array is : ");
for(i=0;i<p;i++)
{
printf(" %d ",c[i]);
}
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,max,min;
clrscr();
printf("enter number of values ");
Page 26 of 39
Problem Solving and Computer Programming
scanf("%d",&n);
printf("\nenter values \n");
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
printf("searching for minimum & maximum values from given array");
min=a[0];
max=a[0];
for(i=1;i<n;i++)
{
if(min>a[i])
{
min=a[i];
}
if(max<a[i])
{
max=a[i];
}
}
printf("\nminimum of given values is %d",min);
printf("\nmaximum of given values is %d",max);
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],key,i,j,n;
clrscr();
printf("\nenter no of array elements : ");
scanf("%d",&n);
printf("\nenter the array elemetns : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
Page 27 of 39
Problem Solving and Computer Programming
}
printf("\nenter searching element : ");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(key==a[i])
break;
}
if(i==n)
printf("\nsearching element not find");
else
printf("\nelement found at position ", (i+1));
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],temp,i,j,n;
clrscr();
printf("\nenter no of array elements to sort: ");
scanf("%d",&n);
printf("\nenter the array elemetns : ");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
Page 28 of 39
Problem Solving and Computer Programming
}
printf("\nsorted array is : ");
for(i=1;i<=n;i++)
printf("\t%d",a[i]);
getch();
}
Output:
Recursive Functions in C
In C programming language, function calls can be made from the main() function, other
functions or from the same function itself. The recursive function is defined as follows...
A function called by itself is called recursive function.
The recursive functions should be used very carefully because, when a function called by
itself it enters into the infinite loop. And when a function enters into the infinite loop, the
function execution never gets completed. We should define the condition to exit from the
function call so that the recursive function gets terminated.
When a function is called by itself, the first call remains under execution till the last call
gets invoked. Every time when a function call is invoked, the function returns the execution
control to the previous function call.
P) Find Factorial of Given integer number using Recursive function
#include<stdio.h>
#include<conio.h>
int factorial( int ) ;
int main()
{
int fact, n ;
printf("Enter any positive integer: ") ;
scanf("%d", &n) ;
fact = factorial( n ) ;
printf("\nFactorial of %d is %d\n", n, fact) ;
return 0;
}
int factorial( int n )
{
int temp ;
if( n == 0)
return 1 ;
else
temp = n * factorial( n-1 ) ; // recursive function call
return temp ;
}
Page 29 of 39
Problem Solving and Computer Programming
#include<stdio.h>
#include<conio.h>
void binary(int[],int low,int high,int key);
void main()
{
int a[10],n,i,key;
clrscr();
printf("\nenter n value : " );
scanf("%d",&n);
printf("\nenter %d sorted elements",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nenter searching element : ");
scanf("%d",&key);
binary(a,0,n-1,key);
getch();
}
void binary(int a[10],int low,int high,int key)
{
int mid;
if(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
printf("element is found at passion %d",(mid+1));
else if(key<a[mid])
{
high=mid-1;
binary(a,low,high,key);
}
else
{
low=mid+1;
binary(a,low,high,key);
}
}
else
printf("\n entered element is not found ");
}
Output:
Enter n value is: 6
Enter sorted elements : 1 2 3 4 5 6
8
Searching element is: 4
Element is found at position is: 4
P) Program to sort array of elements using functions
Page 30 of 39
Problem Solving and Computer Programming
#include<stdio.h>
#include<conio.h>
void sort(int[],int);
void main()
{
int a[10],i,n;
clrscr();
printf("\nenter no of array elements to sort: ");
scanf("%d",&n);
printf("\nenter the array elemetns : ");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n Given Array : ");
for(i=1;i<=n;i++)
printf("\t%d",&a[i]);
sort(a,n);
printf("\nsorted array is : ");
for(i=1;i<=n;i++)
printf("\t%d",a[i]);
getch();
}
void sort(int a[10],int n)
{
int temp,i,j;
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
Two-Dimensional Array
Two-Dimensional array can be thought as a rectangular display of the elements with rows
and columns. For example elements of int x[3][4]
Page 31 of 39
Problem Solving and Computer Programming
The arrangement of array elements shown in the above table is only for the sake of
understanding. Conceptually the elements are shown in matrix form. Physically array elements are
stored in one continuous form in the memory.
Initialization
int a[3][3]={{1,2,3},{4,5,6},{7,9,9}};
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int i,j;
printf(“\n Array Elements Are : \n“);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“ %d”,a[i][j]);
}
printf(“\n”);
}
}
Output:
Page 32 of 39
Problem Solving and Computer Programming
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],trace=0,i,j,m,n;
clrscr();
printf("\nprogram to find trace of matrix");
xx:
printf("\nmatrix order of matrix");
scanf("%d%d",&m,&n);
if(m!=n)
goto xx;
printf("\nenter the elements");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
trace=trace+a[i][j];
}
}
printf("\n trace of matrix is = %d",trace);
getch();
}
Output:
program to find trace of matrix
Matrix order of matrix3 3
enter the elements
123
456
789
trace of matrix is = 15
Page 33 of 39
Problem Solving and Computer Programming
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],i,j,m,n;
clrscr();
printf("\nprogram to perform transpose of matrix ");
printf("\nenter matrix order m and n ");
scanf("%d%d",&m,&n);
printf("\nenter matrix elements \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\ntranspose of matrxi is : \n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",a[j][i]);
}
printf("\n");
}
getch();
}
Output
transpose of matrix is :
1 4 6
2 5 7
Page 34 of 39
Problem Solving and Computer Programming
The C program allows array of two or more multi dimensional. The compiler determines
the restriction on it. The syntax of multi-dimensional array as follows.
data_type variable_name[s1][s2][s3][s4]……[si]…[sn];
{1,2,3,
2,3,4,
6,4,1}
{9,8,7,
6,5,4,
3,2,1}
};
Arrays are a collection of one more elements of same data type. Array elements can be
passed to the function by the value or a reference.
Write a program to read array elements from functions and display them
#include<stdio.h>
#include<conio.h>
void read(int a[],int n);
void main()
{
int a[10],n,i;
clrscr();
printf("initializing array elements through array");
printf("\nenter n value ");
scanf("%d",&n);
read(a,n);
printf("\narray elements \n");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
Page 35 of 39
Problem Solving and Computer Programming
getch();
}
void read(int a[],int n)
{
int i;
printf("\nenter array elements \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
Output :
initializing array elements through array
enter n value 5
enter array elements
10
20
30
40
50
array elements
10 20 30 40 50
Write a program to perform matrix addition and multiplication using functions
#include<stdio.h>
#include<conio.h>
#include<process.h>
int a[10][10],b[10][10],c[10][10];
int i,j,k;
void matadd();
void matmul();
void main()
{
int ch;
printf("\performing matrix addition and multiplication using functions");
printf("\n1.matrix addition\n2.matrix multiplication\n3.exit");
printf("\nenter ur choice ");
scanf("%d",&ch);
switch(ch)
{
case 1: matadd();
break;
case 2: matmul();
break;
case 3: exit(0);
Page 36 of 39
Problem Solving and Computer Programming
Page 37 of 39
Problem Solving and Computer Programming
if(n!=p)
{
printf("\n enter valued order for matrix multiplication ");
goto xx;
}
else
{
printf("\n enter first matrix elements : \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n enter second matrix elements : \n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
printf("\n multiplication of matrix is : \n");
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
sum=0;
for(k=0;k<n;k++)
{
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf(" %d ",c[i][j]);
}
printf("\n");
}
}
}
Output:
Page 38 of 39
Problem Solving and Computer Programming
enter ur choice 2
multiplication of matrix is :
22 28
49 64
Page 39 of 39