Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134521176
Author: 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.
Please help me with this: using js create an array of 30 random numbers that range between 1and 100. And yet again, write a function that will receive a number from the userand determine if that number exists in the array or not. But this time, start bySORTING your input list. After a sort, the list in problem 1 is as follows:[2, 2, 3, 5, 12, 14, 14, 15, 23, 36, 39, 41, 44, 44, 45, 48,49, 50, 52, 52, 59, 71, 81, 82, 88, 89, 89, 93, 96, 97] Approach: Implement a method called findC(x, A, i, j), where x is the number we arelooking for in array A, the first index of the array is i and the last index is j. We wantto determine whether x exists in A anywhere between index i and index j. Your firstcall to this method will therefore look like this: findC(x, A, 0, A.length-1). In the body of your function, compare x with the item that is in the middle of thearray, as you did before. As before, call the middle of index of the array mid. But thistime, if x<=a[mid], recursively call your…
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