bipo143

lab full code

Feb 18th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.13 KB | None | 0 0
  1. void display_Array(int b[],int k);
  2. void edit (int b[],int pp, int vv);
  3. int Insertion_lastpositon (int b[],int nn, int vv);
  4. int Insertion_specification (int b[],int nn, int vv,int pp);
  5. int Deletion (int b[], int nn, int pp);
  6. void linear_search(int b[],int nn, int vv);
  7. void Bubble_short (int b[], int nn);
  8. void Binary_search (int b[],int nn,int vv);
  9. int main ()
  10.  
  11. {
  12.     int a[100],n,i,c,b=1,v,p;
  13.     printf("\n Enter how many values\n");
  14.     scanf("%d",&n);
  15.     printf("\n Enter Values\n");
  16.     for(i=1;i<=n;i++)
  17.     scanf("%d",&a[i]);
  18.  
  19.     while (b)
  20.     {
  21.         printf("\n...........menu.......\n");
  22.         printf("\n pres 0 for Quit\n");
  23.         printf("\n pres 1 for display\n");
  24.         printf("\n pres 2 for insertion at last position\n");
  25.         printf("\n pres 3 for insertion at specific position\n");
  26.         printf("\n pres 4 for delete from specific position\n");
  27.         printf("\n pres 5 for linear search\n");
  28.         printf("\n pres 6 for Bubble sort\n");
  29.         printf("\n pres 7 for Binary search\n");
  30.         printf("\n pres 8 for Edit\n");
  31.         printf ("\n Enter your choice\n");
  32.         scanf("%d", &c);
  33.  
  34.         switch(c)
  35.         {
  36.             case 0:b=0;
  37.             break;
  38.  
  39.             case 1:printf("\n choice=display\n");
  40.             if(n!=0)
  41.             display_Array(a,n);
  42.             else
  43.             printf("\n No data to display\n");
  44.             break;
  45.  
  46.  
  47.             default:printf("\n Wrong choice\n");
  48.             break;
  49.  
  50.             case 2: printf ("\n choice = Insertion at last position \n");
  51.             printf("Enter new value \n");
  52.             scanf ("%d",&v);
  53.             n=Insertion_lastpositon(a,n,v);
  54.             break;
  55.             case 3: printf("\n choice = insertion att specification\n");
  56.             if(n!=0)
  57.  
  58.             {
  59.  
  60.             K:printf("\n Enter position between %d to %d \n \n",1,n+1);
  61.             scanf("%d",&p);
  62.             if(p>=1 && p<=n+1)
  63.             {
  64.                 printf("\Enter new value \n");
  65.                 scanf("%d",&v);
  66.                 n=Insertion_specification(a,n,v,p);
  67.                 printf("\n\n inserted at position %d\n",p);
  68.             }
  69.             else
  70.             {
  71.                 printf("\n wrong position \n");
  72.                 goto K;
  73.             }
  74.             }
  75.             else
  76.             printf("\n the array is empty \n");
  77.             break;
  78.  
  79.             case 4: printf("\n Choice = Delete from Array \n");
  80.             if (n!=0)
  81.             {
  82.                 Z: printf("\n Enter Position between %d to %d\n",1,n);
  83.                 scanf("%d",&p);
  84.                 if (p>=1 && p<=n)
  85.                 {
  86.                 printf("\n Delected value = %d\n", a[p]);
  87.                 n=Deletion (a,n,p);
  88.                 printf ("\n Delete Sucessfully from position %d\n",p);
  89.                 }
  90.                 else
  91.                 {
  92.                     printf("\n Wrong position \n");
  93.                     goto Z;
  94.                 }
  95.             }
  96.             else
  97.             printf("\n No data to delete \n");
  98.             break;
  99.  
  100.             case 5: printf("\n choice =linear_search \n");
  101.             if(n!=0)
  102.             {
  103.                 printf("\n Enter Value for searching \n");
  104.                 scanf ("%d",&v);
  105.                 linear_search (a,n,v);
  106.             }
  107.             else
  108.             printf("\n No Data to search \n");
  109.             break;
  110.  
  111.             case 6: printf("\n choice =Bubble_short \n");
  112.             if(n!=0)
  113.             Bubble_short(a,n);
  114.             else
  115.             printf("\n The Array is Empty \n");
  116.             break;
  117.  
  118.             case 7: printf("\n choice = Binary_search \n");
  119.             if (n!=0)
  120.             {
  121.                 printf("\n Enter value for searching \n");
  122.                 scanf ("%d", &v);
  123.                 Binary_search(a,n,v);
  124.             }
  125.             else
  126.             printf("\n The Array is Empty \n");
  127.             break;
  128.  
  129.  
  130.  
  131.             case 8:
  132.             printf("\n choice=edit\n");
  133.             if(n!=0)
  134.             {
  135.                 printf("\n Enter new value\n");
  136.                 scanf ("%d",&v);
  137.  
  138.  
  139.             M: printf ("enter position between %d to %d\n ",1,n);
  140.                 scanf("%d",&p);
  141.                 if(p>=1&& p<=n)
  142.                 edit (a,p,v);
  143.  
  144.                 else {
  145.                 printf ("\n wrong position\n");
  146.                 goto M;
  147.                      }
  148.  
  149.                      }
  150.  
  151.              else
  152.                 printf("No data to edit\n");
  153.                 break;
  154.  
  155.         }
  156.  
  157.     }
  158. }
  159. void display_Array(int b[],int k)
  160. {
  161.     int j;
  162.     printf("\n the values are:\n");
  163.     for(j=1;j<=k;j++)
  164.     printf("%d\n", b[j]);
  165.  
  166. }
  167.  
  168. void edit (int b[], int pp ,int vv)
  169. {
  170.     b[pp]=vv;
  171.     printf("\n edited successfully\n");
  172. }
  173. int Insertion_lastpositon (int b[],int nn, int vv)
  174. {
  175.     b[nn+1]=vv;
  176.     printf("\n Inserted Sucessfully \n\n");
  177.     return nn+1;
  178. }
  179. int Insertion_specification (int b[],int nn, int vv,int pp)
  180. {
  181.     int j;
  182.     for (j=nn;j>=pp;j--)
  183.     b[j+1]=b[j];
  184.     b[pp]=vv;
  185.     return nn+1;
  186. }
  187. int Deletion (int b[], int nn, int pp)
  188. {
  189.     int j;
  190.     for (j=pp; j<nn;j++)
  191.     b[j]=b[j+1];
  192.     return nn-1;
  193. }
  194.  
  195. void linear_search(int b[],int nn, int vv)
  196. {
  197.     int j,c=0;
  198.     for(j=1;j<=nn;j++)
  199.     {
  200.         if (b[j]==vv)
  201.         {
  202.             printf("\n Found at position =%d \n",j);
  203.             c=c+1;
  204.         }
  205.     }
  206.     if (c==0)
  207.     printf("\n Not Found \n");
  208.     else
  209.     printf("Total Found =%d \n",c);
  210. }
  211. void Bubble_short (int b[], int nn)
  212. {
  213.     int i, j, t;
  214.     for(i=1;i<=nn;i++)
  215.     {
  216.         for(j=1;j<=nn-i;j++)
  217.         {
  218.             if(b[j]>b[j+1])
  219.             {
  220.                 t=b[j];
  221.                 b[j]=b[j+1];
  222.                 b[j+1]=t;
  223.             }
  224.         }
  225.     }
  226. }
  227.  
  228. void Binary_search (int b[],int nn,int vv)
  229. {
  230.     int beg ,end,mid;
  231.     Bubble_short(b,nn);
  232.     beg=1;
  233.     end=nn;
  234.     do
  235.     {
  236.         mid=(beg+end)/2;
  237.         if(vv<b[mid])
  238.         end = mid-1;
  239.         else if (vv>b[mid])
  240.         beg = mid+1;
  241.     }
  242.     while (vv!= b[mid] && beg <=end);
  243.     if (vv==b[mid])
  244.     printf("\n Found \n");
  245.     else
  246.     printf("\n Not Found \n");
  247.  
  248. }
Add Comment
Please, Sign In to add comment