Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lab 2
- //Array
- 1.
- #include<stdio.h>
- #include<conio.h>
- int main()
- {
- int a[50], b[50], c[100], limitA, limitB, i, j, limitC, temp;
- printf("How many elements you want to store in array A: ");
- scanf("%d", &limitA);
- printf("How many elements you want to store in array B: ");
- scanf("%d", &limitB);
- printf("Enter %d elements in array A:", limitA);
- for(i=0; i<limitA; i++)
- scanf("%d", &a[i]);
- printf("Enter %d elements in array B:", limitB);
- for(i=0; i<limitB; i++)
- scanf("%d", &b[i]);
- // merging the two arrays
- for(i=0; i<limitA; i++)
- {
- c[i] = a[i];
- }
- for(j=0; j<limitB; j++)
- {
- c[i] = b[j];
- i++;
- }
- limitC = i;
- // sorting the merged array
- for(j=0; j<(limitC-1); j++)
- {
- for(i=0; i<(limitC-1); i++)
- {
- if(c[i]>c[i+1])
- {
- temp = c[i];
- c[i] = c[i+1];
- c[i+1] = temp;
- }
- }
- }
- printf("\n\nElements of Array C are:\n");
- for(i=0; i<limitC; i++)
- {
- if(i==(limitC-1))
- printf("%d", c[i]);
- else
- printf("%d, ", c[i]);
- }
- getch();
- return 0;
- }
- //2. Binary search:
- #include <stdio.h>
- int main()
- {
- int i, low, high, mid, n, key, array[100];
- printf("Enter number of elements:");
- scanf("%d",&n);
- printf("Enter %d integers:", n);
- for(i = 0; i < n; i++)
- scanf("%d",&array[i]);
- printf("Enter value to find:");
- scanf("%d", &key);
- low = 0;
- high = n - 1;
- mid = (low+high)/2;
- while (low <= high)
- {
- if(array[mid] < key)
- low = mid + 1;
- else if (array[mid] == key)
- {
- printf("%d found at location %d", key, mid+1);
- break;
- }
- else
- high = mid - 1;
- mid = (low + high)/2;
- }
- if(low > high)
- printf("Not found! %d isn't present in the list", key);
- return 0;
- }
- //Function:
- 1. #include <stdio.h>
- //Function declaration
- float areaOfcircle(float radius_circle){
- float area_circle;
- area_circle = 3.14 * radius_circle * radius_circle;
- return area_circle;
- }
- int main() {
- float radius;
- // take radius as input
- printf("Enter the radius of circle : ");
- scanf("%f", &radius);
- printf("Area of circle : %.2f", areaOfcircle(radius));
- printf("\n");
- return 0;
- }
- 2.
- #include <stdio.h>
- int maxValue(int a[], int n) {
- int c, index = 0;
- for (c = 1; c < n; c++)
- if (a[c] > a[index])
- index = c;
- return index;
- }
- int main() {
- int c, array[100], size, location, maximum;
- printf("Input number of elements in array:");
- scanf("%d", &size);
- printf("Enter %d integers :\n", size);
- for (c = 0; c < size; c++)
- scanf("%d", &array[c]);
- maximum = array[maxValue(array, size)];
- printf("Maximum element is=%d", maximum);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment