Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to sort numbers in either descending or ascending order. The pro

ID: 3771510 • Letter: W

Question

Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int numbers[ ], int size, int ascending ) to sort the numbers. Size is the total number of numbers entered. If the argument ascending is 1, the numbers should be sorted in ascending order, if it is 0, the numbers should be sorted in descending order. The sortedNumbers function should call a swap numbers should be sorted in decending order. The sortednumbers function should call a swap.

Explanation / Answer

Here is the code for you. If you have any further queries, just revert here.

#include <stdio.h>
void swap(int num[], int n)
{
for(int i = 0; i < n/2; i++)
{
int temp = num[i];
num[i] = num[n-i-1];
num[n-i-1] = temp;
}
}
void sortNumbers(int num[], int n, int ascending)
{
for(int i = 0; i < n; i++)
for(int j = 0; j < n-i-1; j++)
if(num[j] > num[j+1])
{
int temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
}
if(ascending != 1)
swap(num, n);
}
int main()
{
int numbers[50], ascending, num, count=0;
printf("Enter the numbers. (-1 to stop): ");
scanf("%i", &num);
while(num != -1)
{
numbers[count] = num;
count++;
scanf("%i", &num);
}
printf("Which order do you want to sort. (1. Ascending. 0. Descending.): ");
scanf("%i", &ascending);
printf("The numbers before sorting is: ");
for(int j = 0; j < count; j++)
printf("%i ", numbers[j]);
printf(" ");
sortNumbers(numbers, count, ascending);
printf("The numbers after sorting is: ");
for(int j = 0; j < count; j++)
printf("%i ", numbers[j]);
printf(" ");
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote