Trouble with sorting an array.

I'm sure this code is complete gore to anyone who is fluent in C++, but I'm not sure how to go about sorting a user-inserted array and printing the middle element.

Any help is appreciated! If there's an easier way to do it just point me in the right direction if you would :)

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
67
68
69
70
71
72
73
74
75
76
77
  #include <iostream>
using namespace std;

int main() {
	int medianArray[5]{};
	int temp;
	cout << "Enter integer number one." << endl;
	cin >> medianArray[0];

	cout << "Enter integer number two." << endl;
	cin >> medianArray[1];
	if (medianArray[1] < medianArray[0]) {
		temp = medianArray[1];
		medianArray[1] = medianArray[0];
		medianArray[0] = temp;
	}

	cout << "Enter integer number three." << endl;
	cin >> medianArray[2];
	if (medianArray[2] < medianArray[1]) {
		temp = medianArray[2];
		medianArray[2] = medianArray[1];
		medianArray[1] = temp;
	}
	if (medianArray[2] < medianArray[0]) {
			temp = medianArray[2];
			medianArray[2] = medianArray[0];
			medianArray[0] = temp;
	}

	cout << "Enter integer number four." << endl;
	cin >> medianArray[3];
	if (medianArray[3] < medianArray[2]) {
		temp = medianArray[3];
		medianArray[3] = medianArray[2];
		medianArray[2] = temp;
	}
		if (medianArray[3] < medianArray[1]) {
			temp = medianArray[3];
			medianArray[3] = medianArray[1];
			medianArray[1] = temp;
	}
		if (medianArray[3] < medianArray[0]) {
			temp = medianArray[3];
			medianArray[3] = medianArray[0];
			medianArray[0] = temp;
	}


	cout << "Enter integer number five." << endl;
	cin >> medianArray[4];
	if (medianArray[4] < medianArray[3]) {
		temp = medianArray[4];
		medianArray[4] = medianArray[3];
		medianArray[3] = temp;
	}
	if (medianArray[4] < medianArray[2]) {
		temp = medianArray[4];
		medianArray[4] = medianArray[2];
		medianArray[2] = temp;
	}
	if (medianArray[4] < medianArray[1]) {
		temp = medianArray[4];
		medianArray[4] = medianArray[1];
		medianArray[1] = temp;
	}
	if (medianArray[4] < medianArray[0]) {
		temp = medianArray[4];
		medianArray[4] = medianArray[0];
		medianArray[0] = temp;
	}
			
	cout << "The median is " << medianArray[2] << endl;

	return 0;

}
Have you heard about loops? I suggest you use one or two for the user input.

Then use your favorite search engine and look up "c++ sorting" and study some of the links. Once you have the array sorted finding the middle is an easy mathematical function.

Fetch all five integers together:
1
2
3
4
5
for (int i=0; i<5; i++)
{
  cout << "Enter value " << i+1 << ": " << endl;
  cin >> medianArray[i];
}


then sort them

1
2
#include <algorithm>
sort (medianArray[0], medianArray[5]);


Output middle one:
 
cout << "Median: " << medianArray[2];


instead of sorting the array as you read the integers you could just read them, afterwards sorting it. if you are new to sorting algorithms i reccomend learning insertion sort as it is probably the easiest to implement and a pretty efficient one for its simple implementation

the algorithm works as follows: for each element in the array check the previous elements, if the current element is less than the previous one, swap them, then go on the previous element till you reach the first element, then go on the next element in the array.

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
int main(){
	
	//create array and size variable
	int medians[10];
	int size = 10;

	//read input
	for (int i = 0; i < size; i++)
	{
		cout << "Median number " << i + 1 << ": ";
		cin >> medians[i];
	}

	//simple sort. (insertion sort)
	int temp, j;
	for (int i = 0; i < size; i++)
	{
		j = i;
		while (j > 0 && medians[j] < medians[j - 1])
		{
			temp = medians[j];
			medians[j] = medians[j - 1];
			medians[j - 1] = temp;
			j--;
		}
	}

	//output medians sorted
	for (int i = 0; i < size; i++)
	{
		cout << medians[i] << " ";
	}

	return 0;
}


a sorting algorithm that i reccomend checking out is quick sort, beware that quicksort is very efficient for large arrays
Hey, everyone.

I feel like a total idiot. Apparently for this segment of the assignment, I can in fact use other libraries, namely the "algorithm" library.

So sorry for the hassle and thanks for the help, everyone!!!
The sort method with the algorithm library isn't working?

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
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int medianArray[5]={};

	cout << "User! It's been awhile! Good to see you again." << endl;
	cout << "I'm pretty bored and have been practicing arrays! Check this out." << endl;
	
	cout << "Start by inserting one number, from one to one hundred (no negative values, please!)." << endl;
	cin >> medianArray[0];

	cout << "Cool, good job. Now number two!" << endl;
	cin >> medianArray[1];

	cout << "Now the third number!" << endl;
	cin >> medianArray[2];

	cout << "Alright, now number four; almost done!" << endl;
	cin >> medianArray[3];

	cout << "Great, last one! One more number, please." << endl;
	cin >> medianArray[4];
	
	sort(medianArray[0], medianArray[5]);

	cout << "Thanks for your help, user! The median number given the ones you entered is " << medianArray[2] << endl;

	return 0;


}
sort(&medianArray[0], &medianArray[5]);

you have to pass the array as a pointer, not as a value, so you have to pass the adress of the element from medianArray[0] , you do that by putting & in front of it, if you just pass medianArray[0] you will give the function the first value of the array, the function algoritm doesnt know what to do with that variable he needs a start point and a end point, not the values of the start point and the end point
I'm fairly sure the more usual way is to do sort(medianArray.begin(), medianArray.end()).
Topic archived. No new replies allowed.