How can we make this code able to arrange any number x of integers ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How can we make this code able to arrange any number x of integers ?

This code reorder 10 numbers from the user in an increasing order. suppose that we don't know how many number the user will enter, what is the modifications to do to this code in C++ such as it stills work correctly ? Good luck 😉 http://code.sololearn.com/ci8168oTsiNH/?ref=app

5th Sep 2017, 5:53 AM
Hamza Ba-mohammed
Hamza Ba-mohammed - avatar
14 Answers
+ 12
You're talking about a sentinel controlled loop. Interesting. I can combine it with vectors. This code probably does what you want: #include <iostream> #include <vector> int main() { std::vector<int> obj; bool input = true; int value; while (input) { std::cin >> value; if (std::cin.fail()) { std::cin.clear(); std::cin.ignore(512, '\n'); input = false; } else obj.push_back(value); } // more codes } When you input "ok", it flags the input stream as fail, so you clear the input stream and flip the boolean variable to terminate the loop.
5th Sep 2017, 9:20 AM
Hatsy Rei
Hatsy Rei - avatar
+ 12
int count; std::cout << "Please enter the number of elements"; std::cin >> count; int * array = new int[count];
5th Sep 2017, 6:18 AM
Hatsy Rei
Hatsy Rei - avatar
+ 11
@Hamza Ba-mohammed C++ has a scope so wide, I'm not sure if SoloLearn covered 1% of it. Extra reference will always be needed. The C++ official documentation can be of much help. http://en.cppreference.com/w/
6th Sep 2017, 7:01 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
I'd do the same as Hasty Rei, but using the modern C++ way; int count; std::cout << "Please enter the number of elements"; std::cin >> count; auto array = std::make_unique<int[]>(count);
5th Sep 2017, 6:30 AM
aklex
aklex - avatar
+ 2
If you don't know the number of elements, it is often a better idea to use a list. Unless you want the contiguous container for some operation, then you could use vector with push_back.
5th Sep 2017, 9:51 PM
Denis Felipe
Denis Felipe - avatar
+ 1
It's true, but we want to do it without asking the user to precise the number of elements that he'll input, like making another sign to stop inputting data in the array. Hint : the code stop inputting numbers in the array when the user enter the string "OK" to the program. It will be easier.
5th Sep 2017, 8:37 AM
Hamza Ba-mohammed
Hamza Ba-mohammed - avatar
+ 1
Don't forget to upvote the question if you like it friends !
5th Sep 2017, 8:41 AM
Hamza Ba-mohammed
Hamza Ba-mohammed - avatar
+ 1
Thank you Hatsy Rei ! Where did you learn about vectors ? I didn't find it in the SoloLearn C++ course !
5th Sep 2017, 9:14 PM
Hamza Ba-mohammed
Hamza Ba-mohammed - avatar
+ 1
int count; std::cout << "Please enter the number of elements"; std::cin >> count; auto array = std::make_unique<int[]>(count);
6th Sep 2017, 2:24 AM
Nawlesh kumar
+ 1
why are you guys using std:: instead of cout << and putting using namespace std; at the header
7th Sep 2017, 2:02 AM
adam
adam - avatar
+ 1
Can do with 2 approach. an input of how many items to be entered or a loop that will ask if you would like to add or terminate input and process the data. In cpp i would use vector as it is dynamic in size.
7th Sep 2017, 1:11 PM
Mark Macky Anacan
Mark Macky Anacan - avatar
+ 1
Vector is not exactly dynamic. Every time you add one extra element over the capacity of a vector, it allocates another space in memory of double size and copies all elements to it. Depending of the number of times this happen, it can affect the computational time negatively.
7th Sep 2017, 2:03 PM
Denis Felipe
Denis Felipe - avatar
+ 1
You can solve that easily by using a while/for loop and and going through the list of the integers. The only problem is that this loops my sometimes make your code too complex, which eventually may leas to suffer from bugs that are hard to be detected. There programs who helps doing that, such as checkmarx but if you choose to do that on your own, be sure to work hard as it;s not that easy sometimes. Anyway, good luck!
14th Sep 2017, 8:08 AM
Ben hart
0
@Hatsy Rei thanks so much !
7th Sep 2017, 2:48 AM
Hamza Ba-mohammed
Hamza Ba-mohammed - avatar