Unit 02
Unit 02
for (i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
#include <stdio.h>
int main()
{
int arr[100];
int i, item, pos, n;
printf("Enter the number of elements: ");
scanf(“%d”,&n);
printf (“Enter the elements”);
for (i = 0; i <n; i++)
scanf("%d",&arr[i]);
printf ("Array before insertion: ");
for (i = 0; i < n; i++)
printf ("%d ", arr[i]);
printf("\n");
printf ("Enter the element to be inserted: ");
scanf("%d",&item);
printf ("Enter the position at which the element is to be inserted: ");
scanf("%d",&pos);
size++;
return 0;
}
#include<stdio.h>
int main()
{
int key, i, pos = -1, size=5;
int arr[5] = {1, 20, 5, 78, 30};
if(pos != -1)
{
//shift elements backwards by one position
for(i = pos; i < size - 1; i++)
arr[i] = arr[i+1];
return 0;
}
Sorting:
Sorting is an operation of array, where it is a process of arranging the
elements in either ascending or descending order.
Write selection, bubble, insertion, quick, merge sort lab programs
in notes.
Searching:
Searching is an operation of array, where it a process of finding the
element in the array, while searching the element may or may not be
present in the array.
Write linear search and binary search lab programs in notes.
Sparse matrix:
Sparse matrices are those matrices that have the majority of their
elements equal to zero. In other words, the sparse matrix can be defined
as the matrix that has a greater number of zero elements than the non-
zero elements.
Let's understand the array representation of sparse matrix with the help
of the example given below -
In the above figure, we can observe a 5x4 sparse matrix containing 7 non-
zero elements and 13 zero elements. The above matrix occupies 5x4 =
20 memory space. Increasing the size of matrix will increase the wastage
space.
Stack:
Stack is a linear data structure based on LIFO(Last In First Out) or
(FILO)First in Last out principle in which the insertion of a new element
and removal of an existing element takes place at the same end
represented as the top of the stack.
Program
#include <stdio.h>
#include <stdlib.h>
#define SIZE 4
void push();
void pop();
void show();
int main()
int choice;
while (1)
scanf("%d", &choice);
switch (choice)
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
exit(0);
default:
printf("\nInvalid choice!!");
void push()
int x;
if (top == SIZE - 1)
printf("\nOverflow!!");
else
scanf("%d", &x);
top = top + 1;
arr[top] = x;
}
}
void pop()
if (top == -1)
printf("\nUnderflow!!");
else
top = top - 1;
void show()
if (top == -1)
printf("\nUnderflow!!");
else