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

DATA (AutoRecovered)

Uploaded by

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

DATA (AutoRecovered)

Uploaded by

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

Date: __/__/____ Page no.

: ___

Program 1

 AIM : Write a program to insert an element at a user-defined


position in an linear array.

 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

 AIM : Write a program to delete an element from a user-defined


position in an linear array.

 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]); }

printf("\n Name—Tejsvi Bhat , Roll no.—03813202722");


getch();
return 0;
}
Date: __/__/____ Page no. : ___
Date: __/__/____ Page no. : ___

Program 3

 AIM : Write a program to search an element in an linear array


using Linear Search.

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

printf("\n Name—Tejsvi Bhat , Roll no.—03813202722");


getch();
return 0;
}
Date: __/__/____ Page no. : ___
Date: __/__/____ Page no. : ___

Program 4

 AIM : Write a program to search an element in an linear array


using Binary Search.

 Code :
//TO SEARCH AN ELEMENT IN A LINEAR ARRAY USING BINARY SEARCH
#include<stdio.h>
#include<conio.h>

int binary_search(int arr[],int beg, int end, int x)


{
int mid;
while(beg <= end)
{ mid = (beg + end)/2;

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

printf("\n Name—Tejsvi Bhat , Roll no.—03813202722");


getch();
return 0;
}
Date: __/__/____ Page no. : ___

Program 5

 AIM : Write a program to sort a linear array using Bubble Sort.

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

printf("\n Name—Tejsvi Bhat , Roll no.—03813202722");


getch();
return 0;
}
Date: __/__/____ Page no. : ___
Date: __/__/____ Page no. : ___
Date: __/__/____ Page no. : ___

Program 6

 AIM :Write a program to implement Sparse Matrix using Matrix


Representation.

 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. : ___

}
}

printf("\nImplementing sparse matrix using matrix representation:\n");


for(i=0 ; i<t+1 ; i++)
{ printf("%d %d %d\n", arr2[i][0], arr2[i][1], arr2[i][2]); }

printf("\n Name—Tejsvi Bhat , Roll no.—03813202722");


getch();
return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy