0% found this document useful (0 votes)
19 views19 pages

Arrays

Vj gg vjh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views19 pages

Arrays

Vj gg vjh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Problem Solving and Computer Programming

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.

Declaration of array follows below syntax

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};

ex: int a[10]={10,20,15,25,11,21,16,26,12,22};

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:

In C-programming language supports three different types of Arrays


1. One-Dimensional Arrays
2. Two-Dimensional Arrays
3. Three / Multi – Dimensional 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.

Element A[0] A[1] A[2] A[3] A[4]


Address 4000 4002 4004 4006 4008

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:

enter limitation of array n=5


enter array elemetns
a[0]= 12
a[1]= 34
a[2]= 52
a[3]= 14
a[4]= 16

array elements are


array_index array_value array_address
a[0] = 12 --> -32
a[1] = 34 --> -30
a[2] = 52 --> -28
a[3] = 14 --> -26
a[4] = 16 --> -24

Page 23 of 39
Problem Solving and Computer Programming

P)write a program to perform addition of two arrays

#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

p) write a program to perform the sum of array elements

#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:

enter limitations of array n = 5


enter array elements : 12 45 -10 15 60
array elements are : 12 45 -10 15 60
perform sum of array elements
sum of array elements is : 122

p) write a program to concatenate two arrays

#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:

enter limitations of array n = 4


enter array a elements : 12 34 45 65
array a elements are : 12 34 45 65
enter limitations of array m = 5
enter array b elements : 21 43 54 56 11
array b elements are : 21 43 54 56 11
perform concatenation of arrays
concatenated array is : 12 34 45 65 21 43 54 56 11

p) write a program to find minimum and maximum value of array elements

#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:

enter number of values 5


enter values
a[0]=10
a[1]=20
a[2]=8
a[3]=45
a[4]=6
searching for minimum & maximum values from given array
minimum of given values is 6
maximum of given values is 45

P) Write a program to implement the linear search program

#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:

Enter no. of array elements is : 5


Enter the array elements: 5 4 8 9 2
Enter searching element is : 2
Searching element is find at position is : 5
Output:

Enter no. of array elements is : 5


Enter the array elements: 5 4 8 9 2
Enter searching element is : 10
searching element not find

p) Write a c program to implement exchange sort

#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:

Enter no. of array elements to sort: 5


Enter the array elements: 7 6 9 15 20
Sorted array is : 6 7 9 15 20

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

p) Write a program to implement the binary search operation using recursive


functions

#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;
}
}
}
}

Enter no. of array elements to sort: 5


Enter the array elements: 7 6 9 15 20
Given Array 7 6 9 15 20
Sorted array is : 6 7 9 15 20

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]

Col1 Col2 Col3 Col4


Row1 X[0][0] X[0][1] X[0][2] X[0][3]

Page 31 of 39
Problem Solving and Computer Programming

Row2 X[1][0] X[1][1] X[1][2] X[1][3]


Row3 X[2][0] X[2][1] X[2][2] X[2][3]

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.

The Two-Dimensional array is a collection of a number of One-Dimensional arrays, which


are placed one after another. For example in the above table each row of a two dimensional array
can be thought of as a single-dimensional array.

Syntax: data_type variable_name[size][size];

Ex: int a[10][10];

Initialization
int a[3][3]={{1,2,3},{4,5,6},{7,9,9}};

write a c-program to display the elements of two dimensional array

#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:

Array Elements Are :


123
456
789

Page 32 of 39
Problem Solving and Computer Programming

Write a program to find trace of matrix

#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

p) Write a program to find transpose of matrices

#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

program to perform transpose of matrix


enter matrix order m and n 3 2
enter matrix elements
12
45
67

transpose of matrix is :
1 4 6
2 5 7

Page 34 of 39
Problem Solving and Computer Programming

THREE OR MULTI DIMENSIONAL ARRAYS

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];

Where si is the size or the Ith dimensions.


The three dimensional array can be initialized as follows.
int a[3][3][3]={
{{1,2,3,
4,5,6,
7,8,9}

{1,2,3,
2,3,4,
6,4,1}

{9,8,7,
6,5,4,
3,2,1}
};

Arrays using functions

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

default: printf("\nenter proper choice ");


}
getch();
}
void matadd()
{
int m,n;
clrscr();
printf("performing addition of matrix");
printf("enter m and n values");
scanf("%d%d",&m,&n);
printf("\nenter a matrix elements ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
printf("\nenter b matrix elements ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
//performing addition operation
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
printf("\n addition of two matrices is \n\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
void matmul()
{
int m,n,p,q,sum;
printf("\n welcome to matrix multiplication");
xx:
printf("\n enter order of first matrix : ");
scanf("%d%d",&m,&n);
printf("\n enter order of second matrix : ");
scanf("%d%d",&p,&q);

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:

performing matrix addition and multiplication using functions


1.matrix addition
2.matrix multiplication
3.exit
enter ur choice 1
performing addition of matrix
enter m and n values2 2

Page 38 of 39
Problem Solving and Computer Programming

enter a matrix elements


12
34
enter b matrix elements
12
34
addition of two matrices is
2 4
6 8
Output2:

performing matrix addition and multiplication using functions


1.matrix addition
2.matrix multiplication
3.exit

enter ur choice 2

welcome to matrix multiplication


enter order of first matrix : 2 3
enter order of second matrix : 3 2

enter first matrix elements :


123
456
enter second matrix elements :
12
34
56

multiplication of matrix is :
22 28
49 64

Page 39 of 39

You might also like