Question

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

C programing

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 function, which will swap the contents of two array elements. Lastly, display the sorted numbers.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

output:

#include<stdio.h>
int array[100];
void main()
{
   int i=0,asc,j,item;
   printf("Enter element into array last element should be -1\n");
   while(1)
   {
       scanf("%d",&item);
       if(item<0)
       break;
       else
       {
       array[i]=item;  
       i++;  
       }
          
}
printf("given array\n");
   for(j=0;j<i;j++)
   printf("%d\t",array[j]);
      
   printf("\nenter 1 for ascending 0 for descending\n");
   scanf("%d",&asc);
  
   sortnumbers(array,i-1,asc );
   printf("sorted array\n");
   for(j=0;j<i;j++)
   printf("%d\t",array[j]);
  
}
sortnumbers( int numbers[ ], int n, int asc )
{
int p,j,temp;  
for(p=0;p<n-1;p++)             // Loop for Pass
{

for(j=0;j<=n-1;j++)
{
if(asc==1)
{
      
if(array[j]>array[j+1])
{
swap(j,j+1);

}

}
else
{
  
if(array[j]<array[j+1])
{
swap(j,j+1);

}  
}
}

}  
  
}
swap(int j,int k)
{
int temp;
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;  
  
}

output:

CAUsers MDRDocumentslsort.exe Enter element into array last element should be -1 91 56 45 23 78 giuen array enter 1 for ascen

C:Users MDRDocuments sort.exe Enter element into array last element should be -1 98 45 56 12 23 giuen array 98 enter 1 for as

Add a comment
Know the answer?
Add Answer to:
C programing Write a program to sort numbers in either descending or ascending order. The program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. the algorithims for sorting numbers should be managed in separate functions (e.g., sortAscending() and sortDescending())...

    4. the algorithims for sorting numbers should be managed in separate functions (e.g., sortAscending() and sortDescending()) Create a Kotlin console program that satisfies the following 1. The program prompts a user to enter a number that is used to create an array with the number of elements. 2. The program asks the user to enter a series of numbers, and the numbers are to be entered and stored in the array 3. The program asks the user to select the...

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • This program is in C++ Write a program that validates charge account numbers read in from...

    This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...

  • Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers

    Write a program in MIPS assembly language that implements the DESCENDING bubble sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers...

    c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers into an array of capacity 100. The program will then ask for one of three letters(A, M or S). if the user inputs something other than one of these letters, then the program should ask for that input until an A, M, or S is entered. A do-while loop should be used for this. If the user inputs an A, then the program should...

  • LANGUAGE = C i. Write a program that takes int numbers from user until user gives...

    LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

  • Write a program that will do the following. The main function should ask the user how...

    Write a program that will do the following. The main function should ask the user how many numbers it will read in. It will then create an array of that size. Next, it will call a function that will read in the numbers and fill the array (fillArray). Pass the array that was made in themain function as a parameter to this function. Use a loop that will read numbers from the keyboard and store them in the array. This...

  • Write a python program to sort numbers in descending order using insertion sort? Enter numbers: 1...

    Write a python program to sort numbers in descending order using insertion sort? Enter numbers: 1 2 3 4 4 3 2 1

  • //bubble sort implementation// Fig 6.15 with simplified convention for the for loop. #define _CRT...

    //bubble sort implementation// Fig 6.15 with simplified convention for the for loop. #define _CRT_SECURE_NO_WARNINGS //for windows os only #include <stdio.h> #include <stdlib.h> // Fig. 6.15: fig06_15.c // Sorting an array's values into ascending order. #include <stdio.h> #define SIZE 10 // function main begins program execution int main(void) {    // initialize a    int a[SIZE] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };    printf("Data items in original order");    // output original array    for (int i = 0;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT