Basically, I'm unsure of how to utilize the function rand(). All I want to do is create 20 random numbers, use a bubble sort on them, print the sorted list and the time it takes to sort it.
Code:#include <cstdlib> #include <iostream> #include <ctime> #include <cstdlib> using std::cout; using std::endl; int main() { void bubble(int a[], int n); void print(int a[], int n); int random(int n); int n = 20; int a[n]; a[n] = random(n); clock_t start = clock(); bubble(a, n); clock_t end = clock(); print(a, n); double time_elapsed = double(end - start)/CLOCKS_PER_SEC; system("PAUSE"); } void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } void bubble(int a[], int n) { int sorted = false; int pass = 1; while (pass < n && !sorted) { sorted = true; for ( int i = 0; i <= n - pass - 1; i++) { if (a[i] > a[i + 1]) { swap(a[i], a[i + 1]); sorted = false; } } pass = pass + 1; } } void print(const int a[], int n) { for (int i = 0; i <= n; i++) cout << a[i] << " " << endl; } int random(int n) { int a[n]; for (int i = 0; i <= n; i++) a[i] = rand(); return a[n]; }