[go: up one dir, main page]

0% found this document useful (0 votes)
6 views4 pages

Program Print

The document contains three C programs: the first implements a linear search algorithm to find an element in an array, the second performs a binary search on a sorted array after sorting it using bubble sort, and the third handles the transpose of a sparse matrix using tuple representation. Each program includes user input for array elements and the element to search or matrix dimensions. The output displays the results of the searches and the tuple representation of the sparse matrix and its transpose.

Uploaded by

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

Program Print

The document contains three C programs: the first implements a linear search algorithm to find an element in an array, the second performs a binary search on a sorted array after sorting it using bubble sort, and the third handles the transpose of a sparse matrix using tuple representation. Each program includes user input for array elements and the element to search or matrix dimensions. The output displays the results of the searches and the tuple representation of the sparse matrix and its transpose.

Uploaded by

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

// LINEAR SEARCH

#include <stdio.h>
void main() {
int array[100], n, i, key;

// Input: number of elements


printf("Enter number of elements: ");
scanf("%d", &n);

// Input: array elements


printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{scanf("%d", &array[i]);}

// Input: element to be searched


printf("Enter the element to search: ");
scanf("%d", &key);

// Searching using Linear Search


for (i = 0; i < n; i++) {
if (array[i] == key) {
printf("Element found at position %d \n", i + 1);
break;}}
if (i==n) {
printf("Element not found in the array.\n");}}
// BINARY SEARCH (UNSORTED ARRAY)

#include <stdio.h>
void main()
{int left=0,right,middle,n,key,i,a[50],j,temp;

//Input : Number of elements


printf ("Enter the number of elements: ");
scanf ("%d",&n);

//Input : Array elements


printf ("Enter the elements: \n");
for (i=0;i<n;i++)
scanf ("%d",&a[i]);

//Input:Element to search
printf("Enter the element to be searched: ");
scanf ("%d",&key);

//sorting in Ascending order using Bubble Sort


for (i=0;i<(n-1);i++)
{for (j=0;j<(n-i-1);j++)
{if (a[j]>a[j+1])
{temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;}}}

//Printing elements sorted in Ascending order


printf ("The elements sorted in ascending order is : \n");
for (i=0;i<n;i++)
printf ("%d ",a[i]);

//Binary Search

left=0;
right=n-1;
while (left<=right)
{middle=(left+right)/2;
if (a[middle]==key)
{printf("\nElement found at point %d in the sorted list",middle+1);
break;}
else if (a[middle]>key)
right =middle-1;
else
left=middle+1;}

if (left>right)
printf ("Element not found in the list");}
//TRANSPOSE OF A SPARSE MATRIX

#include <stdio.h>
void main ()
{int matrix[10][10],tuple[50][3],transpose[50][3],m,n,i,j,k=1;

//Taking Inputs
printf ("Enter the number of rows and columns: ");
scanf ("%d%d",&m,&n);
printf ("Enter the elements:\n");
for (i=0;i<m;i++)
for (j=0;j<n;j++)
scanf ("%d",&matrix[i][j]);

//Tuple representation of the Sparse matrix taken from user


tuple[0][0]=m;tuple[0][1]=n;
for (i=0;i<m;i++)
{for (j=0;j<n;j++)
{if (matrix[i][j]!=0)
{tuple[k][0]=i;tuple[k][1]=j;
tuple[k][2]=matrix[i][j];
k=k+1;}}}
tuple [0][2]=k-1;

// Printing the Tuple representation of original Sparse matrix


printf ("Tuple representation of the sparse matrix is:\n");
for (i = 0; i < k; i++)
{for (j = 0; j < 3; j++)
printf("%d\t", tuple[i][j]);
printf("\n");}

// Finding Transpose of the Sparse matrix


//Swapping rows and columns
transpose[0][0]= tuple[0][1];
transpose[0][1]=tuple[0][0];
transpose[0][2]=tuple[0][2];

for (i=1;i<k;i++)
{transpose[i][0]=tuple[i][1];
transpose[i][1]=tuple[i][0];
transpose[i][2]=tuple[i][2];}
//Sorting by row and then by column
for (i=1;i<k-1;i++)
{for (j=i+1;j<k;j++)
if (transpose [i][0]>transpose[j][0] || (transpose[i][0]== transpose [j][0] &&
transpose [i][1]>transpose[j][1]))
{int temp0=transpose[i][0];
int temp1=transpose[i][1];
int temp2=transpose[i][2]

transpose[i][0]=transpose[j][0];
transpose[i][1]=transpose[j][1];
transpose[i][2]=transpose[j][2];

transpose [j][0]=temp0;
transpose [j][1]=temp1;
transpose [j][2]=temp2;}}

//printing the tuple representation of transpose of the sparse matrix


printf ("Tuple representation of the TRANSPOSE of the Sparse matrix:\n");
for (i=0;i<k;i++)
{for (j=0;j<3;j++)
printf ("%d\t",transpose[i][j]);
printf ("\n");}}

You might also like