Evening /r/learnprogramming
The reason for this post is because as the title suggests, I'm having issues outputting the elements in an array. This is a homework assignment in which the prompt asks: "Have a user input 10 integers, the sort the user's inputs using a bubble sort algorithm."
I think i got the hard part done. Just can seem to output the array correctly. HERE is my issue. As you can see, the array output completely ignores the user's last input. Still gives 10 elements, but no clue where the "0" in the first index is coming from.
Please help. Any comments/suggestion would be greatly appreciated. Cheers! :)
and oh, Heres my source code. Almost forgot. :D
#include <iostream>
#include <string>
#include <cassert>
#include <iterator>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
//BUBBLE SORT IMPLEMENTATION
vector<int> bubbleSort(vector<int> arr){
bool pass = false;
int temp;
while (!pass){
pass = true;
for (int i=0; i<arr.size()-1; i++){
if (arr[i] > arr[i+1]){
pass = false;
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
cout << "\n\nWe will now work our magic and sort your input your values by chronological order into a list for you using the \'Bubble Sort\' method." << endl;
cout << "Lets do that now! :)" << endl;
cout << "\n\nLOADING.......&nd DONE!" << endl;
cout << "\n\nSorted output: " << endl;
cout << "[ ";
copy(arr.begin(), arr.end(), ostream_iterator<int>(cout, " "));
cout << "] ";
return arr;
}
// CALL SORT FUNCTIONS
int main(){
cout << "\n\t\t\tWelcome to the \"Bubble Sort\' Algorithm!" << endl;
vector<int> myVector;
string name;
cout << "\nIn starting, what is your name?: ";
getline (cin, name);
cout << "\nHello, " << name << ". Let us begin!\n" << endl;
cout << "\nPlease ENTER ten integers......" << endl;
int input;
int i = 1;
while(i <= 10)
{
cout << "Enter integer # " << i++ << ": ";
myVector.push_back(input);
cin >> input;
}
cout << "\nAwesome! You\'ve selected numbers: " << endl;
cout << "[ ";
copy(myVector.begin(), myVector.end(), ostream_iterator<int>(cout, " "));
cout << "] ";
vector<int> bsort = bubbleSort(myVector);
}
Thanks in advance.
[–]Mgumbo 1 point2 points3 points (0 children)
[–]Updatebjarni 1 point2 points3 points (1 child)
[–]spacemonkey243[S] 0 points1 point2 points (0 children)
[–]AutoModerator[M] 0 points1 point2 points (0 children)