Sorting of ten numbers
Program Plan:
- Include required header file.
- Include required “std” namespace.
- Define main function
- Declare “deque” variable to store the numbers in “double” data type.
- Declare “deque” variable to store the result in “double” data type.
- Declare a variable “values” in “double” data type.
- Display prompt statement.
- Read ten numbers from user and then store in “deque” using “push_back()” function.
- Before sorting, display the ten numbers using “for” loop.
- Then sort the ten numbers using generic “sort” function.
- Finally display the sorted numbers using “for” loop.
The below C++ program is used to sorts the ten “double” numbers in the “deque” using the generic “sort” function.
Explanation of Solution
Program:
//Header file
#include <iostream>
#include <deque>
#include <algorithm>
//Std namespace
using std::cout;
using std::cin;
using std::endl;
using std::deque;
using std::sort;
//Main function
int main()
{
/* Declare deque to store the numbers in "double" type */
deque<double> numbers;
/* Declare deque to iterator */
deque<double>::iterator result;
/* Declare "values" in "double" data type */
double values;
/* Display prompt statement */
cout << "Enter ten numbers" << endl;
/*Read ten numbers */
for(int i = 0; i < 10; i++)
{
cin>>values;
/* Store the ten numbers in deque */
numbers.push_back(values);
}
/* Display statement */
cout << "Before sorting, the ten double numbers are " << endl;
/* Display numbers before sorting */
for(result = numbers.begin(); result != numbers.end();result++)
cout << *result << endl;
/* Sort the numbers in "deque" using "sort" function */
sort(numbers.begin(), numbers.end());
/* Display statement */
cout << "After sorting, the ten double numbers are " << endl;
/* Display sorted numbers */
for(result = numbers.begin(); result != numbers.end();result++)
cout << *result << endl;
return 0;
}
Enter ten numbers
40
30.12
12
10
32.10
54.6
80
15.8
98.4
34
Before sorting, the ten double numbers are
40
30.12
12
10
32.1
54.6
80
15.8
98.4
34
After sorting, the ten double numbers are
10
12
15.8
30.12
32.1
34
40
54.6
80
98.4
Want to see more full solutions like this?
Chapter 18 Solutions
Problem Solving with C++ (10th Edition)
- Write a program, in C++, that prints the following array called. listOfNumbers = {8,4,9,12,84,56,34,97,38,90} And sort them in ascending and descending order and list them. Use functions for each sort techniques.arrow_forwardIn C programming languge: Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92}arrow_forwardBy using C++ program Write an application that solves a system of linear equations. Have the user input the coefficients of the two lines. Remember that the definition of a line is: Ax + By = C. You can use any method you wish to solve. The program should output the intersection of the two lines if they do indeed intersect, indicate if the two lines are parallel, or if they are the same line. Note: use an Array.arrow_forward
- You can shuffle a list using random.shuffle(lst). Write your own function without using random.shuffle(lst) to shuffle a list and return the list. Use the following function header:def shuffle(lst):Write a test program that prompts the user to enter a list of numbers, invokes the function to shuffle the numbers, and displays the numbers.arrow_forwardProblem Description: Write a telephone lookup program. Read a data set of 1,000 names and telephone numbers from a file that contains the numbers in random order. Handle lookups by name and also reverse lookups by phone number. Use a binary search for both lookups.arrow_forwardin c++ vs2019 You should write a program to sort an array. For this purpose you will implement two sorting algorithms: Insertion Sort and Merge Sort. For each algorithm, you are supposed to have a counter and count how many times a comparison is made to sort the array. Once this logic is implemented, create integer arrays of 1000 and 10000 elements with random values inside (you can implement a random number generator for this purpose and randomize their elements). Then go ahead and run your application. The output should show us how many comparisons it made for each algorithm for 1000 and 10000 elements. Do it seperate files. 1 cpp file 1 header file 1 main.cpp filearrow_forward
- Write a function that gets an array as a parameter, sorts it in an ascending fashion and returns the number of elements that did not change position in the sort (they were originally in the right position). Use c code int sortASC(int* myArray, int size) if array ={ 2, 3, 1, 0, -1} after calling the function the array should become {-1, 0, 1, 2 , 3 } and the function should return 1 (only one element has not changed place in the array)arrow_forwardJAVA PROGRAM Pretend Write a program that reads integers from the user and stores them in a list. Your program should continue reading values until the user enters 0. Then it should display all of the values entered by the user (except for the 0) in order from smallest to largest, with one value appearing on each line. Use either the sort method or the sorted function to sort the list.arrow_forwardWrite a C++ program : Question:Ask user to give you a list of the numbers and then Sort them, by calling two functions: Asc(Sort in Ascending order), and Desc(Sort in Descending order): Do the above question by using array. First ask user how many numbers are there in the list and then get the numbers and sort them.arrow_forward
- Write a C++ program with a linear search only: You have been hired by the SFPD fraud unit to help detect credit card fraud. They have given you a list of 20 stolen credit card numbers, and want you to write a program that can detect if someone is attempting to use one of these credit card numbers at the local Target store.Your program should first read in a list of credit card numbers from a file into an array, and ask the user for a credit card number they would like to use. Then, search the array of credit card numbers to see if the credit card number is in the list. To simplify, we will use a 6 digit credit card number, e.g. 424238. Your list of stolen credit card numbers must be stored in an array of 20 ints. You can get a list from my website and read it in to initialize your array, it is sorted from lowest to highest card number. To get the list into your hills directory, you can use the wget command from the hills prompt: wget http://fog.ccsf.edu/~mluttrel/cs110b/cards.txt…arrow_forwardin c++. Write a function that returns the sum of all the elements in a specified column in a matrix using the following header: const int SIZE = 4; double sumColumn(double m[][SIZE], int rowSize, int columnIndex); Write a test program that reads a 3-by-4 matrix and displays the sum of each column. please leave comments on each step.arrow_forwardUsing JavaScriptDefine a function getMonth which accepts number from 1 to 12 as an argument and return the descriptive name of the month. For example: getMonth(1) should return January while getMonth(12) returns December, finally getMonth(-1) returns null. Use array or object to define a list of names for the month and refrain from using if statement to check the argument if it's 1, 2, etc.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education