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

Pages: 123
@Austinomical
So you are concerning about that min() - max(). You really should have told us much sooner.

Anyway, glad it helped :)
Also, I am trying to add another separate function that takes 3 parameters of floats and returns the biggest number. I know I want to use VOID. Any thoughts? So far I have this:
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
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;

int randomNumber(int val1, int val2); // Function1 prototype
void biggestNumber(float num1, float num2, float num3); // Function2 prototype

int main ()
{
                           // not sure what goes here for Function2
    srand((int)time(NULL));
    cout << randomNumber(11, 23) << '\n';

    return 0;
}

int randomNumber(int val1, int val2) // Function1 definition
void biggestNumber(int num1, int num2, int num3) // Function2 definition
{
    return rand()%(val2 - val1 + 1) + val1;

    if (num1 > num2 && num1 > num3)
        return num1;

    if (num2 > num1 && num2 > num3)
        return num2;

    if (num3 > num1 && num3 > num2)
        return num3;
}
Last edited on
==>
1
2
3
4
5
6
7
8
9
10
float biggestNumber(float num1, float num2, float num3)
{
    if (num1 > num2 && num1 > num3)
        return num1;
    if (num2 > num1 && num2 > num3)
        return num2;
    if (num3 > num1 && num3 > num2)
        return num3;
        return num1;
}
> I am trying to add another separate function that takes 3 parameters of floats
So how will you use this function in your code?
This function prototype is wrong.
void biggestNumber(float num1, float num2, float num3);

Should be :
float biggestNumber(float num1, float num2, float num3);
> I know I want to use VOID. Any thoughts?
Did your instructor tell you that?
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
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;


float biggestNumber(float , float , float ); // Function2 prototype

int main ()
{
    cout << biggestNumber(1,2,3) << '\n';
    
     cout << biggestNumber(1.2,2.1,-3.7) << '\n';
    return 0;
}

float biggestNumber(float num1, float num2, float num3) // Function2 definition
{
    float max = 0;
    
    if (num1 > max)
        max = num1;

    if (num2 > max)
        max = num2;

    if (num3 > max)
        max = num3;
        
    return max;
}


A couple of things.

It's important the function declaration, function prototype and function definition have compatible types for variables including return value.

The cascade of if statements whether they are complicated like this previous one or even the simple way I have shown are only good for a couple of numbers before it gets out of hand. So arrays, which you may not have come across yet, are something to look forward to.
@Austinomical
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
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;

float biggestNumber(float , float , float ); // Function2 prototype

int main ()
{
    cout << biggestNumber(1,2,3) << '\n';
    
    cout << biggestNumber(1.2,2.1,-3.7) << '\n';
    
    float largest = biggestNumber(-1.2,-2.1,-3.7);
    cout << "Largest " << largest << " be careful.\n";
    
    return 0;
}

float biggestNumber(float num1, float num2, float num3) // Function2 definition
{
    float max = 0;// this number has to be chosen very carefully
    
    if (num1 > max)
        max = num1;

    if (num2 > max)
        max = num2;

    if (num3 > max)
        max = num3;
        
    return max;
}
Last edited on
closed account (48T7M4Gy)
I insist.
@kemort
> I insist.
Insist on what?
@kemort
There is one big flaw in your solution.

float max = 0;

What if the three numbers num1, num2, num3 were all negative?
@kemort
At least the OP's solution is much better than yours.
closed account (48T7M4Gy)
Could be
closed account (48T7M4Gy)
Looks like I'm one step ahead. "Always be careful." Sun Tzu
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
#include <iostream>

float biggestNumber(float , float , float ); // Function2 prototype

int main ()
{
    std::cout << biggestNumber(1,2,3) << '\n';
    
    std::cout << biggestNumber(1.2,2.1,-3.7) << '\n';
    
    float largest = biggestNumber(-1.2,-2.1,-3.7);
    std::cout << "Largest " << largest << " Always be careful - Sun Tzu.\n";
    
    return 0;
}

float biggestNumber(float num1, float num2, float num3) // Function2 definition
{
    float max = num1;
    
    if (num2 > max)
        max = num2;

    if (num3 > max)
        max = num3;
        
    return max;
}
okay thank!
@ Kemort. I have tried to combine the two functions into one program and I am getting some errors. Know why?

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

#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;


}



Also, I am trying to add another separate function that takes 3 parameters of floats and returns the biggest number. I know I want to use VOID.

A return type of void means "this function returns nothing" so that preference is at odds with the description of what you want to do. One approach to doing this is to define a function that returns the larger of 2 values, and use that to implement the function that returns the larger of 3 values. This approach is more scalable than those presented so far. In fact, given an array of arbitrary length, using only the function that returns the larger of 2 values, we can return the largest value in the array. It also illustrates the divide-and-conquer approach that is common to many algorithmic solutions.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

// max of 2
float maxOf(float a, float b) { return a > b ? a : b; }

// max of 3 - uses max of 2.
float maxOf(float a, float b, float c) { return maxOf(maxOf(a, b), c); }

int main()
{
    std::cout << "Largest is " << maxOf(1.2, 2.1, 3.7) << '\n';
}
I mean, I have 2 separate functions that I want to combine...


Function 1:
Write the function necessary to create and RETURN a random number between 11 and 23, inclusive.

Show the returned value in the main().

Returning a value, and showing that value from the main, is worth half the points.
The other half of the points is making the random number.


Function 2:
Write a VOID function which takes THREE float PARAMETERS : num1, num2 and num3.

The function displays the largest number.

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;


}
Last edited on
Pages: 123