Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Jaypee University of Engineering and

Technology
Name – Abhishek Sharma
Batch – B1 – BX1
Er. No. – 201B011

Data Structures Lab 5


1. Write a program to implement binary search algorithm.
Assume user will enter the sorted array.
Code in C

#include <stdio.h> 
#include<conio.h>
void fun_binary_sorted(int a[100], int n, int item)
{
    int low = 0, high, mid = 0, pos = 0;
    high = n - 1;
    while (low <= high)
    {
        mid = (low + high) / 2;
        if (a[mid] == item)
        {
            pos = mid;
            break;
        }
        else if (item < a[mid])
        {
            high = mid - 1;
        }
        else
        {
            low = mid + 1;
        }
    }
    if (pos < 0)
    {
        printf("element is not found \n ");
    }
    else
        printf("element is found at position %d", pos);
}

int main()
{
    int n, a[100], item;
    printf("enter the size of the array \n ");
    scanf("%d", &n);
    printf("enter the elements of the array :\n");
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    printf("enter the element , which you want to search in the array   : \
n");
    scanf("%d", &item);
    fun_binary_sorted(a, n, item);
    return 0;
}

OUTPUT –

Q2. Write a function which accepts an array of integers along


with the size of it. The numbers
are arranged in the list in increasing order until a particular index
and after that it is
arranged in decreasing order. This function should find
and return the index position at
which the increasing list starts decreasing. Call this function
from main function ?

Code in C
#include <stdio.h> 
#include<conio.h>
void fun(int a[100], int n)
{
    int i, count = 0;
    for (i = 0; i <= n - 2; i++)
    {
        if (a[i] < a[i + 1] && a[i + 1] > a[i + 2])
            count = a[i + 2];
    }
    if (count == 0)
        printf("does not found the index position at which the increasing list 
starts decreasing.");
    else
        printf("%d", count);
}

int main()
{
    int a[100], n;
    printf("enter the size of the array : \n");
    scanf("%d", &n);
    printf("enter the elements of the array : \n");
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    fun(a, n);
    return 0;
}

Output –
Q3. Write a program to check whether given Matrix
is sparse or not. We say a matrix as
sparse when more than 50% of total elements are zero. If
matrix is sparse then represent
it in triplet form with the help of array data structure.
Also print the number of bytes that
are saved or wasted when you represent input matrix
in the triplet form ?
Code in C

#include <stdio.h> 
#include<conio.h> 
int main()

{
    int a[100][100], n, x, i, j, count = 0, y;
    printf("enter the size of the row and column : \n");
    scanf("%d %d", &n, &x);
    printf("enter the elements of the array : \n");
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < x; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < x; j++)
        {
            if (a[i][j] == 0)

                count++;
        }
    }
    y = (n * x) / 2;
    if (count > y)
    {
        printf("the sparse matrix is : \n");
        printf("%5d %5d %5d \n", n, x, (n * x) - count);
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < x; j++)
            {
                if (a[i][j] > 0)
                {
                    printf("%5d %5d %5d \n", i, j, a[i][j]);
                }
            }
        }
    }
    else
        printf("given array can not convert in sparse matrix \n ");
    return 0;
}

Output –

Q4. Write a time efficient program for finding the element


which appears maximum number of times in the array.
ample input: 2, 4, 5, 6, 8, 9, 10, 13, 2, 3, 2
Sample output: 2 [as 2 is coming three times]
Code in C
#include <stdio.h> 
#include<conio.h> 
int main()
{
    int a[100], n, i, j, count = 1, max = 0, maxele;
    printf("enter the size of the array : \n");
    scanf("%d", &n);
    printf("enter the elements of the array : \n");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    for (i = 0; i < n; i++)

    {
        for (j = i + 1; j < n; j++)
        {
            if (a[j] == a[i])
            {
                count++;
                if (count > max)
                {
                    maxele = a[j];
                }
            }
        }
    }
    if (count > 1)
printf("maximum number repeating element is %d",maxele); 
return 0;
}

Output –

You might also like