Help w/ Homework!

So we are learning how to build different types of functions... Only problem is our teacher is continuing to assign homework without teaching us in class!!(School has been out for past 3 days due to snow)
While I'm not complaining of a little snow, I am confused by how to build this program. Can anyone take a look and point me in the right direction??

DIRECTIONS

You will write a program that generates random numbers between a minimum and maximum using a seed provided by the user.
For this part, you must:

1.
You will need to ask the user to input three values that will form your seed by using the following formula;
seed = s1 + s2 * s3.

2.
Use the calculated seed to seed your random number generator once and only once inside of int main
prior to generating any random number. (Do not
reseed your random number generator for any phase of this lab)

3.
Write a function called getRandomNumber that takes two integers (min and max). It will generate a random number, narrow it between min and max [inclusively] , and return the result.
This function MUST have a prototype above int main() and a definition
below int main().

4.
Ask the user for a minimum and maximum. If the user makes the maximum lower than the minimum, you must swap them.

5. Finally, output the random number your function calculated.

6.
NOTE: There are different random number generators per operating system, you may NOT get the same values we do in the sample interaction.
Here is the sample interaction.

Make your prompts and outputs look like the sample below (user inputs are in blue):

Enter a minimum and maximum: -5 20
Enter random number seeds s1, s2, and s3: 65 72 17
Random number: 8

***Due to differences in random number generators, your program may not get
the value 8, however ensure that
the number you do get is between the
minimum and maximum***
Function tutorial: http://www.cplusplus.com/doc/tutorial/functions/

I would normally recommend using the <random> header for generating random numbers, but it appears your professor wants you use use rand() and srand() (he probably doesn't know they are deprecated). You can find info about them here:
http://www.cplusplus.com/reference/cstdlib/rand/
I appreciate your help LB, however I have actually referenced that tutorial before making this post. I have very limited knowledge on this subject, and was hoping someone could help me build the program from scratch. Our prof. said it was a program that (if you understand the concept and new material) would be very easy to build. However I am having trouble grasping this concept so that is why I have come to the forum.

Any help is appreciated!!
Last edited on
What have you written so far?

Start by writing whatever you can, even if you're not confident it is correct. When you get stuck, post what you have and ask a specific question.
1
2
3
4
5
6
#include <iostream>
using namespace std;

int getRandomNumber (&int, &int);

int main{


Here is my work thus far. I know it is not much, I am just wondering if 1. Where the "seed" comes into play(very confused by this) and 2. How to narrow it down to a min and max. I have never seen the rand/srand in action and just cannot visual what to do within the context of my problem.
Last edited on
Focus on the user-input aspect for now - get that done first and then start thinking about the random stuff.
Is my work correct so far? And so you would suggest building the getRandomNumber function within the int{main next?

so something like

1
2
3
4
5
6
7
int min, max

getRandomNumber (seed); 
cout << "Enter a min and max\n";
cin >> min, max;
cout << "Enter random number seeds s1, s2, and s3\n"; 
cin >> (is this where the seed aspect comes into play?)
TackyTechyy wrote:
(is this where the seed aspect comes into play?)
No, not yet.

Post full programs as if they were going to be compiled - it helps me help you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <stdlib.h>
using namespace std;

int getRandomNumber (&int, &int);

int main{

int min, max

getRandomNumber (seed); 
cout << "Enter a min and max\n";
cin >> min, max;
cout << "Enter random number seeds s1, s2, and s3\n"; 
cin >> 


Here is the big picture thus far. I am not sure what to do next, in terms of my cin value and moving forward after putting one in.
Last edited on
Does anyone think they can help me be able to build and run this program correctly? Not trying to be pushy, but this is an assignment of mine due in 3 hours. I am not trying to get someone else to do my homework but instead help me complete it. If anyone is able to understand this problem please help me!
What are the ampersands for on line 5? This is not valid syntax.

You are missing the argument list on line 7.

The opening curly brace on line 7 does no have a matching closing curly brace.

Line 11 should not exist yet.
The problem is straightforward. You must learn how to write code that compiles. All the samples you've provided are incomplete and do not compile. This segment of code compiles. It doesn't do anything, but it certainly compiles. As you add each feature, sanity check yourself and compile. Don't try to write the entire program in one go and compile at the end.
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
#include <iostream>
#include <cstdlib>  //for srand, rand

//write the function prototype for getRandomNumber here

int main()
{
    //ASK USER FOR MAX AND MIN
    // create variables to hold max and min
    // print a prompt to the user (cout)
    // read max and min from user (cin)

    //ASK USER FOR INITIAL SEED VALUES
    // create variables to hold seed, s1, s2, and s3
    // print a prompt to the user
    // read s1, s2, and s3 from the user
    // use formula provided to set value of seed

    //SEED THE RANDOM NUMBER GENERATOR
    // call srand and pass it the calculated seed value

    //PRINT RANDOM NUMBER IN RANGE
    // invoke your getRandomNumber function
    // print what it returns
}

//write function definition for getRandomNumber here 
Last edited on
Here is what I have now. This is still not all of the work but I have done most of what I can without additional help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int getRandomNumber (int, int);
int main()
{
	int x, y;
	string seed, s1, s2, s3;
	
    cout << "Enter a min and a max:\n"; 
    cin >> x, y; 
    cout << "Enter random number seeds s1, s2, and s3:\n";
    seed =  (s1 + s2 * s3); 

    
   srand(seed(0));

    
    getRandomNumber (seed);
    cout << "Return\n";
}


Compiled with /EHsc /nologo /W4 /c
main.cpp
main.cpp(14): error C2676: binary '*': 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
main.cpp(17): error C2064: term does not evaluate to a function taking 1 arguments
main.cpp(20): error C2660: 'getRandomNumber': function does not take 1 arguments
I'm in the same class. I'm actually just lost as to step 3: narrow between min and max. I'm not sure what it is asking us to do within the function in regards to this.

Here's what I have so far anyway.
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
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int getRandomNumber (int min, int max);

int main()
{
	int min, max, seed, s1, s2, s3, random;
	cout << "Enter a minimum and maximum: ";
	cin >> min >> max;
		if (min > max)
		{	
			std::swap(min, max);
		}
	cout << min << max << endl;	
	cout << "Enter random number seeds s1, s2, and s3: ";
	cin >> s1 >> s2 >> s3;
		seed = (s1 + s2 * s3);

	random = getRandomNumber(min, max);
	
	cout << "Random number: " << random;
	return 0;
}

int getRandomNumber(int min, int max)
{
	srand(seed);
}


Pretty sure there's an issue in that I don't believe I can use seed from above within the function, but that's what I'm getting from the instructions. Anyway, some clarity on step 3 would be a big help.

The couts that I have for min/max are just for me to make sure that the swap function is working properly.
Last edited on
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
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int getRandomNumber (int min, int max);

int main()
{
	int min, max, seed, s1, s2, s3, random;
	cout << "Enter a minimum and maximum: ";
	cin >> min >> max;
		if (min > max)
		{	
			std::swap(min, max);
		}
	cout << min << max << endl;	
	cout << "Enter random number seeds s1, s2, and s3: ";
	cin >> s1 >> s2 >> s3;
		seed = (s1 + s2 * s3);

	random = getRandomNumber(min, max);
	
	cout << "Random number: " << random;
	srand(seed);
	return 0;
}

int getRandomNumber(int min, int max)
{
	rand();
}
Line 23 calls a function that uses the random number generator (srand). Line 26 seeds the random number generator. You want to seed the random number generator before you use it. Move line 26 (srand) up to line 22.

To get a random number in a certain range, first determine how many numbers fall in the span. Say, min = 5 and max = 7. The span of this range is 3 (5, 6, and 7). In general, the span of the range if you include both the max and the min is ( span = max - min + 1 ).

Once you know how many numbers you'll need, generate a random number and use the modulo operator (%) to get it within a certain range. For this example:
min = 5
max = 7
span = max - min + 1 = 3
rand() % span = rand() % 3, which will yield 0, 1, or 2.
Add min to this result and you will end up with a number that is 5, 6, or 7. (the span that you want)

In summary:
span = max - min + 1
randomNumberInSpan = (rand() % span) + min
Last edited on
Topic archived. No new replies allowed.