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.
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;
}
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?
Chapter 18 Solutions
EBK PROBLEM SOLVING WITH C++
- Write a program that will take this dictionary: gradePoints = {"A":4,"B":3,"C":2,"D":1,"F":0} and this list: courseList = ["CST 161","Mat 144","ENG 201","PSY 101","HIS 101"] and randomly use elements from each of these lists : gradeList = ["A","B","C","D","F"] creditList = [3,4] and produce a grade point average.arrow_forwardAsk the user to enter a series of integers, and store these numbers into a linked list. The user can enter as many numbers as she like, so your program must keep asking for more until she enters -100 to signify she wants to stop. Please note that your program must store the number into a *linked list*, and not an array, because in theory, the user can enter unlimited number of integers, and all array has a fixed size. * Implement a C function to perform merge sort. This function takes the linked list above as the input, and sort it using the Merge Sort algorithm. You do have a choice here: You can either perform the merge sort directly on the linked list (more fun), or copy the numbers from the linked lists into an array first, and then sort the array (more familiar).arrow_forwardGo program using generics Output True Falsearrow_forward
- C++ Create a generic function increment(start, stop, x) that adds x to every element in the range [start,stop). The addition is done using the + operator. The arguments start and stop are bidirectional iterators. Write a test driver.arrow_forwardIt is not desirable to use a static array like aux[] in library software since numerous clients may be using the class at once. Give a Merge implementation that doesn't depend on a static array. Do not localise aux[] to combine()Hint: Pass the auxiliary array to the recursive sort() function as an argument.arrow_forwardWrite a Python function countEvens that consumes an integer N and a nested list representing a matrix of arbitrary size (but larger than NxN). The function returns the number of even numbers in the NxN sub-matrix of the first N rows and columns. Note: You may assume the list argument passed to the function is a nested list of integers, but it may be larger than NxN!arrow_forward
- C++ Create a generic function average({elements}) that returns the average of all the given elements. For example, average({x, y, z}), returns (x + y + z)/3. The argument is an initializer list that is assumed not to be empty. For the sake of the exercise, don’t use other STL algorithms in the implementation of the function. Write a test driverarrow_forwardWrite a function that gets a linked list of ints, and reverses it. For example - on input 1 -> 2 -> 3 -> 4, after the function finishes the execution, the list becomes 4 -> 3 -> 2 -> 1 - If the list has one element, then it doesn’t change - If the list is empty, then it doesn’t change You may use the data fields in the struct and the functions provided in LL.h and LL.c. // reverses a linked list void LL_reverse(LL_t* list); Test for the Function: void test_q3() { LL_t* lst = LLcreate(); LL_add_to_tail(lst, 1); LL_add_to_tail(lst, 3); LL_add_to_tail(lst, 8); LL_add_to_tail(lst, 4); LL_add_to_tail(lst, 3); LL_reverse(lst); intcorrect[] = {3,4,8,3,1}; inti; node_t* n = lst->head; for(i=0;i<5;i++) { if (n==NULL) { printf("Q3 ERROR: node %d==NULL unexpected\n", i); return; } if (n->data != correct[i]) { printf("Q3 ERROR: node%d->data==%d, expected %d\n", i, n->data, correct[i]); return; } n = n->next; } if (n==NULL) printf("Q3 ok\n"); } Support…arrow_forwardWrite a function that gets a linked list of ints, and reverses it. For example - on input 1 -> 2 -> 3 -> 4, after the function finishes the execution, the list becomes 4 -> 3 -> 2 -> 1 - If the list has one element, then it doesn’t change - If the list is empty, then it doesn’t change You may use the data fields in the struct and the functions provided in LL.h and LL.c. // reverses a linked list void LL_reverse(LL_t* list); Test for the Function; void test_q3() { LL_t* lst = LLcreate(); LL_add_to_tail(lst, 1); LL_add_to_tail(lst, 3); LL_add_to_tail(lst, 8); LL_add_to_tail(lst, 4); LL_add_to_tail(lst, 3); LL_reverse(lst); intcorrect[] = {3,4,8,3,1}; inti; node_t* n = lst->head; for(i=0;i<5;i++) { if (n==NULL) { printf("Q3 ERROR: node %d==NULL unexpected\n", i); return; } if (n->data != correct[i]) { printf("Q3 ERROR: node%d->data==%d, expected %d\n", i, n->data, correct[i]); return; } n = n->next; } if (n==NULL) printf("Q3 ok\n"); } Support File…arrow_forward
- Create a data type that allows you to insert, delete the maximum, and delete the minimum (all in logarithmic time), as well as find the maximum and find the minimum (both in constant time). Tip: Use two heaps.arrow_forwardImplement two out of three of the basic sorting algorithms - Bubble Sort, Selection Sort, and Insertion Sort - in a generic List. Initial code to be completed: #include <iostream>#include "linkedlist.h" void bubbleSort(List*);void selectionSort(List*);void insertionSort(List*); /** * This activity is focused on using Arrays and Linked Lists as two different * implementations of List. It follows that you are only to use the methods of * List and not of the specific array or linkedlist. */int main(void) { // WARNING! Do not modify main method! // Doing so will nullify your score for this activity. char li; cin >> li; List* list; if (li == 'A') { list = new ArrayList(); } else { list = new LinkedList(); } int length; cin >> length; int input; for (int i = 0; i < length; i++) { cin >> input; list->add(input); } char sym; cin >> sym; int arg1; switch (sym) {…arrow_forwardWrite a program in C or Java to delete an element at desired position from an array.Test Data :Input the size of array : 5Input 5 elements in the array in ascending order:element - 0 : 1element - 1 : 2element - 2 : 3element - 3 : 4element - 4 : 5Input the position where to delete: 3Expected Output :The new list is : 1 2 4 5arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning