DATA STRUCTURE -ASSIGNMENT NO.1:

 PRACTICE PROGRAM:

1) Write a menu driven C program to perform the following operation on an integer array:

a) Display the sum of elements at even subscript position of array

b) Display the sum of elements at odd subscript position of array

ANS:

#include <stdio.h>

#include <conio.h>

int main()

{

    int a[10], i, n;

    int choice, sum = 0;

    printf("\n enter array limit:");

    scanf("%d", &n);

    printf("\n enter array elements:");

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

    {

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

    }

    printf("\n 1.sum of all even index element of an array.\n");

    printf("\n 2.sum of all odd index element of an array.\n");

    printf("\n.................................................!");

    printf("\n enter your choice:");

    scanf("%d", &choice);


    switch (choice)

    {

    case 1:

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

        {

            if (i % 2 == 0)

            {

                sum = sum + a[i];

            }

        }

        printf("\n sum of all even index element is %d ", sum);

        sum = 0;

        break;

    case 2:

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

        {

            if (i % 2 != 0)

            {

                sum = sum + a[i];

            }

        }

        printf("\n sum of all odd index element is %d", sum);

        sum = 0;

        break;


    default:

        printf("\n wrong input....!");

        break;

    }

}

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

2) Write a C Program to find the largest pair sum in an unsorted array.(hint: find 2 maximum 

elements from array and then find the sum of both numbers.)

ANS:

#include <stdio.h>

#include <conio.h>

int main()

{

    int a[10], i, n;

    int max,smax,sum=0;

    printf("\n enter array limit:");

    scanf("%d", &n);

    printf("\n enter array elements:");

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

    {

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

    }

    max = a[0];

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

    {

        if (max<a[i])

        {

            max = a[i];

        }

    }

    printf("\n the largest element is %d",max);

   smax = a[0];

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

   {

      if (a[i]!=max)

      {

          if (smax<a[i])

        {

            smax = a[i];

        }

      }

      

   }

   printf("\n second largest element is %d",smax);


   sum = max+smax;

   printf("\n sum of largest element is %d",sum);

   

     

}

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

3) Write a C Program to calculate Median of two sorted arrays of different sizes.

ANS:

#include <stdio.h>

#include <conio.h>

void display(int a[], int n)

{

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

    {

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

    }

}

void bubble_sort(int a[], int n)

{

    int pass, temp, i;

    for (pass = 1; pass < n - 1; pass++)

    {

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

        {

            if (a[i] > a[i + 1])

            {

                temp = a[i];

                a[i] = a[i + 1];

                a[i + 1] = temp;

            }

        }

    }

}

void combine(int a[], int b[], int c[], int n1, int n2)

{


    int n3 = n1 + n2;

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

    {

        c[i] = a[i];

        c[i + 1] = b[i];

    }

}


int main()

{

    int a[20], b[20], c[40];

    int i, j, k;

    int n1, n2, n3;

    printf("\n Enter the limit for first array:");

    scanf("%d", &n1);

    printf("\n Enter the limit for second array:");

    scanf("%d", &n2);

    printf("\n Enter The first array elements:");

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

    {

        printf("\n Enter [%d] index element:", i);

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

    }

    printf("\n Enter The first array elements:");

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

    {

        printf("\n Enter [%d] index element:", i);

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

    }

    printf("\n The first array is:");

    display(a, n1);

    printf("\n The second array is:");

    display(b, n2);

    printf("\n the first sorted array is:");

    bubble_sort(a, n1);

    display(a, n1);

    printf("\n the second sorted array is:");

    bubble_sort(b, n2);

    display(b, n2);

    combine(a, b, c, n1, n2);

    bubble_sort(c, n1 + n2);

    printf("\n The third array is:");

    display(c, n1 + n2);

    int size = n1 + n2 / 2;

    printf("\n The median of two sorted array is:%d", c[size]);

}

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

SET A:

1) Write a C Program to Count number of occurrences (or frequency) in a given sorted array

 Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2

 Output: 4 // x (or 2) occurs 4 times in arr[].

ANS:

#include<stdio.h>

#include<conio.h>

int main(){

    int a[10],i,n;

    int o,count=0;

    printf("\n enter a limit:");

    scanf("%d",&n);

    printf("\n enter sorted array elements:");

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

    {

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

    }

    printf("\n enter number for finding occurances:");

    scanf("%d",&o);

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

    {

        if (a[i]==o)

        {

            count++;

        }

        

    }

    printf("\n the number %d is counted up to %d times in given array.",o,count);    

}

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

2) Write a C program to accept n elements, store those elements in array and store the square 

of these numbers in another array and display both the array.

ANS:

#include<stdio.h>

#include<conio.h>

int main(){

    int a[10],i,n;

    int b[10];

    printf("\n enter array limit:");

    scanf("%d",&n);

    printf("\n enter array elements:");

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

    {

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

    }


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

    {

        b[i] = a[i]*a[i];

    }

    

  printf("\n the user array:");

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

  {

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

  }

  printf("\n the square of previous array:");

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

  {

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

  }    

}

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

3) Write a C program to Copy one array into another array.

ANS;

#include<stdio.h>

#include<conio.h>

int main(){

    int a[10],i,n;

    int b[10];

    printf("\n enter array limit:");

    scanf("%d",&n);

    printf("\n enter array elements:");

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

    {

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

    }


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

    {

        b[i]=a[i];

    }


    printf("\n the previous array:");

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

    {

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

    }

    printf("\n the copied array:");

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

    {

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

    }  

}

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

SET B:

1) Write a C program accept the polynomial and display it in format e.g. 6x^4+2x^2+5x^1+3.

ANS:

#include<stdio.h>

#include<conio.h>

int main(){

    int a[10],i,n;

    int p,c;

    

    printf("\n enter a limit:");

    scanf("%d",&n);

    printf("\n enter first polynomial.");

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

    {

        printf("\n enter power&coefficient:");

        scanf("%d %d",&p,&c);

        a[p]=c;

    }


    printf("\n polynomial is:");

    for ( i = n-1; i >=0; i--)

    {

        if (i==0)

        {

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

        }else{

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

        }    

    }

    getch();

    return 0;

}

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

2) Write a ‘C’ program to accept n elements store those elements in array and find and replace 

a given number. 

ANS:

#include <stdio.h>

#include <conio.h>

void display(int a[], int n)

{

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

    {

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

    }

}

int binary_search(int a[],int first,int last,int key){

    int mid;

    while (first<=last)

    {

        mid=first+last/2;

        if (a[mid]==key)

        {

            return mid;

        }

        if (a[mid]<key)

        {

            first=mid+1;

        }

        if (a[mid]>key)

        {

            last=mid-1;

        }  

    }  

}

void bubble_sort(int a[], int i, int n)

{

    int temp, pass;

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

    {

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

        {

            if (a[i] > a[i + 1])

            {

                temp = a[i];

                a[i] = a[i + 1];

                a[i + 1] = temp;

            }

        }

    }

}

int main()

{

    int a[20], n, i, key,mid,user_value;

    printf("\n Enter how many elements you want:");

    scanf("%d", &n);

    printf("\n Enter array elements:");

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

    {

        printf("\n Enter index %d element:", i);

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

    }

    printf("\n The unsorted array is:");

    display(a, n);

    // printf("\n The sorted array is:");

    bubble_sort(a, 0, n);

    printf("\n The sorted array is:");

    display(a, n);

    printf("\n Enter which element you want to find?:");

    scanf("%d",&key);

    mid=binary_search(a,0,n-1,key);

    printf("\n The %d is found at index no.%d",key,mid);

    printf("\n Enter value to replace search value:");

    scanf("%d",&user_value);

    a[mid]=user_value;

    printf("\n After interchanging array is: ");

    display(a,n);

    printf("\n After interchange sorted array:");

    bubble_sort(a, 0, n);

    display(a,n); 

}

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

3) Write a ‘C’ program to accept two polynomials and find the addition of accepted 

polynomials.

ANS:

#include<stdio.h>

#include<conio.h>

int main(){

    int a[10],b[10],c[10];

    int i,n,p,coe;

    printf("\n Enter the limit for polynomials: ");

    scanf("%d",&n);

    printf("\n Enter the coefficient & power for first polynomial under (power under the array limit): ");

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

    {

        printf("\n Enter the coefficient&power:");

        scanf("%d%d",&coe,&p);

        a[p]=coe;

    }

    printf("\n Enter the coefficient & power for second polynomial(power under the array limit):");

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

    {

         printf("\n Enter the coefficient&power:");

        scanf("%d%d",&coe,&p);

        b[p]=coe;

    }


    printf("\n The addition of two polynomial:");

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

    {

        c[i]=a[i]+b[i];

    }

    for ( i = n-1; i >=0; i--)

    {

        if (i==0)

        {

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

        }else{

            printf("%dx^%d+",c[i],i);

        }    

    }  

}

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

SET C:

1) Write a ‘C’ program to accept two polynomials and find the Multiplication of accepted 

polynomials.

ANS:

#include <stdio.h>

#include <conio.h>

int main()

{

    int a[10], b[10], n1;

    int k[20], p, c;

    int i;

    printf("\n Enter  polynomial's limit:");

    scanf("%d", &n1);

    printf("\n Enter first polynomial elements:");

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

    {

        printf("\nEnter coefficient and power under [%d]:", n1);

        scanf("%d%d", &c, &p);

        a[p] = c;

    }

    printf("\n Enter second polynomial elements:");

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

    {

        printf("\nEnter coefficient and power under [%d]:", n1);

        scanf("%d%d", &c, &p);

        b[p] = c;

    }


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

    {

        k[i] = a[i] * b[i];

    }


    printf("\n The multiplication of two polynomial :");

    for (i = n1 - 1; i >= 0; i--)

    {

        if (i == 0)

        {

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

        }

        else

        {

            printf("%dx^%d+", k[i], i);

        }

    }

}

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

Comments

Popular posts from this blog

PHP ALL ASSIGNMENT PDF

DATA STRUCTURE ALL PDF(LAB ASSIGNMENTS)

DATA STRUCTURE :ASSIGNMENT NO.8:TREE