Generate two random number and multiply

J

jjmillertime

I'm new so i apologize if this is in the wrong spot. I'm also new to
programming in C and i've been searching for quite a while on how to
create a program using C that will generate two random numbers,
multiply them, and ask you for the result. It also needs to have four
responses for both right and wrong answers and should print them
randomly as well. The program should use at least 2 functions. Any
help would be greatly appreciated.

Thanks
 
S

santosh

I'm new so i apologize if this is in the wrong spot. I'm also new to
programming in C and i've been searching for quite a while on how to
create a program using C that will generate two random numbers,
multiply them, and ask you for the result. It also needs to have four
responses for both right and wrong answers and should print them
randomly as well. The program should use at least 2 functions. Any
help would be greatly appreciated.

Thanks

This seems like a homework question. Have you atleast made an attempt.
Attempt a try at the problem and post the code here, however bad it
is.

Hint: To generate pseudo-random integer values, use the rand function.
Before using it, it's recommended to invoke the srand function to
"seed" the pseudo-random number generator. To learn how to use rand
and srand, try looking at your implementation's documentation for the
Standard library. If it's a Unix system, try typing 'man 3 rand' and
'man 3 srand' at the command prompt.

To select a string, from a collection randomly, you can use the same
rand function to get a random number, trim it down to be within the
bounds of your array of strings and use it to index into the array and
extract a random string.

Well, try your hand at it.
 
D

Daniel Rudy

At about the time of 3/5/2007 7:23 AM, (e-mail address removed) stated
the following:
I'm new so i apologize if this is in the wrong spot. I'm also new to
programming in C and i've been searching for quite a while on how to
create a program using C that will generate two random numbers,
multiply them, and ask you for the result. It also needs to have four
responses for both right and wrong answers and should print them
randomly as well. The program should use at least 2 functions. Any
help would be greatly appreciated.

Thanks

Homework?

Go take a look and rand(3) and srand(3). It's standard C.

http://www.acm.uiuc.edu/webmonkeys/book/c_guide/

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
 
M

Malcolm McLean

I'm new so i apologize if this is in the wrong spot. I'm also new to
programming in C and i've been searching for quite a while on how to
create a program using C that will generate two random numbers,
multiply them, and ask you for the result. It also needs to have four
responses for both right and wrong answers and should print them
randomly as well. The program should use at least 2 functions. Any
help would be greatly appreciated.
rand() will generate a random number between 0 and RAND_MAX.
RAND_MAX is usually 32767, which is too high for arithmetic drill.

So get a random number in the range 1 = 100 by taking modulus 100. There are
more accurate ways of doing this, but this should be fine now.

Unfortunately you program will generate exactly the same numbers on each
run. So call srand() with the time from time() to seed the random number
generator.

Having got your random numbers, print them out with a message asking the
user to multiply. Then call scanf() to input the result. Don't forget to
check the return value of scanf() to make sure the user actually entered a
number.

Now you need to check whether the number is correct. If it is, call the
random number generator again and print out a random "well done" message -
you can use a switch - if not, print out a "wrong answer message".
 
J

jjmillertime

rand() will generate arandomnumber between 0 and RAND_MAX.
RAND_MAX is usually 32767, which is too high for arithmetic drill.

So get arandomnumber in the range 1 = 100 by taking modulus 100. There are
more accurate ways of doing this, but this should be fine now.

Unfortunately you program will generate exactly the same numbers on each
run. So call srand() with the time from time() to seed therandomnumber
generator.

Having got yourrandomnumbers, print them out with a message asking the
user tomultiply. Then call scanf() to input the result. Don't forget to
check the return value of scanf() to make sure the user actually entered a
number.

Now you need to check whether the number is correct. If it is, call therandomnumber generator again and print out arandom"well done" message -
you can use a switch - if not, print out a "wrong answer message".
this is what i have so far, don't laugh, i know it's horrible, but i
wanted to know if i'm going in the right direction... i think i'm lost
and confused


#include <stdlib.h>
#include <math.h>
#include <time.h>
#define num_val 1
int randnum (int k);
int product (void);
#define num1 (randnum)*1
#define num2 (randnum)+1

int main (void)
{
int num1= (randnum),
num2= ((randnum) +2);
int value, right, wrong;

srand (unsigned) time ();

printf ("Type -1 to exit!/n");
printf ("What is the product of %d and %d:/t", num1, num2);
scanf ("%d", &value);
if (value==-1)
printf ("Thanks for Playing, Goodbye!");
else (value == product)
printf ("Correct!");
else if (value!=product)
printf ("Wrong!");
}
return (0);
}
int randNum (int k)
{
int ranNum;
ranNum = 1 + (int) ( (float) k * rand() / ( RAND_MAX % 100 ) );
return (ranNum);
}
int product (int j)
{
int prod;
prod = num1 * num2;
return (prod);
}
 
J

jjmillertime

rand() willgeneratearandomnumber between 0 and RAND_MAX.
RAND_MAX is usually 32767, which is too high for arithmetic drill.

So get arandomnumber in the range 1 = 100 by taking modulus 100. There are
more accurate ways of doing this, but this should be fine now.

Unfortunately you program willgenerateexactly the same numbers on each
run. So call srand() with the time from time() to seed therandomnumber
generator.

Having got yourrandomnumbers, print them out with a message asking the
user to multiply. Then call scanf() to input the result. Don't forget to
check the return value of scanf() to make sure the user actually entered a
number.

Now you need to check whether the number is correct. If it is, call therandomnumber generator again and print out arandom"well done" message -
you can use a switch - if not, print out a "wrong answer message".

ok this is what i have so far, don't laugh, i know it's probably
horrible, but if anyone could help with finishing it i would really
appreciate it

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define num_val 1
int randnum (int k);
int product (int j);
#define num1 ((randnum)*1)
#define num2 ((randnum)+1)

int main (void)
{int value;

srand (unsigned) time ();

printf ("Type -1 to exit!/n");
printf ("What is the product of %d and %d:/t", num1, num2);
scanf ("%d", &value);
if (value==-1)
printf ("Thanks for Playing, Goodbye!");
else (value == product)
printf ("Correct!");
else if (value!=product)
printf ("Wrong!");
}
return (0);
}
int randNum (int k)
{
int ranNum;
ranNum = 1 + (int) ( (float) k * rand() / ( RAND_MAX % 100 ) );
return (ranNum);
}
int product (int j)
{
int prod;
prod = num1 * num2;
return (prod);
}
 
S

santosh

ok this is what i have so far, don't laugh, i know it's probably
horrible, but if anyone could help with finishing it i would really
appreciate it

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define num_val 1
int randnum (int k);
int product (int j);
#define num1 ((randnum)*1)
#define num2 ((randnum)+1)

Wrong invocation of function randnum. It expects an int parameter,
according to your prototype above, but here you invoke it not only
without a parameter, but also without the parenthesis. A function call
must always have the accompanying parenthesis, even if there are no
arguments.
int main (void)
{int value;

srand (unsigned) time ();

srand((unsigned)time(NULL));
printf ("Type -1 to exit!/n");

Newline is '\n', not '/n'. In fact all C escape sequences start with a
'\', like '\t' for tab, '\a' for audible alert etc.
printf ("What is the product of %d and %d:/t", num1, num2);

Likewise '\t' for tab.
scanf ("%d", &value);

scanf is prone to failure. You should always check it's return value
before proceeding.
if (value==-1)
printf ("Thanks for Playing, Goodbye!");
else (value == product)

You mean an else if here, and an else below. Also, again you have to
supply the parenthesis around a function call. product also expects an
argument of type int.
printf ("Correct!");
else if (value!=product)
printf ("Wrong!");

Also always include a newline character at the end of printf strings
or alternatively call fflush(stdout) immediately afterwards, otherwise
output may appear delayed due to buffering.
}
return (0);

Parenthesis is redundant, but harmless.
}
int randNum (int k)

This function is never called in your code. C is case sensitive.
You've named this function randNum, but you invoke it as randnum,
(which is itself incorrect), above. It'll lead to a linkage error,
during compilation.
{
int ranNum;
ranNum = 1 + (int) ( (float) k * rand() / ( RAND_MAX % 100 ) );

Huh? Why so much contortion. Just:

return rand() % 100;

should do.
return (ranNum);
}

int product (int j)
{
int prod;
prod = num1 * num2;
return (prod);
}

Why're you invoking num1 and num2 again? You should use the previous
values. Each call to rand will return different values.
 
S

santosh

On Mar 5, 1:46 pm, "Malcolm McLean" <[email protected]> wrote:

[ ... ][ ... ]

Here's a short programme that may be similar to what you want:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
int n1, n2;
long prd, guess;

srand((unsigned int)time(NULL));

n1 = 1 + (int)(100.0 * (rand() / (RAND_MAX + 1.0)));
n2 = 1 + (int)(100.0 * (rand() / (RAND_MAX + 1.0)));
prd = n1 * n2;

printf("What is the product of %d and %d\?\n"
"[Enter -1 to exit]\n", n1, n2);

if(scanf("%ld", &guess) != 1) {
puts("Incorrect input.");
return EXIT_FAILURE;
}
else if(guess == -1) {
puts("bye.");
}
else if(guess == prd) {
puts("Correct!");
}
else {
puts("Wrong guess.");
}
return 0;
}
 
J

jjmillertime

Wrong invocation of function randnum. It expects an int parameter,
according to your prototype above, but here you invoke it not only
without a parameter, but also without the parenthesis. A function call
must always have the accompanying parenthesis, even if there are no
arguments.





Newline is '\n', not '/n'. In fact allCescape sequences start with a
'\', like '\t' for tab, '\a' for audible alert etc.


Likewise '\t' for tab.


scanf is prone to failure. You should always check it's return value
before proceeding.


You mean an else if here, and an else below. Also, again you have to
supply the parenthesis around a function call. product also expects an
argument of type int.


Also always include a newline character at the end of printf strings
or alternatively call fflush(stdout) immediately afterwards, otherwise
output may appear delayed due to buffering.


Parenthesis is redundant, but harmless.


This function is never called in your code.Cis case sensitive.
You've named this function randNum, but you invoke it as randnum,
(which is itself incorrect), above. It'll lead to a linkage error,
during compilation.


Huh? Why so much contortion. Just:

return rand() % 100;

should do.



Why're you invoking num1 and num2 again? You should use the previous
values. Each call to rand will return different values.
ok, i'm not sure if i fixed everything you were talking about, but i
hope i did, thank you so much for your help, this is for homework and
my teacher basically just tells us different parts of things but never
really explains how to put anything together and its really irritating
because i'm usually fairly computer literate


int main (void)
{int value,product;
//set and seed rand
srand ((unsigned) time (NULL));
printf ("Type -1 to exit!\n");
printf ("What is the product of %d and %d:\t", num1, num2);
scanf ("%d", &value);
product = ((num1) * (num2))
//if value is -1, end program
if (value!=-1)
printf ("OK, Goodbuy!\n");
else if ((value) == (product))
printf ("Correct!\n");
else ((value)!=(product))
printf ("Wrong!\n");
}
return (0);
}
//first function
int randnumber (int k)
{
int random;
return rand() % 100;
return (random);
}
 
J

jjmillertime

wow, thats so much cleaner, could you help me put another function
into it so it will have 4 correct answers (eg "Thats right!") and 4
wrong ("sorry try again") i know you probably have better things to
do, but i have to hand it in in 30 minutes, and obviously i don't know
what i'm doing
 
S

santosh

[ ... ]

[ ... ]

[ ... ]
ok, i'm not sure if i fixed everything you were talking about, but i
hope i did, thank you so much for your help, this is for homework and
my teacher basically just tells us different parts of things but never
really explains how to put anything together and its really irritating
because i'm usually fairly computer literate


int main (void)
{int value,product;
//set and seed rand
srand ((unsigned) time (NULL));
printf ("Type -1 to exit!\n");
printf ("What is the product of %d and %d:\t", num1, num2);

Since you're not terminating your printf string with a newline, call
fflush(stdout) after it.
Additionally, I hope you've fixed your num1 and num2 macros.
scanf ("%d", &value);
product = ((num1) * (num2))

Here's a mistake. Each invocation of the rand function returns
different values. The two invocations above will not return the same
values as the invocations within the printf statement earlier. So, in
effect, you're computing a product to two different integers than what
you presented to the user earlier.
//if value is -1, end program
if (value!=-1)

if(value == -1)
printf ("OK, Goodbuy!\n");
else if ((value) == (product))
printf ("Correct!\n");
else ((value)!=(product))
printf ("Wrong!\n");
}
return (0);
}
//first function
int randnumber (int k)
{
int random;

Don't use the identifier random. There're certain popular non-standard
functions that use that name.
return rand() % 100;

ITYM random = rand() % 100;

But to get even better randomness do:

random = 1 + (int)(100.0 * (rand() / (RAND_MAX + 1.0)));

This uses the higher order bits of the value that rand returns, which
are more random than the lower order bits.
return (random);

Again the parenthesis aren't necessary.
 
S

santosh

wow, thats so much cleaner, could you help me put another function
into it so it will have 4 correct answers (eg "Thats right!") and 4
wrong ("sorry try again") i know you probably have better things to
do, but i have to hand it in in 30 minutes, and obviously i don't know
what i'm doing

Try this:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define CORRECT 1
#define INCORRECT 0

void print_rand_msg(const int msg_type);

int main(void) {
int n1, n2;
long prd, guess;

srand((unsigned int)time(NULL));

n1 = 1 + (int)(100.0 * (rand() / (RAND_MAX + 1.0)));
n2 = 1 + (int)(100.0 * (rand() / (RAND_MAX + 1.0)));
prd = n1 * n2;

printf("What is the product of %d and %d\?\n"
"[Enter -1 to exit]\n", n1, n2);

if(scanf("%ld", &guess) != 1) {
puts("Incorrect input.");
return EXIT_FAILURE;
}
else if(guess == -1) {
puts("bye.");
}
else if(guess == prd) {
print_rand_msg(CORRECT);
}
else {
print_rand_msg(INCORRECT);
}
return 0;
}

void print_rand_msg(const int type) {
char *correct_msgs[4] = { "Correct", "You're right!", "Perfect!",
"You've hit the nail on the head!" };
char *incorrect_msgs[4] = { "Wrong.", "Bad luck, try again",
"Waaay of target.", "Not even close." };
const int rndidx = 1 + (int)(4.0 * (rand() / (RAND_MAX + 1.0)));

switch (type) {
case CORRECT:
puts(correct_msgs[rndidx]);
break;
case INCORRECT:
puts(incorrect_msgs[rndidx]);
break;
default:
puts("Wrong argument.");
exit(EXIT_FAILURE);
}
return;
}
 
J

jjmillertime

wow, thats so much cleaner, could you help me put another function
into it so it will have 4 correct answers (eg "Thats right!") and 4
wrong ("sorry try again") i know you probably have better things to
do, but i have to hand it in in 30 minutes, and obviously i don't know
what i'm doing

Try this:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define CORRECT 1
#define INCORRECT 0

void print_rand_msg(const int msg_type);

int main(void) {
int n1, n2;
long prd, guess;

srand((unsigned int)time(NULL));

n1 = 1 + (int)(100.0 * (rand() / (RAND_MAX + 1.0)));
n2 = 1 + (int)(100.0 * (rand() / (RAND_MAX + 1.0)));
prd = n1 * n2;

printf("What is the product of %d and %d\?\n"
"[Enter -1 to exit]\n", n1, n2);

if(scanf("%ld", &guess) != 1) {
puts("Incorrect input.");
return EXIT_FAILURE;
}
else if(guess == -1) {
puts("bye.");
}
else if(guess == prd) {
print_rand_msg(CORRECT);
}
else {
print_rand_msg(INCORRECT);
}
return 0;

}

void print_rand_msg(const int type) {
char *correct_msgs[4] = { "Correct", "You're right!", "Perfect!",
"You've hit the nail on the head!" };
char *incorrect_msgs[4] = { "Wrong.", "Bad luck, try again",
"Waaay of target.", "Not even close." };
const int rndidx = 1 + (int)(4.0 * (rand() / (RAND_MAX + 1.0)));

switch (type) {
case CORRECT:
puts(correct_msgs[rndidx]);
break;
case INCORRECT:
puts(incorrect_msgs[rndidx]);
break;
default:
puts("Wrong argument.");
exit(EXIT_FAILURE);
}
return;

}

thanks so much for your help, i really appreciate it
 
M

Mark McIntyre

On 5 Mar 2007 22:05:31 -0800, in comp.lang.c ,
(snip example)

thanks so much for your help, i really appreciate it

Make sure you spend the time to understand it, so that when your
teacher asks, you will be able to answer.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
C

CBFalconer

pete said:
Daniel Rudy wrote:
.... snip ...

What is rand(3) supposed to mean?

It means, on unixy systems, enter "man N topic". Here it would
translate to "man 3 rand".
 
N

Nelu

CBFalconer said:
It means, on unixy systems, enter "man N topic". Here it would
translate to "man 3 rand".

I think it was man -M 3 rand on Solaris 2.7 but the -M is no
longer necessary on Linux systems.
 
K

Keith Thompson

Nelu said:
I think it was man -M 3 rand on Solaris 2.7 but the -M is no
longer necessary on Linux systems.

<OT>
Not quite. "man man" for details. (If you don't have the "man"
command, then you don't need to know how it works.)
</OT>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top