code is not working

Hi All

I have built to programs, one that generates a seat of random numbers based on the user requirements. So they enter how many random numbers they would like to generate and the range. The other was to sorrt a set of predfined numbers using bubble sorted. I them merged the two programs together.

I am trying to get random numbers generated then sorted via bubble sort technique. Below is my code. Can someone tell me whats wrong?

Thanks in advance

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream> 
#include <stdio.h>
#include <ctime> 
#include <cstdlib>
using namespace std;
int random_integer;
int generate;
int random();
//int bubble();
//int selection();
void bubbleSort(int *array,int length);
void printElements(int *array,int length);

int main()
{
	random();
	system ("Pause");
	int a[]={random_integer};   // array to sort 
    bubbleSort(a,10);                 //call to bubble sort  
    printElements(a,10);               // print elements
	return 0;
}
int random() 
{ 
    srand((unsigned)time(0)); 
    int lowest, highest; 
	
	cout<<"How Many Numbers would you like to generate?: ";
	cin>>generate;
	cout<<"Please Enter the Lowest number of your Range: ";
	cin>>lowest;
	cout<<"Please Enter the Highest number of your range: ";
	cin>>highest;
	int range=(highest-lowest)+1;
	system("cls");
	cout<<"Your Unsorted Random Numbers are: \n";
    for(int index=0; index<generate; index++)
	{ 
        random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
        cout << random_integer<<", "; 
		
    } 
	cout<<endl;
	return 0;
}

void bubbleSort(int *array,int length)//Bubble sort function 
{
    int i,j;
    for(i=0;i<generate;i++)
    {
        for(j=0;j<i;j++)
        {
            if(array[i]>array[j])
            {
                int temp=array[i]; //swap 
                array[i]=array[j];
                array[j]=temp;

                printElements(array, length);
            }

        }

    }

}
void printElements(int *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<generate;i++)
        cout<<array[i]<<", ";
    cout << endl;
}
Last edited on
Please help, if anyone can
OK, you have a few issues here.

In future, can you be more specific about errors - post the compiler output in full

I imagine this would compile with lots of warnings.

The random function does not put anything into the array a.

There are a number times you use uninitialised variables. Line 18 puts a random number into the first element of the array, but only because the variable is uninitialised, and there is no size of the array specified, so C++11 sets it to 1 because of the brace initialisation . So when the BubbleSort function is called, there is only one element in the array.

The main idea you need to learn is that one cannot specify the size of an array at runtime - it must be a constant specified size at compile time.

And you need to ALWAYS make sure your variables are initialised with something. Try initialising with some value at the same time as declaration. There are situations where this can cause problems, but there is a difference between a wrong answer and a garbage answer. Example answer is 100 instead of 10, versus answer = 17896543 instead of 10. So just think about what initial value a variable is going to have - one that isn't going to cause problems.

So I think you should either have a specified size like
1
2
const unsigned SIZE = 10;
int MyArray[SIZE];

say, or use a vector.

With vectors you can use the push_back function to put objects into the vector. Read this:

http://www.cplusplus.com/reference/vector/vector/


Hope this helps - Cheers.
Thanks for your reply, i am still very new to C++. Is there anyway that i could use a dynamic array or someone mentioned used a string?
Why can't you use a vector? A vector is a dynamic array - in the sense that it resizes itself.

Why would you use strings, when you have numbers that you want to do calculations on?

I have to go out now - have a go with the vector, or use an array with a specified size.
Last edited on
brill thanks for your help, i will give it ago
Topic archived. No new replies allowed.