Advertisement
Guest User

Untitled

a guest
Feb 26th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4. /* This program asks the user to enter up to 20 numbers. It then displays the numbers entered, removes duplicates
  5. and then list the numbers with the duplicates removed in ascending order.
  6. */
  7. int main (void)
  8.  
  9. {
  10. setbuf(stdout, NULL);
  11.  
  12. int nums[20] , i , j, k, swap ;
  13. int count=0;
  14.  
  15. {
  16.  
  17. printf("Enter integers. (Negative -1 to stop):\n");
  18. for (i=0; i < 20; i++)
  19. {
  20.  
  21.  
  22. scanf("%d", &nums[i]);
  23. count = count +1;
  24.  
  25. if(nums[i] == -1 ) // If user enters -1 stops the program from expecting anymore number inputs
  26.  
  27. break;
  28. }
  29.  
  30.  
  31.  
  32. }
  33.  
  34. printf("The numbers you entered are:\n"); // outputs the numbers you entered one number per line
  35. for(i=0;i<count;++i)
  36. {
  37. printf("%d\n", nums[i]);
  38. }
  39. printf("\n Your numbers with the duplicate numbers removed:\n ");
  40. // for loop for removing the duplicate numbers that the user enters.
  41. for(i=0;i<count;i++)
  42. {
  43. for(j=i+1;j<count;)
  44. {
  45. if(nums[j]==nums[i])
  46. {
  47. for(k=j;k<count-1;++k)
  48. {
  49. nums[k]=nums[k+1];
  50. }
  51. count--;
  52. }
  53. else
  54. {
  55. j++;
  56. }
  57. }
  58. }
  59.  
  60. for(i=0;i<count;i++) // outputs the numbers you entered with the duplicates removed one number per line
  61. printf("%d\n ",nums[i]);
  62.  
  63.  
  64.  
  65. // start of the bubble sort for listing the numbers in ascending order. Can replace ">" with "<" to list in descending order
  66. for(i=0; i<count -1; i++)
  67. {
  68. for(j=0; j < count -1 - i; j++)
  69. {
  70. if (nums[j] > nums[j+1])
  71. {
  72. swap = nums[j];
  73. nums[j] =nums[j+1];
  74. nums[j+1] = swap;
  75. }
  76. }
  77. }
  78. printf("\n Your numbers sorted in to ascending order with the duplicates removed:\n");
  79.  
  80. for(j=0;j<count;j++) // outputs the numbers in ascending order. One number per line
  81. printf("%d\n ",nums[j]);
  82.  
  83.  
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement