How would I use a function to return a random number?

Pages: 123
I mean, I have 2 separate functions that I want to combine...

So you've said. You also said you're getting errors, but you declined to provide them.

So, when you get errors you look at the first one the compiler gives you. You correct it. You try again to recompile. If more errors are generated, you look at the first error the compiler gives you. You correct it. Repeat until no errors are generated.

If you have questions about a specific error as it relates to your code, ask a question including the code in question and the exact text of the error message. "I have errors help~!" doesn't help anyone help you.
Okay, I am getting val2 and val1 was not declared. Also, expected initializer before 'float'. I have no idea how to fix these. Any ideas?
You should post your full code here so we can know what is going on.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main ()
{
    // Function 1 random number from 11 to 23 inclusive
    srand((int)time(NULL));
    cout << randomNumber(11, 23) << '\n';
    // Function 2 finds the biggest number of 3 values
    float largest = biggestNumber(1.2,2.1,3.7);
    cout << "Largest is " << largest << endl;
    // Function 1 return

float val2 = 0; // <--
float val1 = 0; // <--

    return rand()%(val2 - val1 + 1) + val1;


    return 0;
}
closed account (48T7M4Gy)
initialiser error is often a missing ; But that error might disappear when you fix the declared error.
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
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;

// Function 1
int randomNumber(int val1, int val2); // Function 1 prototype
// Function 2
float biggestNumber(float num1, float num2 , float num3 ); // Function 2 prototype

int main ()
{
    // Function 1 random number from 11 to 23 inclusive
    srand((int)time(NULL));
    cout << randomNumber(11, 23) << '\n';
    // Function 2 finds the biggest number of 3 values
    float largest = biggestNumber(1.2,2.1,3.7);
    cout << "Largest is " << largest << endl;
    // Function 1 return
    return rand()%(val2 - val1 + 1) + val1;


    return 0;
}

int randomNumber(int val1, int val2) // Function 1 definition
float biggestNumber(float num1, float num2, float num3) // Function 2 definition
{

    // Function 2 return
    float max = 0;
    if (num1 > max)
        max = num1;

    if (num2 > max)
        max = num2;

    if (num3 > max)
        max = num3;

    return max;


}

closed account (48T7M4Gy)
Your line 20 needs to go with the function at line 26 as originally for that function.

Then, in main you call that function the way you were shown originally.
Last edited on
> Write a VOID function which takes THREE float PARAMETERS : num1, num2 and num3.
The function displays the largest number.

That is the question of the assignment.
But unfortunately, it just seems no one has ever noticed that yet.
@Austinomical
Let me ask you again.

If you really want a void BiggestNumber() function, that is. I can show you if you insist.
closed account (48T7M4Gy)
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
#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;

// Function 1
int randomNumber(int, int); // Function 1 prototype

// Function 2
float biggestNumber(float, float , float ); // Function 2 prototype

int main ()
{
    float v1 = 0;
    float v2 = 0;
    float v3 = 0;
    float answer = 0;
    
    srand((int)time(NULL));
    
    v1 = 11;
    v2 = 23;
    answer = randomNumber(v1, v2);
    cout << answer << '\n';
    
    // An alternative for the same
    cout << randomNumber(11, 23) << '\n';
    
    float largest = biggestNumber(1.2,2.1,3.7);
    cout << "Largest is " << largest << endl;
    
    // An alternative for this one
    v1 = 27;
    v2 = 32;
    v3 = 30;
    answer = biggestNumber(v1, v2, v3);
    cout << "Largest is " << answer << endl;
    
    return 0;
}

int randomNumber(int val1, int val2)
{
    return rand()%(val2 - val1 + 1) + val1;
} 

float biggestNumber(float num1, float num2, float num3) // Function 2 definition
{

    // Function 2 return
    float max = num1; // <--
    
    if (num2 > max)
        max = num2;

    if (num3 > max)
        max = num3;

    return max;


}


23
21
Largest is 3.7
Largest is 32
 
Exit code: 0 (normal program termination)
Last edited on
@kemort
You are so convenient. But do you really care for the OP?
Thank you @Kemort. You helped me out a lot!
closed account (48T7M4Gy)
Cheers :)
I wonder if his instructor will give him a 10 for this...
Topic archived. No new replies allowed.
Pages: 123