hi im new to c++ and at the moment trying to write a program that generates random numbers and then sorts those numbers using bubblesort. So far ive managed to get the random numbers working but i am unable to sort the random numbers with bubblesort ?

#include <iostream>
#include <cstdlib>
using namespace std;
int main ()
{
    int min, max, rsize;
    cout << "enter the amount of numbers wanted: ";
    cin >> rsize; // array size
    cout << "enter minimum: ";
    cin >> min;
    cout << "enter maximum: ";
    cin >> max;
    if (min > max) {
    cout << "your minium value is greater than maximum, it is now swapped" << endl;
    cout << "your array: " << endl;
    int temp = min;
    min = max;
    max = temp; }
    //int range = max - min + 1;
    max = max - min;
    ++max;
    srand( time(NULL) );
     int r;
     for (int i = 0; i < rsize; i++)
    {r = rand() % max + min; // the random numbers = r
     const int w = r;
    
         cout << w <<" ";
    }
    cout << endl;

{ 

double temp2, marker;
int j; 
    const int p = rsize;
    double a [p];
        for (j=0; j<p; j++){ 
    a[j]; 
    a[j] = r
}

for (marker=p; marker>1; marker--){
    j = 1; 

    for (j=0; j<p; j++) {
        if(a[j] < a[j-1]){
                temp2 = a[j-1];
                a[j-1] = a[j];
                a[j] = temp2;
                j++;
                }
}               
 }  
 
 for (j=0; j<p; j++){
     cout << a[j] << " "; // this will print the sorted numbers


    }
    system("pause"); // this is used otherwise the program begins then just suddenly terminates
      return 0; 
}

Recommended Answers

All 2 Replies

srand( time(NULL) );
int r, rsize;
for (int i = 0; i < rsize; i++)
{
   r = rand() % max + min;      
   const int w = r;
}
{
   const int p = rsize;
   double a [p];
   for (int j=0; j<p; j++)
  { 
    a[j]; 
    a[j] = r
  }

Both r and rsize are variable ints and as such cannot be used as assignments for a constant int during the course of a program. The value of a variable declared with the keyword const must be given at time of compilation, not at run time. If you won't know the size of an array until runtime then you will need to use dynamic memory to declare the size of the array.

You are writing over the value of r each time through the first loop. You want to assign the value of r to an element of p each time a new value of r is determined so move the assignemtn into the same loop as the calculation.

The curly opening brace between the two loops isn't supposed to be there.

The tradition for the nested loops of a bubble sort is to have both go the same way (either up or down) at the same time. If it works the way you have it, so be it.

You're incrementing j twice each time through the inner loop of the nested loops of the bubble sort. That won't get done what you want to get done. Get rid of the increment in the if statement of the inner loop.

Also

r = rand() % max + min;

won't always give a number in the range of min...max. You may want to consider using

r = (rand() % (max - min + 1)) + min;
commented: Good point. +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.