Hint please!1

What is wrong with this code?? I am supposed to take the min and max in RandRange and the return should be the random no. b/t min and max using (rand() % (1 + max - min) + min).

#include <iostream>
#include <ctime>

using namespace std;

int RandRange(int min, int max);
int main()
{
int min, max;

srand((unsigned int)time(0));

cout << "Enter your min value" << endl;
cin >> min;
cout << "Enter your max value" << endl;
cin >> max;

int RandRange = (rand() % (1 + max - min) + min);
for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 10; i++)
{
cout << RandRange << "\t";

}
cout << endl;
}
I am supposed to take the min and max in RandRange and the return should be the random no.


On line 06, you use the function prototype RandRange, but you never define the function. Instead, you define and declare as a variable on Line 18. I could be wrong,
but according to the instructions, I would expect RandRange as a function with a
return value of the random number. If I am correct, then I recommend you to define
the function int RandRange(int min, int max).

Your code (It was missing the left } so I added)
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>
#include <ctime>

using namespace std;

int RandRange(int min, int max);
int main()
{
    int min, max;

    srand((unsigned int)time(0));

    cout << "Enter your min value" << endl;
    cin >> min;
    cout << "Enter your max value" << endl;
    cin >> max;

    int RandRange = (rand() % (1 + max - min) + min);
    for (int j = 0; j < 10; j++)
    {
	   for (int i = 0; i < 10; i++)
	   {
		  cout << RandRange << "\t";

	   }
	   cout << endl;
    }
}


to something like:
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>

using namespace std;

int RandRange(int min, int max);
int main()
{
    int min, max;

    srand((unsigned int)time(0));

    cout << "Enter your min value" << endl;
    cin >> min;
    cout << "Enter your max value" << endl;
    cin >> max;

    cout << "The random number is " << RandRange(min, max) << endl;

    //int RandRange = (rand() % (1 + max - min) + min);
    //for (int j = 0; j < 10; j++)
    //{
	   //for (int i = 0; i < 10; i++)
	   //{
		  //cout << RandRange << "\t";

	   //}
	   //cout << endl;
    //}
}
int RandRange(int min, int max)
{
    //Define the random function here
    //return the random number
}

Thanks dude I really appreciate it... Thank you.
Topic archived. No new replies allowed.