Kamrul13981

Untitled

Jun 12th, 2021
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. //Lab 2
  2. //Array
  3. 1.
  4. #include<stdio.h>
  5. #include<conio.h>
  6. int main()
  7. {
  8.     int a[50], b[50], c[100], limitA, limitB, i, j, limitC, temp;
  9.     printf("How many elements you want to store in array A: ");
  10.     scanf("%d", &limitA);
  11.     printf("How many elements you want to store in array B: ");
  12.     scanf("%d", &limitB);
  13.     printf("Enter %d elements in array A:", limitA);
  14.     for(i=0; i<limitA; i++)
  15.         scanf("%d", &a[i]);
  16.     printf("Enter %d elements in array B:", limitB);
  17.     for(i=0; i<limitB; i++)
  18.         scanf("%d", &b[i]);
  19.    
  20.     // merging the two arrays
  21.     for(i=0; i<limitA; i++)
  22.     {
  23.         c[i] = a[i];
  24.     }
  25.     for(j=0; j<limitB; j++)
  26.     {
  27.         c[i] = b[j];
  28.         i++;
  29.     }
  30.     limitC = i;
  31.     // sorting the merged array
  32.     for(j=0; j<(limitC-1); j++)
  33.     {
  34.         for(i=0; i<(limitC-1); i++)
  35.         {
  36.             if(c[i]>c[i+1])
  37.             {
  38.                 temp = c[i];
  39.                 c[i] = c[i+1];
  40.                 c[i+1] = temp;
  41.             }
  42.         }
  43.     }
  44.     printf("\n\nElements of Array C are:\n");
  45.     for(i=0; i<limitC; i++)
  46.     {
  47.         if(i==(limitC-1))
  48.             printf("%d", c[i]);
  49.         else
  50.             printf("%d, ", c[i]);
  51.     }
  52.     getch();
  53.     return 0;
  54. }
  55. //2. Binary search:
  56. #include <stdio.h>
  57. int main()
  58. {
  59.     int i, low, high, mid, n, key, array[100];
  60.     printf("Enter number of elements:");
  61.     scanf("%d",&n);
  62.     printf("Enter %d integers:", n);
  63.     for(i = 0; i < n; i++)
  64.         scanf("%d",&array[i]);
  65.     printf("Enter value to find:");
  66.     scanf("%d", &key);
  67.     low = 0;
  68.     high = n - 1;
  69.     mid = (low+high)/2;
  70.     while (low <= high)
  71.     {
  72.         if(array[mid] < key)
  73.             low = mid + 1;
  74.         else if (array[mid] == key)
  75.         {
  76.             printf("%d found at location %d", key, mid+1);
  77.             break;
  78.         }
  79.         else
  80.             high = mid - 1;
  81.         mid = (low + high)/2;
  82.     }
  83.     if(low > high)
  84.         printf("Not found! %d isn't present in the list", key);
  85.     return 0;
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92. //Function:
  93. 1. #include <stdio.h>
  94.  
  95.   //Function declaration
  96.  float areaOfcircle(float radius_circle){
  97.    float area_circle;
  98.    area_circle = 3.14 * radius_circle * radius_circle;
  99.  
  100.    return area_circle;
  101. }
  102.  
  103.  
  104.   int main() {
  105.     float radius;
  106.  
  107.     // take radius as input
  108.     printf("Enter the radius of circle : ");
  109.     scanf("%f", &radius);
  110.  
  111.     printf("Area of circle : %.2f", areaOfcircle(radius));
  112.     printf("\n");
  113.  
  114.    return 0;
  115. }
  116.  
  117. 2.
  118. #include <stdio.h>
  119. int maxValue(int a[], int n) {
  120.   int c, index = 0;
  121.  
  122.   for (c = 1; c < n; c++)
  123.     if (a[c] > a[index])
  124.       index = c;
  125.  
  126.   return index;
  127. }
  128.  
  129. int main() {
  130.   int c, array[100], size, location, maximum;
  131.  
  132.   printf("Input number of elements in array:");
  133.   scanf("%d", &size);
  134.  
  135.   printf("Enter %d integers :\n", size);
  136.  
  137.   for (c = 0; c < size; c++)
  138.     scanf("%d", &array[c]);
  139.  
  140.  
  141.   maximum  = array[maxValue(array, size)];
  142.  
  143.   printf("Maximum element is=%d", maximum);
  144.   return 0;
  145. }
  146.  
Advertisement
Add Comment
Please, Sign In to add comment