0% found this document useful (0 votes)
1 views12 pages

array

ARRAY DEFINITION, TYPES OF ARRAYS AND PROGRAMS

Uploaded by

principal.sbvrdc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
1 views12 pages

array

ARRAY DEFINITION, TYPES OF ARRAYS AND PROGRAMS

Uploaded by

principal.sbvrdc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

UNIT-III

An array is a collection of elements of same type that share a common name. The elements of
the array are stored in consecutive memory locations and are referred by an index (also
known as subscript).
Array types:
1. One Dimensional Arrays
2. Multi Dimensional Arrays

ONE DIMENSIONAL ARRAYS


When array is declared with only one dimension (subscript) then it is called one dimensional
array or single dimensional array.

Declaration of one-dimensional array:


Syntax:
Type array_name[size];
1. The type specifies the type of element that will be contained in the array, such as int,
float, or char.
2. Size indicates the maximum number of elements that can be stored inside the array.
3. Array_name is an identifier that specifies the name of the array.
Ex:
int arr[5];
0 1 2 3 4

a[0] a[1] a[2] a[3] a[4]

Initialization of arrays:
After an array is declared, its elements must be initialized. Otherwise, they will contain
garbage value.
Arrays can be initialized as follows:
Syntax:
Datatype array_name[size]={elements separated by commas};
Ex:
int arr[5]={5,7,3,9,1};

5 7 3 9 1
arr[0] arr[1] arr[2] arr[3] arr[5]

Accessing array elements:


Once an array is defined, its elements can be accessed by using an index or subscript.
Syntax: arr_name[index];
Example: arr[4];
Points to the element five of the given list.

1
UNIT-III

STORING VALUES IN ARRAY


When we declare and define an array, we are just allocating space for the elements. No
elements are stored in the array. To store values in the array, there are three ways-
First, to initialize the array elements:
type array_name[size]={list of values};
Ex: int arr[4]={1,2,4,5,8};
Second, to input values for every individual element
int mark[6];
for (i=0;i<6;i++)
scanf(“%d”,&a[i]);
Third, to assign values to the elements
Ex: int a[0]=3;
int a[1]=4;
int a[2]=5;

CALCULATING THE LENGTH OF THE ARRAY


Length of the array is given by the number of elements stored in it. The general formula to
calculate the length of the array is,
Length=upper_bound-lower_bound+1
Where upper_bound is the index of the last element and lower_bound is the index of the first
element in the array.

OPERATIONS THAT CAN BE PERFORMED ON ARRAYS


There are number of operations that can be performed on arrays. These operations include:
1. Traversal
2. Insertion
3. Search
4. Deletion
5. Merging
6. Sorting

1. TRAVERSAL :
Traversing the array elements means accessing each and every element of the array for a
specific purpose. If arr_name is an array name then, traversing the data elements of an array
includes:
1. Printing every element in the array.
2. Counting the total number of elements in the array.
3. Performing any processing on elements of the array.
Example: write a program to accept elements into the array and print them
#include <stdio.h>
#include<conio.h>
main() {
int arr[10];
int size, i;

2
UNIT-III

clrscr(); Output:
printf(" Enter the size of an array");
Enter the size of an array 5
scanf(" %d",&size);
printf(" Enter %d elements into array \n",size); Enter 5 elements into array
for(i=0;i<size;i++)
{ arr[0] = 1
printf("\n arr[%d] =",i); arr[1] = 2
scanf("%d",&arr[i]); arr[2] = 3
} arr[3] = 4
printf(" \n The array elements are \n");
arr[4] = 5
for(i=0;i<size;i++)
{ The array elements are
printf(" arr[%d] = %d",i,arr[i]); arr[0] = 1
} arr[1] = 2
getch(); arr[2] = 3
} arr[3] = 4
arr[4] = 5
2. INSERTION:
Inserting an element in the array means adding a new data element in an already existing
array.
Example:
#include<stdio.h>
#include<conio.h> Output:
main() Enter the size of an array 5
{ Enter the elements for an array 1
int arr[10];
2
int pos,size,i,num;
printf(" Enter the size of an array "); 3
scanf("%d",&size); 4
printf(" \n Enter the elements for an array "); 5
for(i=0;i<size;i++)
{ Enter the number to be inserted :8
scanf("%d",&arr[i]);
Enter the position to insert the number :2
}
printf("\n Enter the number to be inserted :"); Elements after insertion are 1 2 8 3 4 5
scanf("%d",&num);
printf("\n Enter the position to insert the number :");
scanf("%d",&pos);
for(i=size;i>=pos;i--)
{
arr[i+1]=arr[i];
}
arr[pos]=num;

printf("\n Elements after insertion are " );


for(i=0;i<size+1;i++)
printf(" %d ",arr[i]);
getch();
}

3
UNIT-III

3. DELETION
Deleting an element from the array means removing a data element from an already existing
array.
// program to delete an element from a given location in an array
#include<stdio.h>
#include<conio.h>
main()
{
int arr[10],i,n,pos;
Output:
printf("\n Enter the size of an array ");
scanf("%d",&n); Enter the size of an array 5
printf("\n Enter the elements of the array "); Enter the elements of the array 1
for(i=0;i<n;i++) 2
{ 3
scanf("%d",&arr[i]); 4
} 5
printf("\n Enter the position to delete ");
scanf("%d",&pos);
for(i=pos;i<n;i++) Enter the position to delete 2
arr[i]=arr[i+1];
n--; The array after deletion is :
printf(" \n The array after deletion is :"); a[0] = 1
for(i=0;i<n;i++) a[1] = 2
printf("\n a[%d] = %d",i,arr[i]); a[2] = 4
getch();
a[3] = 5
}

4. MERGING
Merging two arrays in a third array means first copying the contents of the first array into the
third array and then copying the contents of the second array into the third array. Hence, the
merged array contains contents of the first array followed by the contents of the second array.
Program to merge two unsorted arrays
#include<stdio.h>
#include<conio.h>
main()
Output:
{
int arr1[10],arr2[10],arr3[20]; Enter the size of array1: 3
int n1,n2,index=0,i,m; Enter the elements of array1: 1
printf(" \n Enter the size of array1: "); 2
scanf("%d",&n1); 3
printf(" \n Enter the elements of array1: "); Enter the size of array2: 3
for(i=0;i<n1;i++) Enter the elements of array2: 4
scanf("%d",&arr1[i]);
5
printf("\n Enter the size of array2: ");
scanf("%d",&n2); 6
printf("\n Enter the elements of array2: "); The merged array is
for(i=0;i<n2;i++) arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 4
scanf("%d",&arr2[i]); arr[4] = 5 arr[5] = 6
4
UNIT-III

m=n1+n2;
for(i=0;i<n1;i++)
{
arr3[index]=arr1[i];
index++;
}
for(i=0;i<n2;i++)
{
arr3[index]=arr2[i];
index++;
}
printf(" \n The merged array is \n");
for(i=0;i<m;i++)
{
printf("arr[%d] = %d ",i,arr3[i]);
}
}

5. SEARCHING
Searching means to find whether a particular value is present in the array or not. There are
two popular methods for searching the array elements. One is linear search and the second is
binary search.
Linear search: We can call this as sequential search. In this method we are going to search
the element in sequence order.

Write a program to implement linear search


#include<stdio.h>
#include<conio.h>
main()
{ Output:
int arr[10]; Enter the size of an array :6
int n,i,num,flag=0; Enter the elements for an array :
clrscr(); 45
printf(" \n Enter the size of an array :"); 62
scanf("%d",&n); 35
printf(" \n Enter the elements for an array :\n");
for(i=0;i<n;i++) 48
scanf("%d",&arr[i]); 76
printf("\n Enter the number to search :"); 4
scanf("%d",&num); Enter the number search :35
for(i=0;i<n;i++) 35 is found at position 2
{
if(arr[i]==num)
{

flag=1;
break;
}
}

5
UNIT-III

if(flag==1)

printf(" %d is found at position %d",num,i);


else
printf(" Does not found in the array ");
getch();
}

Binary search: Binary search method is fast and efficient. Sort the list.
In this method, to search an element we compare it with the element present at the centre of
the list. It matches then the search is successful. Otherwise, the list is divided into two halves.
One from 0th element to the centre element and another from centre element to the last
element.
Write a program to implement binary search
#include<stdio.h>
#include<conio.h>
main()
{
int arr[10],num,i,n,pos=-1,beg,end,mid,found=0;
printf("\n Enter the number of elements in the array : ");
scanf("%d",&n);
printf("\n Enter the elements :");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("\n Enter the number that has to be searched : ");
scanf("%d",&num);
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(arr[mid]==num)
{
printf("\n %d is present in the array at position= %d",num,mid);
found=1;
break;
}
if(arr[mid]>num)
end=mid-1;
else if(arr[mid]<num)
beg=mid+1;
}
if(beg>end && found==0)
printf("\n %d does not exist in the array :",num);
getch();
}

6
UNIT-III

6. SORTING
Sorting means arranging a set of data in an order, either in ascending or in descending order.
Example:
#include<stdio.h>
#include<conio.h>
main()
{
int arr[10],i,j,n;
clrscr();
printf("\n Enter the array size ");
Output:
scanf("%d",&n);
Enter the array size 6
printf("\n Enter the elements :");
for(i=0;i<n;i++) Enter the elements :4
{ 8
scanf("%d",&arr[i]); 1
} 3
printf(" \n Elements after sorting are : \n "); 7
for(i=0;i<n;i++) 2
{
for(j=i+1;j<n;j++)
{ Elements after sorting are :
if(arr[i]>arr[j]) 1 2 3 4 7 8
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("%3d",arr[i]);
}
getch();
}

ONE-DIMENSIONAL ARRAYS FOR INTER-FUNCTION COMMUNICATION


Like variables of other data types, we can also pass an array to a function.
Example:
#include<stdio.h>
#include<conio.h>
void read_array(int arr[],int);
void dis_array(int arr[],int);
main()
{
int num[10],n;
clrscr();
printf("\n Enter the size of an array :");
scanf("%d",&n);

7
UNIT-III

read_array(num,n);
dis_array(num,n);
getch();
}
void read_array(int arr[10],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n array[%d]= ",i);
scanf("%d",&arr[i]);
}
}
void dis_array(int arr[10],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n array[%d]=%d ",i,arr[i]);
}
}

TWO-DIMENSIONAL ARRAY
When an array uses only two subscripts then it is called “Two dimensional array”. It can be
viewed as table of elements, which contains rows and columns. A Two dimensional array is
useful for matrix operations.

Declaration of two-dimensional arrays:


Syntax: data_type array_name[row_size][col_size]
Example: int arr[3][3];

Initialization of 2-D arrays:


int marks[2][3]={90,85,74,88,66,86};
int marks[2][3]={{86,89,66},{56,75,78}};

0 1 2

0
1
Two rows and three columns marks[2][3].
If we omit values to an array default it is going to take a value 0.

8
UNIT-III

Write a program to accept array elements and print them in matrix form
#include<stdio.h>
#include<conio.h>
main() Output:
{
int a[3][3],i,j; Enter the elements for Matrix A 1
clrscr(); 2
printf(" \n Enter the elements for Matrix A "); 3
for(i=0;i<3;i++) 4
{ 5
for(j=0;j<3;j++) 6
{ 7
scanf("%d",&a[i][j]); 8
} 9
} The elements of Matrx A are :
printf("\n The elements of Matrx A are :\n"); 1 2 3
for(i=0;i<3;i++)
{ 4 5 6
for(j=0;j<3;j++)
{ 7 8 9
printf("%3d",a[i][j]);
}
printf("\n");
}
getch();
}

OPERATIONS ON 2-D ARRAYS


We can perform the following operations on a two-dimensional array:
Write a program to transpose 3 × 3 matrix
Output:
#include<stdio.h>
#include<conio.h> Enter the Elements for the Matrix A :1
main()
{ 2
int a[3][3],i,j; 3
clrscr() 4
printf("\n Enter the Elements for the Matrix A :"); 5
for(i=0;i<3;i++) 6
{ 7
for(j=0;j<3;j++) 8
{ 9
scanf("%d",&a[i][j]); Elements of Matrix A are:
} 1 2 3
} 4 5 6
printf("\n Elements of Matrix A are: \n "); 7 8 9
for(i=0;i<3;i++) After transpose the elements in matrix A
{ are
for(j=0;j<3;j++)
1 4 7
{
2 5 8
3 6 9
9
UNIT-III

printf("%3d",a[i][j]);
}
printf("\n");
}

printf("\n After transpose the elements in matrix A are \n");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%3d",a[j][i]);
}
printf("\n");
}
getch();
} Output:

Write a program to sum two matrices Enter the Elements for the Matrix A :1
#include<stdio.h> 2
#include<conio.h> 3
main() 4
{ 5
int a[3][3],b[3][3],c[3][3],i,j; 6
clrscr(); 7
printf("\n Enter the Elements for the Matrix A :"); 8
for(i=0;i<3;i++) 9
{ Enter the Elements for the Matrix B :1
for(j=0;j<3;j++) 2
{ 3
scanf("%d",&a[i][j]); 4
} 5
6
} 7
printf("\n Enter the Elements for the Matrix B :"); 8
for(i=0;i<3;i++) 9
{ Elements of Matix A are:
for(j=0;j<3;j++) 1 2 3
{ 4 5 6
scanf("%d",&b[i][j]); 7 8 9
}
Elements of Matix B are:
}
printf("\n Elements of Matix A are: \n "); 1 2 3
4 5 6
for(i=0;i<3;i++) 7 8 9
{
for(j=0;j<3;j++) Elements after the sum of two matrices
{ are:
printf("%3d",a[i][j]);
} 2 4 6
printf("\n"); 8 10 12
14 16 18
10
UNIT-III

}
printf("\n Elements of Matix B are: \n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%3d",b[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n Elements after the sum of two matrices are: \n");

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%3d",c[i][j]);
}
printf("\n");
}
getch();
}

Write a program to subtract two matrices


#include<stdio.h>
#include<conio.h>
main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("\n Enter the Elements for the Matrix A :");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}

}
printf("\n Enter the Elements for the Matrix B :");
for(i=0;i<3;i++)
{

11
UNIT-III

for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n Elements of Matix A are: \n ");
Output:
for(i=0;i<3;i++)
{ Enter the Elements for the Matrix
for(j=0;j<3;j++) A :4
{ 5
printf("%3d",a[i][j]); 6
} 7
printf("\n"); 8
} 9
printf("\n Elements of Matix B are: \n"); 10
11
for(i=0;i<3;i++) 12
{
for(j=0;j<3;j++) Enter the Elements for the Matrix
{ B :1
printf("%3d",b[i][j]); 2
} 3
printf("\n"); 4
} 5
for(i=0;i<3;i++) 6
{ 7
for(j=0;j<3;j++) 8
{ 9
c[i][j]=a[i][j]-b[i][j];
} Elements of Matix A are:
} 4 5 6
printf("\n Elements after the subtraction of two matrices7are:
8 \n");
9
10 11 12
for(i=0;i<3;i++)
{ Elements of Matix B are:
for(j=0;j<3;j++) 1 2 3
{ 4 5 6
printf("%3d",c[i][j]); 7 8 9
}
printf("\n");
Elements after the subtraction of two
}
matrices are:
getch();
3 3 3
}
3 3 3
3 3 3

12

You might also like