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
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 C++ that lets the user enter a product code.  The program should determine if the product code is valid by checking for it in the following list: 65482     15975    48758   58698   69618   78965      68416   26489   35498   91651 The codes should be initialized into an array. A simple linear search should be used to locate the code entered by the user. If the user enters a code that is in the array, the program should display a validation message. If the user enters a code that is not in the array, it should display an error message.
Given a file of unsorted words with mixed case: read the entries in the file and sort those words lexicographically. The program (in c++) should then prompt the user for an index, and display the word at that index. Since you must store the entire list in an array, you will need to know the length. The "List of 1000 Mixed Case Words" contains 1000 words. You are guaranteed that the words in the array are unique, so you don't have to worry about the order of, say, "bat" and "Bat."
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