random numbers from 50 ~ 100

I have a code that generates 40 random numbers from 50~100 and put them in array and then sort it from smallest to largest. also print out 5 smallest at the end
I really don't know why but first number in my array is always 40.
everything else looks fine.

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
  
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int random();
void sort(int a[], int n);
void print5(int a[]);

int main()
{
    const int ArraySize = 40;
    int array[ArraySize];
    srand(time(NULL));

    for (int i = 0; i < ArraySize;i++) {
        array[i] = random();
    }

    sort(array,ArraySize);
    print5(array);
    return 0;
}

int random()
{
    return ((rand() % 50) + 50);
}

void sort(int a[], int n)
{
    int temp;
    for (int x = 0; x < n ; x++)
    {

        for (int i = 0; i < n; i++)
        {
            if (a[i] > a[i+1])
            {
                temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
            }
        }
    }
}

void print5(int a[])
{
    for (int c = 0; c < 5; c++)
    {
        cout << a[c] << endl;
    }
}

You are accessing out of bounds in your sort function.

Also,
1
2
void print(int a[], int size);
print(array, 5); //prints the first 5 
Last edited on
@ne555

nah it works fine, only problem is that random() returns 40 at first no matter what
try this
line 13 const int ArraySize = 30;
and the first number will be always 30 check your code have fun ;)
I don't get 40, I get garbage which is consistent with what ne555 said. Just for the heck of it, try changing the conditions in your for loops to x < n - 1 and i < n - 1.
Last edited on
When I try to compile this, I get an error at line 7:
/.../Test/main.cpp||In function ‘int random()’:|
/.../Test/main.cpp|26|error: new declaration ‘int random()’|
/usr/include/stdlib.h|327|error: ambiguates old declaration ‘long int random()’|
||=== Build finished: 2 errors, 0 warnings ===|



There is already a long int random(); function in some versions of cstdlib. I changed all references from random() to random50() and now it at least compiles for me, however instead of 40 for my first number, I get 0. I'll keep looking.
Got it:
Replace line 37:
for (int i = 0; i < n; i++)
with:
for (int i = 0; i+1 < n; i++)

On your last iteration, of this loop i is 39. Then you perform operations on a[i+1] which is a[40] which is not part of the array. This is a value which shouldn't be accessed. It could cause a seg fault. In my compiler it happened to be 0, in your compiler it happened to be 40. Tomorrow when you boot up, you may find that it is something else.
Last edited on
> There is already a long int random(); function in some versions of cstdlib
Non-standard (i.e. non-ISO C) functions.
http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html


> I changed all references from random() to random50()
¿ever heard of namespaces?
You are trying "Bubble Sort" the wrong way. That is why when sorting it always gives the first element of array the value of ArraySize...

Try this one and it will work....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void sort(int a[], int n)
{
    int temp;
    for (int x = 1; x <= n ; x++)
    {

        for (int i = 0; i < n-x; i++)
        {
            if (a[i] > a[i+1])
            {
                temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
            }
        }
    }
}
@akshayoh

thanks! it works now
:3
Topic archived. No new replies allowed.