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)
- Problem Attachedarrow_forwardIn C++ Sort an array from largest to smallest (you may use one of the sorts we did, you just have to change it so that it goes from largest to smallest instead).arrow_forwardYou 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_forward
- please use pythonarrow_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_forwardComplete a C++ program that reads from a file named fruits.txt which contains a list of fruits. First, the fruits should be read into an array. Then, your program should ask the user for a specific fruit to search. If the word is in the list of words, the program should return “Fruit located at index: X” where the X is the index of the array where the fruit is located. If the fruit is not in the array, your program should return “I didn’t find the fruit!”. This message should only print once at the very end if the fruit is not found. If there is an error opening the file, your program should print an error message. If the fruit you are looking for is in the array in multiple locations, your program only needs to print one of the indexes. The following presents a sample words.txt and execution result. fruits.txt 6 apple carrot banana date grape fig in c++ programingarrow_forward
- Give me answer fast please.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_forwardIn C++arrow_forward
- In c++, ask the user to enter numbers to be sorted. Add them to the vector. The user will signal the end of input by giving you a negative number.Then, sort the vector and print it.arrow_forwardIn Python use the Design Recipe to write a function count_evens_NxN, that consumes a nested list representing a matrix of size NxN. The function should return the number of even numbers in the matrix. For this function, 0 is considered an even number. Include a Docstring! Note: You may assume the list argument passed to the function is a nested list of integers Write 3 assert_equal statements to test your functionarrow_forwardThis is for a C++ class Part 2a: Find the Median Write a program that prompts the user to enter a number of estimated home values (integers). First, ask the user how many estimates will be entered and then prompt for and store those values into an array. Your program should then compute the median estimated home value. Note that you will need to first sort the estimates in ascending (or descending) order before you can compute the median. Display the median value. Your program should use pointers to the array. Hint: you may consider writing a function that accepts the array of integers in sorted order and an integer representing the array size and return the median value. Part 2b: Password Verification A secure password is critically important in today's world. Organizations typically have stringent requirements to ensure a password is complex enough so as not to be easily hacked. Write a program that will accept as input a potential password and determine whether or not it is…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