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 :)
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.
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.
#include <iostream>
#include <algorithm>
usingnamespace 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;
}
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