+ 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
14 odpowiedzi
+ 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.
+ 12
int count;
std::cout << "Please enter the number of elements";
std::cin >> count;
int * array = new int[count];
+ 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/
+ 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);
+ 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.
+ 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.
+ 1
Don't forget to upvote the question if you like it friends !
+ 1
Thank you Hatsy Rei !
Where did you learn about vectors ? I didn't find it in the SoloLearn C++ course !
+ 1
int count;
std::cout << "Please enter the number of elements";
std::cin >> count;
auto array = std::make_unique<int[]>(count);
+ 1
why are you guys using std:: instead of cout << and putting using namespace std; at the header
+ 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.
+ 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.
+ 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!
0
@Hatsy Rei
thanks so much !