Write a program that inputs ten double-precision numbers into an array named raw
ID: 3533217 • Letter: W
Question
Write a program that inputs ten double-precision numbers into an array named
raw. After ten user-input numbers are entered into the array, your program
should cycle through raw ten times. During each pass through the array, your
program should select the lowest value in raw and place the selected value in the
next available slot in an array named sorted. Thus, when your program is
complete, the sorted array should contain the numbers in raw in sorted order
from lowest to highest. (Hint: Be sure to reset the lowest value selected during
each pass to a number which is greater than the maximum value in array raw so
that it is not selected again. You will need a second for loop within the first for
loop to locate the minimum value for each pass.)
Explanation / Answer
#include <vector>
#include <algorithm>
#include <iostream>
template<class Vals>
void sortingPermutation(const Vals& values, std::vector<int>& v){
int size = values.size();
v.clear(); v.reserve(size);
for(int i=0; i < size; ++i)
v.push_back(i);
std::sort(v.begin(), v.end(), [&values](int a, int b) -> bool {
return values[a] < values[b];
});
}
int main()
{
std::vector<double> values;
values.push_back(24);
values.push_back(55);
values.push_back(22);
values.push_back(1);
std::vector<int> permutation;
sortingPermutation(values, permutation);
typedef std::vector<int>::const_iterator I;
for (I p = permutation.begin(); p != permutation.end(); ++p)
std::cout << *p << " ";
std::cout << " ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.