bubble sort and call functions
Nov 12, 2014 at 8:46pm UTC
So I'm having a little trouble with a program I have to do for a class. The objective is to use a bubble sort algorithm to sort a list of 10 randomly generated numbers (between 1 and 100) that are stored in a vector. I'm able to generate the 10 numbers but I don't know how to store them in the vector or how to bubble sort them. This is what I have so far...
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
#include <iostream>
#include <ctime>
#include <cmath>
#include <vector>
#include <string>
using namespace std;
int rangen(){ //generates 10 random numbers
int xRan;
srand(time(NULL));
for (int xRan = 0; xRan < 10; xRan++){
int y;
y = rand() % 100 + 1;
cout << y << endl;
}
return 0;
}
int main(){
vector<double > rangen(10);
cout << rangen();
system("PAUSE" );
return 0;
}
Nov 12, 2014 at 9:36pm UTC
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
#include <iostream>
#include <ctime>
#include <cstdlib> //
#include <vector>
using namespace std;
int rangen1(vector<int > &vec) // use void
{
int xRan;
srand(time(NULL));
cout << "\nfrom rangen1 function: \n" ;
for (int xRan = 0; xRan < vec.size(); xRan++){
int y;
y = rand() % 100 + 1;
cout << y << " " ;
vec.at(xRan) = y; // or vec.push_back(y);
}
cout << "\n\n" ;
return 0;
}
int main(){
vector<int > rangen(10); //variable name has been same as a function
//cout << rangen(); // prints 0
rangen1(rangen);
cout << "\nfrom main: \n" ;
for (auto x: rangen) // print all
cout << x << ", " ;
cout << "\n\n" ;
system("PAUSE" );
return 0;
}
Topic archived. No new replies allowed.