Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /* This program asks the user to enter up to 20 numbers. It then displays the numbers entered, removes duplicates
- and then list the numbers with the duplicates removed in ascending order.
- */
- int main (void)
- { //Variables
- setbuf(stdout, NULL);
- int nums[20] , i , j, k, swap ;
- int count=0;
- // ===================================================
- // Part one. Enter the numbers
- printf("Enter integers. (Negative -1 to stop):\n");
- for (i=0; i < 20; i++)
- {
- scanf("%d", &nums[i]);
- count = count +1;
- if(nums[i] == -1 ) // If user enters -1 stops the program from expecting anymore number inputs
- break;
- }
- // ===========================================================
- //Part 2. Print the numbers entered
- printf("The numbers you entered are:\n"); // outputs the numbers you entered one number per line
- for(i=0;i<count;++i)
- {
- printf("%d\n", nums[i]);
- }
- // ============================================================
- // Part 3. Display numbers with duplicates removed
- printf("\n Your numbers with the duplicate numbers removed:\n ");
- // for loop for removing the duplicate numbers that the user enters.
- for(i=0;i<count;i++)
- {
- for(j=i+1;j<count;)
- {
- if(nums[j]==nums[i])
- {
- for(k=j;k<count-1;++k)
- {
- nums[k]=nums[k+1];
- }
- count--;
- }
- else
- {
- j++;
- }
- }
- }
- for(i=0;i<count;i++) // outputs the numbers you entered with the duplicates removed one number per line
- printf("%d\n ",nums[i]);
- //================================================================
- // Part 3. Bubble sort for listing numbers in ascending order
- // start of the bubble sort for listing the numbers in ascending order. Can replace ">" with "<" to list in descending order
- for(i=0; i<count -1; i++)
- {
- for(j=0; j < count -1 - i; j++)
- {
- if (nums[j] > nums[j+1])
- {
- swap = nums[j];
- nums[j] =nums[j+1];
- nums[j+1] = swap;
- }
- }
- }
- printf("\n Your numbers sorted in to ascending order with the duplicates removed:\n");
- for(j=0;j<count;j++) // outputs the numbers in ascending order. One number per line
- printf("%d\n ",nums[j]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement