PDA

View Full Version : Store a rand() into a variable?


kaymeezy
11-18-2009, 10:25 PM
I have to write a program that prints out a line like

"Write two single digits that use (randomoperator) to make (randomnumber)"

Where the random operater will be +, -, x or / and the random number is well a random number. How can I ensure that the random number will only give numbers that are products of two single digits??

Fumigator
11-18-2009, 11:15 PM
Thinking in algebraic terms, what are you solving for? Will your function take two random numbers, apply a random operator, to get a number? Sure you realize the result of the equation won't be random; not every piece can be random.

kaymeezy
11-18-2009, 11:42 PM
I'm supposed to write a program that does this:

"Write a C program that tests a student’s knowledge of simple math using single digits, and the operations +, -, x and /. Your program will ask a question, such as “Use two single digits and the x operation to make 36”, and the student is expected to answer: “4x9” (or “9x4”, or “6x6”). "

The "36" is to be a random number. I'm also told I need to use a function such as:

int randomBetween(int low, int high) – returns a random integer between low and high (inclusive). Hint: consider using rand()%(high – low + 1). For example, randomBetween(0,9) could have the value 3 (or 2, or 6… because it is random!).

My question is how can I ensure that the random number(s) are only numbers that are products of two single digits?

Spookster
11-19-2009, 02:51 AM
Based on the assignment description I think your teacher is expecting you to figure out how to solve that. So it seems you are asking us how to do that. To that I would say show us what you have come up with so far and maybe we can guide you in the right direction. For any programming assignment come up with your algorithm first. Don't worry about code. Just write out the steps in English that it would take to get from starting point to a result. See if it makes sense and then start working on using code to test your algorithm.

Start with things that you do know. What is the largest possible number that can be a product of 2 single digits? What's the smallest? This tells you the range you have to work within. Now within this range a random number is selected how do you determine all the possible combinations of single digits that can produce that number as a product? What is another name for these single digits? Hint: Factors. Another Hint: In math how do you find the factors of a number?

testware
11-21-2009, 04:40 AM
Hint:

Use randomize() before you use the rand() function to get true (Well, for all practical purposes) random numbers.

And well, another hint:

If you need a random number made up of two single digit numbers acted upon by a binary operator, why do you have to worry about what you get as the result? You have your binary operators. You don't have a shortage of single digit numbers do you? Think for a couple.