😁Hello guys i need help, i have the following exercise:
1- Ask the user to enter ten numeric values and sort these in ascending order.
values, storing them in a vector. Order the value as it is entered. show the
final on the screen the values in order. My code sorts after reading, but it asks to sort as values are read, how do i do it?
----------------------------begin of My code---------------------------
#include <iostream>
#include <array>
int main(){
std::array<int,10>v{};
//input values
for(int i = 0;i < v.size();i++){
std::cout<<"value-> ";
std::cin>>v.at(i);
}
// sorted array
int j{};
std::cout<<"Array sorted->";
while(j < v.size()){
for(int k = j+1; k < v.size();k++){
if(v.at(j) > v.at(k)){
int exchange{};
exchange = v.at(j);
v.at(j) = v.at(k);
v.at(k) = exchange;
}
}
// output array
std::cout<<" "<<v.at(j);
j++;
}
return 0;
}
------------end code---------------------------------------------
set a flag, let's say sort, to true if the user enters second number
Now loop from back, if number is greater insert infront if not at back
You can use std::vector::rcbegin() to loop backwards
Anonymous
Pavel
Sarthak
\Device\NUL
Kevin
Naol D
smene
Dima
Gilded
Ludovic 'Archivist'