Problem with if statement?

What have I done wrong with if statement? Or is something else?

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
  #include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;


int randomNUMBER(int vecje,int manjse)
{
    int random = rand() % (vecje - manjse ) + manjse;
}

int main()
{
    int array[10];
    int smallestNUMBER = 20;
    srand(time(NULL));


    for(int i = 0; i < 10; i++)
    {
    array[i] = randomNUMBER(10,1);
    cout << array[i] << endl;

    if ( smallestNUMBER > array[i])
    {
    array[i] = smallestNUMBER;
    }


    }

cout << smallestNUMBER;

}
line 10 - you want to return the value generated by rand?

int random = rand() % (vecje - manjse ) + manjse;

should be

return rand() % (vecje - manjse ) + manjse;

line 27 - did you mean to reverse these? The random number generated is always between 1 and 9, so always less than 20 in smallestNUMBER. You're assigning 20 to each element of the array but I think you meant to update the smallestNUMBER variable so it doesn't output 20 at the end?
for if statement I know that random number wont be bigger than 9, so I used one random big number in this example 20. in if array if smallsetNUMBER is bigger than array[i] then smallestNUMBER should take array[i] .
Works now! AND I REALLY DIDNT KNOW THAT smallestNUMBER = array[i]; AND array[i] = smallestNUMBER; IS DIFFERENT Xd!.


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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;


int randomNUMBER(int vecje,int manjse)
{
    return rand() % (vecje - manjse ) + manjse;
}

int main()
{
    int array[10];
    int smallestNUMBER = 20;
    srand(time(NULL));


    for(int i = 0; i < 10; i++)
    {
    array[i] = randomNUMBER(10,1);
    cout << array[i] << endl;

    if ( smallestNUMBER > array[i])
    {
    smallestNUMBER = array[i];
    }


    }

cout <<  "\n\n"<<  smallestNUMBER;

}
Topic archived. No new replies allowed.