Advertisement
SMASIF

DS LAB1 - Array

Feb 25th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int n,i,j, max,x=0,ch,temp,min,pos,p,v, temp1, temp2;
  5.     printf("Enter array size: ");
  6.     scanf("%d", &n);
  7.     int a[n];
  8.     for(i=0;i<n;i++){
  9.         printf("Enter Element[%d]: ", x);
  10.         scanf("%d", &a[i]);
  11.         x++;
  12.     }
  13.     while(1){
  14.         printf("\nPress 1 for Largest\n");
  15.         printf("Press 2 for Sorting\n");
  16.         printf("Press 3 for Insert\n");
  17.         printf("Press 4 for Delete\n");
  18.         printf("Press 5 for Exit\n");
  19.         scanf("%d", &ch);
  20.         if(ch==1){
  21.             max=a[0];
  22.             for(i=1;i<n;i++)
  23.             {
  24.                 if(a[i]>max)
  25.                     max=a[i];
  26.             }
  27.             printf("The largest number is: %d", max);
  28.         }
  29.         else if(ch==2){
  30.                 min=j;
  31.             for(j=0;j<n-1;j++)
  32.             {
  33.                 for(i=j+1;i<n;i++)
  34.                 {
  35.                     if(a[j]>a[i])
  36.                     {
  37.                         temp=a[j];
  38.                         a[j]=a[i];
  39.                         a[i]=temp;
  40.                     }
  41.                 }
  42.             }
  43.             for(i=0;i<n;i++)
  44.                 printf("%d ", a[i]);
  45.         }
  46.  
  47.         else if(ch==3){
  48.             printf("Enter Position: ");
  49.             scanf("%d", &p);
  50.             printf("Enter Value: ");
  51.             scanf("%d", &v);
  52.             if(p<0 || p>n)
  53.                 printf("Invalid\n");
  54.             else
  55.             {
  56.                 for(i=0; i<p; i++)
  57.                 {
  58.                     temp1=a[i];
  59.                     temp2=a[i+1];
  60.                 }
  61.                 a[p-1]=v;
  62.                 for(i=p-1; i<n; i++)
  63.                 {
  64.                     a[i+1]=temp1;
  65.                     temp1=temp2;
  66.                     temp2=a[i+2];
  67.                 }
  68.                 n++;
  69.             }
  70.             for(i=0;i<n;i++)
  71.                 printf("%d ", a[i]);
  72.         }
  73.  
  74.         else if(ch==4)
  75.         {
  76.             printf("Enter the position: ");
  77.             scanf("%d", &pos);
  78.             for(i=pos-1;i<n-1;i++)
  79.                 a[i]=a[i+1];
  80.                 n--;
  81.             for(i=0;i<n;i++)
  82.                 printf("%d ", a[i]);
  83.         }
  84.         else if(ch==5)
  85.         {
  86.                 printf("Thank You.\n");
  87.                 break;
  88.         }
  89.         else
  90.             printf("Invalid Option\n");
  91.     }
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement