Array, stack, heap question

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 = new int[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.

Some help, pls? What am I doing wrong?
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.

delete []array;
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. :(

Thank you for the answer, xhtmlx :)
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.
Last edited on
Ah thank you so much, peter87.

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.
closed account (D80DSL3A)
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.

OK. Simple test:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int f(int x)
{
    if( x > 2 )
        return 2;
    else if( x < -2 )
        return -2;
}

int main()
{
    cout << "f(4) = " << f(4) << endl;
    cout << "f(-6) = " << f(-6) << endl;
    cout << "f(1) = " << f(1) << endl;

    cout << endl;
    return 0;
}

Output:

f(4) = 2
f(-6) = -2
f(1) = 0

Looks like the default value for the type is returned?

The actual warning was:
...\main.cpp|33|warning: control reaches end of non-void function|
Looks like the default value for the type is returned?


What's returned is undefined.
Topic archived. No new replies allowed.