DATA (AutoRecovered)
DATA (AutoRecovered)
: ___
Program 1
Code :
// TO INSERT AN ELEMENT ON A USER-DEFINED POSITION IN A GIVEN
LINEAR ARRAY
#include <stdio.h>
#include <conio.h>
int main()
{
int data[1000], n, i, k, j, item;
clrscr();
printf("Enter the size of array: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++)
scanf("%d", &data[i]);
printf("Enter the position at which the element is to be inserted:");
scanf("%d", &k);
printf("Enter the element at which is to be inserted: ");
scanf("%d", &item);
for (j = n; j >= k - 1; j--)
data[j + 1] = data[j];
data[k - 1] = item;
n = n + 1;
printf("After insertion the array will look like:-\n");
for (i = 0; i < n; i++)
printf(" %d ", data[i]);
printf("\n Name—Tejsvi Bhat , Roll no.—03813202722");
getch();
return 0;
}
Date: __/__/____ Page no. : ___
Program 2
Code :
//TO DELETE AN ELEMENT FROM A USER-DEFINED POSITION IN A GIVEN
LINEAR ARRAY
#include<stdio.h>
#include<conio.h>
int main()
{
int data[1000], n, i, k, j, item;
clrscr();
printf("Enter the size of array: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for(i=0; i<n; i++)
{ scanf("%d", &data[i]); }
printf("Enter the position from which element is to be deleted: ");
scanf("%d", &k);
item = data[k-1];
for(j = k-1 ; j < n ; j++)
{ data[j] = data[j+1]; }
n = n - 1;
printf("After deletion the array will look like:-\n");
for(i = 0; i < n; i++)
{ printf(" %d ", data[i]); }
Program 3
Code :
//TO SEARCH AN ELEMENT IN A LINEAR ARRAY USING LINEAR SEARCH
#include<stdio.h>
#include<conio.h>
int main()
{
int data[1000], n, i, item, k, loc = 0;
clrscr();
printf("Enter the size of array: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for(i=0; i<n; i++)
{ scanf("%d", &data[i]); }
printf("Enter the element you want to search in the given array: ");
scanf("%d", &item);
for(k = 1; k <= n; k++)
{ if(item == data[k])
{ loc = k ; }
}
if( loc == 0)
{ printf("The element is not in the given array."); }
else
{
printf("Element %d is present at the %dth position in the
array.",item,loc+1);
}
Program 4
Code :
//TO SEARCH AN ELEMENT IN A LINEAR ARRAY USING BINARY SEARCH
#include<stdio.h>
#include<conio.h>
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
end = mid - 1;
else
beg = mid + 1;
}
return -1;
}
int main()
{
int data[1000], n, i, result, item;
clrscr();
printf("Enter the size of array: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for(i=0; i<n; i++)
{ scanf("%d", &data[i]); }
Date: __/__/____ Page no. : ___
printf("Enter the element you want to search in the given array: ");
scanf("%d", &item);
result = binary_search(data, 0, n-1, item);
if(result == -1)
{
printf("The given element %d is not present in the array.", item);
}
else
{
printf(" Element %d is present in the array at position %d.", item,
result+1);
}
Program 5
Code :
// TO SORT A LINEAR ARRAY USING BUBBLE SORT
#include<stdio.h>
#include<conio.h>
int main()
{
int data[1000], n, i, k, j, item;
clrscr();
printf("Enter the size of array: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for(i=0; i<n; i++)
{ scanf("%d", &data[i]); }
printf("Enter the position at which the element is to be inserted:");
scanf("%d", &k);
printf("Enter the element at which is to be inserted: ");
scanf("%d", &item);
for(j = n; j >= k-1 ; j--)
{ data[j+1] = data[j]; }
data[k-1] = item;
n = n + 1;
printf("After insertion the array will look like:-\n");
for(i = 0; i < n; i++)
{ printf(" %d ", data[i]);
Program 6
Code :
//TO IMPLEMENT SPARSE MATRIX USING MATRIX REPRESENTATION
#include<stdio.h>
#include<conio.h>
int main()
{
int m, n, i, j, q, a, b;
int arr1[10][10], arr2[10][3], t = 0;
clrscr();
printf("Enter the number of rows and columns: ");
scanf("%d %d", &m, &n);
printf("Enter the elements of the matrix: \n");
for(i=0 ; i<m ; i++)
{ for(j=0 ; j<n ; j++)
{ scanf("%d", &arr1[i][j]);
if(arr1[i][j] != 0)
{ t++; }
}
}
arr2[0][0] = m;
arr2[0][1] = n;
arr2[0][2] = t;
q = 1;
for(i=0 ; i<m ; i++)
{ for(j=0 ; j<n ; j++)
{ if(arr1[i][j] != 0)
{ arr2[q][0] = i;
arr2[q][1] = j;
arr2[q++][2] = arr1[i][j];
}
Date: __/__/____ Page no. : ___
}
}