I tried to make a code to sort an array in which the user specify the dimension of the array and then the program check whether it's sorted from the smallest number to the biggest number and if not, it sorts the array, printing it sorted in the end.
But it doesn't run, it stops after the user inserts the dimension of the array. Could you help me, please?
Hello Eliijahh. When posting code in the future, please surround your code with code tags which can be found to the right of the post text box. (click on the <> icon.)
Technically that code shouldn't compile. in C++, you can't create an array of unknown size like that. If it is compiling and it stops after you input the size, my guess is that it's crashing because of that.
This is how you declare an array of variable size:
int *array = newint[dimensioneArray];
This dynamically allocates memory for your array.
However, whenever you allocate memory, you must make sure to free it to avoid memory leaks. To do this, at the end of your code, insert:
The program doesn't crash. After I enter the dimension of the array the program is just stuck. It doesn't end, it doesn't continue. I can't do anything.
In any case I put the array into the stack, and even if I put a really really small dimension like 2, shouldn't the stack manage to load the array? Am I forced to put arrays on the heap? Cos I made another program using arrays and I put it on the stack, though it was a defined array, not unkwnown.