DS UNIT-I Sample Programs

 

UNIT-I Sample Programs

 a) Implement c program to find min and max element in array

#include <stdio.h>

int main()

{

    int arr1[100];

    int i, mx, mn, n;

    // Prompt user for input

    printf("\n\nFind maximum and minimum element in an array :\n");

    printf("--------------------------------------------------\n");

    printf("Input the number of elements to be stored in the array :");

    scanf("%d", &n);

    // Input elements for the array

    printf("Input %d elements in the array :\n", n);

    for (i = 0; i < n; i++)

        {

            printf("element - %d : ", i);

            scanf("%d", &arr1[i]);

        }

    // Initialize max (mx) and min (mn) with the first element of the array

    mx = arr1[0];

    mn = arr1[0];

    // Traverse the array to find the maximum and minimum elements

    for (i = 1; i < n; i++)

    {

        // Update mx if the current element is greater

        if (arr1[i] > mx)

        {

            mx = arr1[i];

        }

        // Update mn if the current element is smaller

        if (arr1[i] < mn)

        {

            mn = arr1[i];

        }

    }

    // Print the maximum and minimum elements

    printf("Maximum element is : %d\n", mx);

    printf("Minimum element is : %d\n\n", mn);

            return 0;

}

Output:

Find maximum and minimum element in an array :

--------------------------------------------------

Input the number of elements to be stored in the array :5

Input 5 elements in the array :

element - 0 : 10

element - 1 : 15

element - 2 : 5

element - 3 : 26

element - 4 : 36

Maximum element is : 36

Minimum element is : 5

  

b) Implement c program to Matrix Multiplication

    #include<stdio.h>   

    #include<stdlib.h> 

    int main(){ 

    int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;   

    printf("enter the number of row=");   

    scanf("%d",&r);   

    printf("enter the number of column=");   

    scanf("%d",&c);   

    printf("enter the first matrix element=\n");   

    for(i=0;i<r;i++)   

    {   

    for(j=0;j<c;j++)   

    {   

    scanf("%d",&a[i][j]);   

    }   

    }   

    printf("enter the second matrix element=\n");   

    for(i=0;i<r;i++)   

    {   

    for(j=0;j<c;j++)   

    {   

    scanf("%d",&b[i][j]);   

    }   

    }   

    printf("multiply of the matrix=\n");   

    for(i=0;i<r;i++)   

    {   

    for(j=0;j<c;j++)   

    {   

    mul[i][j]=0;   

    for(k=0;k<c;k++)   

    {   

    mul[i][j]+=a[i][k]*b[k][j];   

    }   

    }   

    }   

    //for printing result   

    for(i=0;i<r;i++)   

    {   

    for(j=0;j<c;j++)   

    {   

    printf("%d\t",mul[i][j]);   

    }   

    printf("\n");   

    }   

    return 0; 

    }

 

Output:

enter the number of row=3

enter the number of column=3

enter the first matrix element=

1 1 1

2 2 2

3 3 3

enter the second matrix element=

1 1 1

2 2 2

3 3 3

multiply of the matrix=

6 6 6

12 12 12

18 18 18

 

 C) Find an element in given list of sorted elements in an array using binary search in c

#include <stdio.h> 

int binarySearch(int a[], int beg, int end, int val)   

{   

    int mid;   

    if(end >= beg)    

    {        mid = (beg + end)/2;   

        if(a[mid] == val)   

        {                

            return mid+1;   

        }   

        else if(a[mid] < val)    

        { 

            return binarySearch(a, mid+1, end, val);   

        }   

        else    

        { 

            return binarySearch(a, beg, mid-1, val);   

        }         

    }   

    return -1;    

}  

int main() { 

  int a[] = {11, 14, 25, 30, 40, 41, 52, 57, 70}; 

  int val = 40;

  int n = sizeof(a) / sizeof(a[0]); 

  int res = binarySearch(a, 0, n-1, val); 

  printf("The elements of the array are - "); 

  for (int i = 0; i < n; i++) 

    printf("%d ", a[i]);  

  printf("\nElement to be searched is - %d", val); 

  if (res == -1) 

  printf("\nElement is not present in the array"); 

  else 

  printf("\nElement is present at %d position of array", res); 

  return 0; 

} 

 

Output:

The elements of the array are - 11 14 25 30 40 41 52 57 70

Element to be searched is - 40

Element is present at 5 position of array


d) Selection Sort

#include<stdio.h>

#include<conio.h>

void selection_sort(int[ ],int);

void main()

{

 int n,a[10],i;

            clrscr();

            printf("\n Enter size of the array");

            scanf("%d",&n);

            printf("\n Enter elements of the array");

            for(i=0;i<n;i++)

                        scanf("%d",&a[i]);

            selection_sort(a,n);

            printf("\n After sorting the elements of the array are");

            for(i=0;i<n;i++)

                        printf("%d \t",a[i]);

            getch();

}

void selection_sort(int a[],int n)

{

 int i,j,temp,min;

            for(i=0;i<n;i++)

            {

                        min=i;

                        for(j=i+1;j<n;j++)

                        {

                                    if(a[j]<a[min])

                                                min=j;

                         }

                        temp=a[i];

                        a[i]=a[min];

                        a[min]=temp;

             }

}


Output:

Enter size of the array 55

Enter elements of the array 10 8 5 3 2

After sorting the elements of the array are2 3          5          8          10       

 

 Quick Sort:

#include<stdio.h>

void quicksort(int number[25],int first,int last)

{

   int i, j, pivot, temp;

 

   if(first<last)

   {

      pivot=first;

      i=first;

      j=last;

      while(i<j)

         {

         while(number[i]<=number[pivot]&&i<last)

         {

            i++;

         }

         while(number[j]>number[pivot])

         {

            j--;

         }

         if(i<j)

        {

            temp=number[i];

            number[i]=number[j];

            number[j]=temp;

         }

      }

      temp=number[pivot];

      number[pivot]=number[j];

      number[j]=temp;

      quicksort(number,first,j-1);

      quicksort(number,j+1,last);

   }

}

 int main()

{

   int i, count, number[25];

    printf("How many elements are u going to enter?: ");

   scanf("%d",&count);

    printf("Enter %d elements: ", count);

   for(i=0;i<count;i++)

   {

     scanf("%d",&number[i]);

   }

   quicksort(number,0,count-1);

   printf("Order of Sorted elements: ");

   for(i=0;i<count;i++)

   {

      printf(" %d",number[i]);

   }

   return 0;

}

 

Output:-

How many elements are u going to enter?: 5

Enter 5 elements: 5

20

18

51

21

Order of Sorted elements:  5 18 20 21 51


 

Comments

Popular posts from this blog

ML QP

Data Structures Module_V

Data Structures Module-IV