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
Using C++ Write a program that reads 10,000 words into an array of strings; the "List of 10,000 Random Words" file is on Moodle. The program will then read a second file (the list of "Search Words" is also on Moodle) that contains an undetermined number of words and search the first array for each word. The program will then report the number of words in the second list that were found on the first list. Do not use an array for the second file, process the words as they are read. Note that some of these "words" might have spaces; handle your input accordingly.
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.
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.
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