Could someone explain how program works?

Hello everyone, I'm having trouble with a program that allows a user to enter the elements in an array and then outputs them in order from smallest to greatest.
I tried for about a week before I looked up the answer and I would be very grateful if someone could explain the parts I don't understand, and why what I was trying is incorrect.
Here is the correct program:

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
#include <iostream>
#include <string>

using namespace std;
#define space cout << endl;

int main()
{
	//Create empty array
	int myArr[10];

	int smallest;
	int pos;
	int temp;
	int length = sizeof(myArr) / sizeof(myArr[0]);

	//Assigns user input to the integers in the array
	cout << "Please enter the unsorted numbers one at a time: " << endl;
	for (int x = 0; x < 10; ++x)
	{
		cout << "[" << x << "] = ";
		cin >> myArr[x];
	}
	space

		//Shows the list of integers in the array unsorted
		cout << "Unsorted list = ";
	for (int x = 0; x < 10; ++x)
	{
		cout << myArr[x] << ", ";
	}
	space
	space

	cout << "Sorting......" << endl;
	space

	cout << "Sorted list = ";
	for (int y = 0; y < length - 1; ++y)
	{
		smallest = myArr[y];

		for (int j = y; j < length; ++j)
		{
			if (myArr[j] < smallest)
			{
				smallest = myArr[j];
			        pos = j;
			}
		}

		if (smallest != myArr[y])
		{
			temp = myArr[pos];
			myArr[pos] = myArr[y];
			myArr[y] = temp;
		}

		cout << myArr[y] << ", ";
	}

	cout << myArr[length - 1];
	space
	space
}


The part I'm not understanding is why the length variable is using sizeof. I know how sizeof works for finding how many bytes a type is on a system but have no idea how it's working here. And I'm not understanding all the variable reassignments in the last if statement. Also I'm not sure why the pos variable is necessary.

Here is the program I wrote and to me it seems like it should work but when it should output the sorted list it just outputs the same as the unsorted. So, could someone explain why this is incorrect.

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
#include <iostream>
#include <string>

using namespace std;
#define space cout << endl;

int main()
{
	//Create empty array
	int myArr[10];

	int smallest = myArr[0];

	//Assigns user input to the integers in the array
	cout << "Please enter the unsorted numbers one at a time: " << endl;
	for (int x = 0; x < 10; ++x)
	{
		cout << "[" << x << "] = ";
		cin >> myArr[x];
	}
	space

		//Shows the list of integers in the array unsorted
		cout << "Unsorted list = ";
	for (int x = 0; x < 10; ++x)
	{
		cout << myArr[x] << ", ";
	}
	space
	space

		cout << "Sorting......" << endl;
	space

		cout << "Sorted list = ";
	for (int y = 1; y < 10; ++y)
	{
		if (myArr[y] < smallest)
		{
			smallest = myArr[y];
		}
	}

	for (int i = 0; i < 10; ++i)
	{
		cout << myArr[i] << ", ";
	}
	space
	space

}
Last edited on
Both programs are poorly written, but that's not what this is about. In specific, you want to understand two things, the first being this:

int length = sizeof(myArr) / sizeof(myArr[0]);

The size of the entire array is the number of bytes in memory is occupies (e.g. 40), whereas the size of an individual element in the array is how many bytes each element takes up (e.g. 4), so when you divide them (40/4) you get the number of elements in the array.

Generally speaking though, arrays like this are deprecated.

The second thing you want to know is why your implementation does not sort the numbers - could you tell me where you make any attempt to rearrange the array? I don't see any line other than line 19 that changes the values in the array.

The second thing you want to know is why your implementation does not sort the numbers - could you tell me where you make any attempt to rearrange the array? I don't see any line other than line 19 that changes the values in the array.


Now that you mention it, I just noticed I only changed the smallest variable and not the order of the array elements at all....I'm an idiot.
Well at least the second question is solved.


The size of the entire array is the number of bytes in memory is occupies (e.g. 40), whereas the size of an individual element in the array is how many bytes each element takes up (e.g. 4), so when you divide them (40/4) you get the number of elements in the array.


So,

 
for (int y = 0; y < length - 1; ++y)


is the same as,

 
for (int y = 0; y < 9; ++y)


So in the correct program
1
2
3
4
5
6
if (smallest != myArr[y])
		{
			temp = myArr[pos];
			myArr[pos] = myArr[y];
			myArr[y] = temp;
		}


rearranges the array, but I'm not understanding how it does it.
Here's something that may help: take three cups and put air in one, water in another, and milk (or some other liquid) in the last. Mark "temp" on the one with air, "A" on the one with water, and "B" on the one with milk.

Then, try to "swap" the contents of A and B.
Last edited on
Thanks I understand it now.
Topic archived. No new replies allowed.