5 Arrays in C
5 Arrays in C
5 Arrays in C
• All the values from index at which a new element is to be inserted to last
element are to be shifted down which is time consuming operation.
• If array is already full, then more elements cannot be added as there is no
space left at the end.
• Array insertion does not mean increasing its size. i.e., array can not
contain (11th ) element if its size is 10.
Write a program to insert an element into one dimensional array.
#include<stdio.h> scanf(“%d”,&value);
void main() { for(i=n-1; i>=position; i--) {
int a[10],position,i,n,value; a[i+1]=a[i]; }
printf(“Enter size of array: ”); a[position]=value;
scanf(“%d”,&n); n=n+1;
printf(“Enter array elements: \n”); printf(“\nResultant array is: \n”);
for(i=0; i<n; i++) { for(i=0; i<n; i++) {
scanf(“%d”,&a[i]); } printf(“a[%d] = %d\n”,i,a[i]); }
printf(“Enter the position: ”); }
scanf(“%d”,& position);
printf(“Enter the value you want to insert:”);
OUTPUT
Enter size of array: 5
Enter array elements:
10 20 30 40 50
Enter the position: 2
Enter the value want to insert: 100
Resultant array is:
a[0]=10
a[1]=20
a[2]=100
a[3]=30
a[4]=40
a[5]=50
ARRAY OPERATION – SEARCHING
• The search operation is used to find whether the desired data is present in a set
of data or not (present in array or not).
• The search operation which compares the given data with each data in an array
and if its matches with any of them it’s position will be printed otherwise "Data
not found" message will displayed.
Write a program to search a given value in a one dimensional array.
#include<stdio.h> for(i=0; i<n; i++) {
void main() { if (a[i]==s) {
int a[10], s, i, n; printf(“%d is present at location a[%d]”,s,i);
printf(“Enter size of array: ”); break;
scanf(“%d”,&n); } }
printf(“Enter array elements: ”); if(i==n) {
for(i=0; i<n; i++) { printf(“data not found”);
scanf(“%d”,&a[i]); } }
printf(“Enter a number to search: ”); }
scanf(“%d”,&s);
OUTPUT
Enter size of array: 5
Enter array elements:
10
20
30
40
50
Enter a number to search: 20
20 is present at location a[1]
ARRAY OPERATION – MERGING
• The Merging operation joins two array one after another. It combines two arrays
into large array.
Write a program to merge two one-dimensional array.
#include<stdio.h> printf(“Enter second array elements:\n”);
void main() { for(i=0; i<n2; i++) {
int a[20], b[20], i, n1, ,n2; scanf(“%d”,&b[i]); }
printf(“Enter the size of first array:\n”); n2=n1+n2;
scanf(“%d”,&n1); for(i=0; i< n2; i++) {
printf(“Enter the size of second array:\”); a[n1+i] = b[i]; }
scanf(“%d”,&n2); printf(“Merged elements are: \n”);
printf(“Enter first array elements:\n”); for(i=0; i<n2; i++) {
for(i=0; i<n1; i++) { printf(“%d ”,a[i]); }
scanf(“%d”,&a[i]); }
}
OUTPUT
Enter the size of first array : 3
Enter the size of second array: 4
Enter first array elements:
10 20 30
Enter second array elements:
40 50 60 70
Merge elements are:
10 20 30 40 50 60 70
ARRAY OPERATION – SORTING
• The sorting operation is very frequently used on arrays to arrange them in
either ascending or descending order.
• The sorting is performed by comparing and exchanging the elements of an
array if they are not in order.
Write a program to sort array in ascending order
#include<stdio.h> for(i=0; i<n; i++) {
void main() { for(j=i+1; j<n; j++) {
int a[20]; if(a[j] < a[i]) {
int i, n, j, temp; temp=a[i];
printf(“Enter the size of array:”); a[i]=a[j];
scanf(“%d”,&n); a[j]=temp;
printf(“Enter array elements:”); }}}
for(i=0; i<n; i++) { printf(“\n Array in ascending order:”);
scanf(“%d”,&a[i]); } for(i=o; i<n; i++) {
printf(“%d\n”,a[i]);
} }
OUTPUT
Enter the size of array: 3
Enter array elements:
30
10
20
Array in ascending order:
10
20
30
ARRAY OPERATION – DELETION
• Delete operation removes an element from the specified index.
• Deleting an element does not affect the size of the array.
• A user will enter the position/index at which the array element deletion is required.
• It also checks whether deletion is possible or not; for example, if an array contains five
elements and user wants to delete the element at the sixth position, it is not possible.
• In deletion we need to shift array elements which are after the elements to be deleted.
• It’s very inefficient in the size of the array is large or to remove elements from an array
repeatedly.
Write a program to delete an element from one dimensional array.
#include<stdio.h> if(index>n) {
void main() { printf("Deletion not possible\n"); }
int a[10], index, i, n; else {
printf("Enter the size of array:\n"); for(i=index; i<=n; i++) {
scanf("%d",&n); a[i]=a[i+1];
printf("Enter array elements: \n"); }
for(i=0; i<n; i++) { printf("Resultant array: \n");
scanf("%d",&a[i]); } for(i=0; i<n-1; i++) {
printf("Enter the index value : \n"); printf("%d\n",a[i]);
scanf("%d",&index); }
}}
OUTPUT
Enter the size of array:
4
Enter array element:
10 20 30 40
Enter the index value :
2
Resultant array:
10
20
40
TWO-DIMENSIONAL ARRAY
• An Array of Array is known as 2D Array.
• Two dimensional arrays are used to represent the two-dimensional structures like
matrix. In C programming 2D is also known as matrix.
• A matrix can be represented as a table of rows and columns.
• Synatx: Datatype ArrayName [row_size][column_size];
• Where the “row_size” denotes total number of rows and columns and “column_size”
denotes total number of columns.
• Declaration: int a[3][2];
• First subscript (ith) is use for total number of rows and 2nd one (jth) is use for total
number of columns in matrix.
TWO-DIMENSIONAL ARRAY
Datatype arrayname [row_size][column-size];
Example: int a[3][2];
• Here we have 3 rows and 2 columns;
INITILIZATION OF 2D ARRAY
• Initialization of 2D Array:
int a[3][2] = { {10,20}, {30,40}, {50,60} };
OR
int a[3][2] = {
{10,20},
{30,40},
{50,60}
};
OR
• The nested braces, which indicate the intended row, are optional.
int a[3][2]={10,20,30,40,50,60};
SIMPLE PROGRAM OF 2D ARRAY
#include<stdio.h>
void main()
{ Output:
int a[3][2]={10,20,30,40,50,60}; a[0][0]=10
a[0][1]=20
for(i=0; i<3; i++) a[1][0]=30
{ a[1][1]=40
for(j=0; j<2; j++) { a[2][0]=50
printf(“a[%d][%d] = %d \n” , i , j , a[i][j] ); a[2][1]=60
}
}
MATRIX ADDITION OPERATION IN C
A=
B=
C=
C=A+B
ADDITION OF TWO 3 X 3 MATRIX
#include<stdio.h> scanf(“%d”, &b[i][j]); }}
void main() { for(i=0; i<3; i++) {
int a[3][3], b[3][3], add[3][3]; for(j=0; j<3; j++) {
int i,j; add[i][j]=a[i][j]+b[i][j]; }}
printf(“Enter Matrix a:”); printf(“\nAddition of a and b is: ”);
for(i=0; i<3; i++) { for(i=0; i<3; i++) {
for(j=0; j<3; j++) { for(j=0; j<3; j++) {
scanf(“%d”, &a[i][j]); }} printf(“%d ”,add[i][j]); }
printf(“Enter Matrix b:”); printf(“\n”);
for(i=0; i<3; i++) { }
for(j=0; j<3; j++) { }
OUTPUT
Enter Matrix A:
1 2 3
4 5 6
7 8 9
Enter Matrix B:
9 8 7
6 5 4
3 2 1
Addition of matrix A and B is:
10 10 10
10 10 10
10 10 10
N-D ARRAY/ MULTI DIMENSIONAL ARRAY
• The C programming also allows the use of three or more-dimensional arrays which are
known as multi-dimensional arrays or N-D Array.
• SYNTAX: Datatype array_name[d1][d2][d3]……..[dn];
OR
Datatype array_name [size][size][size];
• EXAMPLE:
int a[3][3][3];
OR
int a[2][4][4];
EXAMPLE OF N-D ARRAY
#include<stdio.h>
void main() printf(“Multi-dimensional elements are:\n”);
{ for(i=0; i<3; i++) {
int i, j, k; for(j=0; j<3; j++) {
int a[3][3][3] = for(k=0; k<3; k++) {
{ printf(“%d ”,a[i][j][k]);
{ {1,2,3},{4,5,6}, {7,8,9}}, }
{ {10,11,12},{13,14,15},{16,17,18}}, printf(“\n”); }
{ {19,20,21}, {22,23,24},{25,26,27},} printf(“\n”); }
}; }
OUTPUT
Multi-dimensional elements are:
123
456
789
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
25 26 27
THANK YOU