Advertisement
Jaagdish47

Array CODE

Nov 23rd, 2024 (edited)
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.64 KB | Source Code | 0 0
  1. Array Codes
  2. 1A. Write a program to store the elements in 1-D array and perform the
  3. operations like searching, sorting and reversing the elements. [Menu Driven]
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. void bubble_sort(int b[], int n);
  7. void reverse(int b[], int n);
  8. void search(int b[], int n);
  9. void main(){
  10.     int a[7] = {10, 45, 51, 20, 75, 30, 35};
  11.     int i, ch = 1, n;    
  12.     n = 7;
  13.     printf("\n A : ");
  14.     for (i = 0; i < n; i++)
  15.         printf("\t %d", a[i]);
  16.     while (ch != 4) {
  17.         printf("\n 1.Sort \n 2.Reverse \n 3.Searching \n 4.Exit \n Select the option : ");
  18.         scanf("%d", &ch);
  19.         switch (ch) {
  20.             case 1:
  21.                 bubble_sort(a, n);
  22.                 break;
  23.             case 2:
  24.                 reverse(a, n);
  25.                 break;
  26.             case 3:
  27.                 search(a, n);
  28.                 break;
  29.             case 4:
  30.                 exit(0);
  31.         }
  32.     }
  33. }
  34. void bubble_sort(int b[], int n) {
  35.     int p, i, temp;
  36.     for (p = 0; p < n - 1; p++) {
  37.         for (i = 0; i < n - 1; i++) {
  38.             if (b[i] > b[i + 1]) {
  39.                 temp = b[i];
  40.                 b[i] = b[i + 1];
  41.                 b[i + 1] = temp;
  42.             }
  43.         }
  44.     }
  45.     printf("\n Array : ");
  46.     for (i = 0; i < n; i++)
  47.         printf("\n %d", b[i]);
  48.  
  49.     printf("\n \n Sorted Array : ");
  50.     for (i = 0; i < n; i++) {
  51.         printf("\t %d", b[i]);
  52.     }
  53. }
  54. void reverse(int b[], int n) {
  55.     int i;
  56.     printf("\n Array : ");
  57.     for (i = 0; i < n; i++)
  58.         printf("\t %d", b[i]);
  59.  
  60.     printf("\n \n Reversed Array : ");
  61.     for (i = n - 1; i >= 0; i--)
  62.         printf("\t %d", b[i]);
  63. }
  64. void search(int b[], int n) {
  65.     int data, i, flag = 0;
  66.     printf("\n Array : ");
  67.     for (i = 0; i < n; i++)
  68.         printf("\t %d", b[i]);
  69.     printf("\nEnter element to search: ");
  70.     scanf("%d", &data);
  71.     for (i = 0; i < n; i++) {
  72.         if (b[i] == data) {
  73.             printf("\n The element is present at position %d", i + 1);
  74.             flag = 1;
  75.             break;
  76.         }
  77.     }
  78.     if (flag == 0) {
  79.         printf("\n The element is not present in the array!!");
  80.     }
  81. }
  82.  
  83. 1B. Read the two arrays from the user and merge them and display the
  84. elements in sorted order. [Menu Driven]
  85. #include<stdio.h>
  86. #include<conio.h>
  87.     void main(){
  88.         int n1, n2, n3, i, j; //array size declaration!
  89.         int a[10], b[10], c[20];
  90.         clrscr();
  91.  
  92.     printf("Enter size of the first array! : \n");
  93.     scanf("%d",&n1);
  94.  
  95.         printf("\nEnter the elements of array : ");
  96.         for(i=0;i<n1;i++){
  97.             scanf("%d",&a[i]);
  98.         }
  99.         printf("\n Enter the size of second array : ");
  100.             scanf("%d", &n2);
  101.  
  102.                 printf("\n Enter the elements in second array! : ");
  103.                     for(i=0; i<n2; i++){
  104.                         scanf("%d", &b[i]);
  105.                     }
  106.                     n3=n1+n2;
  107.                 for(i=0; i<n1; i++){
  108.                     c[i]=a[i];
  109.                 }
  110.                 for(i=0; i<n2; i++){
  111.                     c[i]=a[i];
  112.                 }
  113.             for(i=0; i<n2; i++){
  114.                 c[i+n1]=b[i];
  115.             }
  116.             printf("\n Final arrray after sorting : ");
  117.             for(i=0; i<n3; i++){
  118.                 int temp;
  119.                 for(j=i+1; j<n3; j++){
  120.                     if(c[i]>c[j]){
  121.                         temp=c[i];
  122.                         c[i]=c[j];
  123.                         c[j]=temp;
  124.                     }
  125.                 }
  126.             }
  127.             for(i=0; i<n3; i++){
  128.                 printf("%d ",c[i]);
  129.                 getch ();
  130.             }
  131.     }
  132.  
  133.  
Tags: Array CODE
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement