#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
usingnamespace std;
bool controllaIfSorted(int array[], int dimensioneArray)
{
for(int i = 0; i<dimensioneArray; i++)
{
int controllore = 0;
controllore = array[i] - array[i+1];
if(controllore > 0)
{
returnfalse;
}
if(i == 49)
{
returntrue;
}
}
}
int main()
{
srand(time(NULL));
int dimensioneArray = 0;
cout << "Inserire la dimensione dell'array da controllare.\n";
cin >> dimensioneArray;
int *array = newint[dimensioneArray];
bool isSorted = false;
for(int i = 0; i<dimensioneArray; i++)
{
array[i] = rand() % (100-1)+1;
}
isSorted = controllaIfSorted(array, dimensioneArray);
while(isSorted == false)
{
for(int i = 0; i < dimensioneArray; i++)
{
int val1 = array[i];
int val2 = array[i+1];
if((val1-val2) > 0)
{
array[i] = val2;
array[i+1] = val1;
}
}
isSorted = controllaIfSorted(array, dimensioneArray);
}
cout << "L'array è ordinato dal più piccolo al più grande.\n";
for(int i = 0; i<dimensioneArray; i++)
{
cout << array[i] << endl;
}
}
I'm learning c++ and I was trying to make a little program, for an exercise, to check whether an array is sorted from array[0] the smallest number to array[n] the biggest. In the program the user can define the array's dimension. The problem is that when I compile the program, after I insert the dimension, the program is stuck, like in a endless loop, it doesn't go on, it doesn't crash, it's just stuck.
Someone suggested me to put the array on the heap, but I don't really know how to do it, and when I tried to find some informations and do it myself, the result didn't actually change.
Your array is going to be one-dimensional regardless of what the user inputs. You are also going to need to deallocate the allocated chunk of memory. If you don't, then you will have a memory leak.
It's okay for the array to be one-dimensional for now.
But since the array created in this way is on the stack, do I still have to delete it? And when is the time to delete it? Consider that this program finish with itself. It's not a part of another program, just an exercise to learn c++.
I just can't understand why the program, after I insert the dimension, is stuck. Everything seems all right. :(
You are creating the array on the heap because you are using new.
The problem is that controllaIfSorted is not working as it should. You need to return something after the loop. I also don't get what the second if statement inside the loop is for.
I understand what I did wrong. The controllaIfSorted function didn't have the return after the loop. When I erased the if(i == 49) and returned true after the end of the loop, the program went fine.
So in c++ the function have to return a value outside loops. You can't just put the return inside a loop?
The important thing is that all non-void functions always return a value. If the return is inside a loop or not doesn't matter, but in many cases writing the return after the loop is the easiest way.
Usually I will get a compiler warning along the lines of "not all control paths return a value" when I have written a function which may "dead end" return wise.