zero_shubham1

practical_question_paper

Jul 4th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     int opt=0,arr[50],i,n,s_id,temp,j;
  7.  
  8.     printf("Enter the number of numbers you want to enter: ");
  9.     scanf("%d",&n);
  10.  
  11.     printf("Enter the numbers: ");
  12.     for(i=0;i<n;i++)
  13.     {
  14.         scanf("%d",&arr[i]);
  15.  
  16.     }
  17.  
  18.     while(1)
  19.     {
  20.         printf("Enter 1 to print the list of numbers, 2 to delete the duplicates from the list or 3 to exit the program: ");
  21.         scanf("%d",&opt);
  22.  
  23.         switch(opt)
  24.         {
  25.         case 1:
  26.             for(i=0;i<n;i++)
  27.                 printf("%d \t",arr[i]);
  28.             break;
  29.         case 2:
  30.             for(i=0;i<n-1;i++)
  31.             {
  32.                 s_id = i;
  33.                 for(j=i+1;j<n;j++)
  34.                 {
  35.                     if(arr[s_id]>arr[j])
  36.                         s_id = j;
  37.                 }
  38.                 temp = arr[i];
  39.                 arr[i] = arr[s_id];
  40.                 arr[s_id] = temp;
  41.  
  42.             }
  43.             printf("\nSorted.");
  44.             //Code probably gets into an infinite loop here! No idea why!
  45.             for(i=0;i<n;i++)
  46.             {
  47.                 while(arr[i]==arr[i+1])
  48.                 {
  49.                     for(j=i+1;j<n;j++)
  50.                         arr[j] = arr[j+1];
  51.                 }
  52.             }
  53.             printf("\nDeleted Duplicates.");
  54.         case 3:
  55.             exit(0);
  56.  
  57.         default:
  58.             printf("Entered wrong option. Please try again!");
  59.         }
  60.  
  61.     }
  62.  
  63.  
  64.     return 0;
  65. }
Add Comment
Please, Sign In to add comment