Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
Question
Book Icon
Chapter 18, Problem 1P
Program Plan Intro

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.

Expert Solution & Answer
Check Mark
Program Description Answer

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;

}

Sample Output

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?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
In C++, write a program that reads in an array of type int. You may assume that there are fewer than 20 entries in the array. The output must be a two-column list. The first column is a list of the distinct array elements and the second column is the count of the number of occurences of each element.
In C program language, You are to read 10 numbers from a data file into an array named List. Create another 10 element array named Reverse that is to contain the same items as List but in reverse order. For example, the first element in List will be placed in the last position of Reverse, the second element in List will be placed in the second-to-last position in Reverse, etc. After Reverse has been created, output the contents of both arrays.
Answer in C++ Programming Language. C++, Array Write a program that will ask the user to enter a set of numbers and outputs all the subsets of that set. The size of the array is 5. Ask the user if he wants to repeat the program by pressing y/Y.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education