Help sorting array

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?

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace 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)
{
return false;
}
if(i == 49)
{
return true;
}
}
}

int main()
{
srand(time(NULL));
int dimensioneArray = 0;

cout << "Inserire la dimensione dell'array da controllare.\n";
cin >> dimensioneArray;

int array[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;
}


}
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 = new int[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:
 
delete [] array;
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.

Thank you for the answer though :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace 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)
		{
			return false;
		}
		if(i == 49)
		{
			return true;
		}
	}
}

int main()
{
	srand(time(NULL));
	int dimensioneArray = 0;

	cout << "Inserire la dimensione dell'array da controllare.\n";
	cin >> dimensioneArray;

	int array[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;
	}


}
Topic archived. No new replies allowed.