Problem Solving with C++, Student Value Edition
Problem Solving with C++, Student Value Edition
10th Edition
ISBN: 9780134543680
Author: Walter Savitch
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
Rewrite the Binary search function in the textbook to use a generic type for the array elements. Test the function with arrays of int, double, and string values. *When using test arrays make sure elements are stored in ascending sorted order*
Write a program in Python that uses a dynamic array to enter a list of strings into it. Allow the user to enter as many elements as they want by inserting them, then letting them delete it when need be. Please explain each line.
Can you help me write a C++ Program to do the following:  Create a generic function increment(start, stop, x) thatadds x to every element in the range [start,stop). The addition isdone using the + operator. The arguments start and stop are bidirectional iterators. Write a test driver
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning