Hi, this algorithm is not working, i am trying to sort random numbers generated by the program, then using bubble sort to sort them. When the code runs i get negative numbers that are out of my range, which should not happen. I am asking the program to generate 5 numbers between 1 and 5.
#include <iostream>
#include <ctime>
usingnamespace std;
void bubble_Sort(int arr[], int n);
int main()
{
int arr[1000];
int array;
int low, high;
int random_integer;
srand((unsigned)time(0)); ///// random number generator
cout<<"How Many Numbers would you like to Generate"<< endl;
cin>> array;
cout<<"What is the lowest number of your range: ";
cin>>low;
cout<<"What is the highest number of your range: ";
cin>>high;
int range=(high-low)+1;
cout<<" Your Random Numbers to be sorted are: "<< endl;
for (int i =0; i < array; i++)
{
arr[i] = random_integer = low+int(range*rand()/(RAND_MAX + 1.0));
cout << arr[random_integer] << ", ";
}
bubble_Sort( arr, array);
//cin>>arr[int i];
cout << endl;
cout<<"Your Sorted numbers are: \n";
for (int i =0; i < array; i++)
cout<<arr[i]<< ", ";
cout<<"\n";
}
// Bubble Sort
void bubble_Sort( int arr[], int n)
{
for (int i=0; i< n; i++)
for(int j=0; j< n-i-1; j++)
if(arr[j] > arr[j+1])
{
int tmp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = tmp;
}
}
The Plan of this program is that the user can decided how many numbers can be generated and the range of these numbers, then its sorted using the bubble sort