Learn Multiplicatin Program

R

Richard

I created a program which uses the srand function to generate random
numbers. The program is suppose to help an elementary student learn
multipliation and ask a question, "How much is 6 times 7?". The
problem is the random numbers are not showing up when I run the
program. Only two 0s are shown on the screen where the random numbers
should be, however it does appear to generate the random numbers when
I use a printf statement to see what they are. Also the program
should continue until the EOF is entered but it ends when the answer
is correct. Any suggestions would be appreciated.

TIA

Here is the code.

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

int random1(void); //function prototype for 1st random number
int question(int); //function prototype for question
int firstnum; //variable for 1st random number
int secondnum; //variable for 2nd random number

int main()
{
int response = 0; //return value of question
int resp1; //assign value of repsonse to this variable
int firstnum = random();
int secondnum = random();
int answer = firstnum * secondnum; //1st number times 2nd
number

//use a do-while loop to check the answer of the question
//if user enters the correct answer, end program, else keep
asking
//the same question until user get the question correct.
do
{
resp1 = question(response); //assign the value of
repsonse to resp1

if ( resp1 != answer )
printf("No, Please try again.\n\n");

else if ( answer == resp1 )
{
printf("Very good!\n\n");
break;
} //end else

} while ( resp1 != EOF );

return 0;
}

int random1(void) //function definition for 1st random number
{
int int1;

srand( time( NULL ) );

int1 = 1 + ( rand() % 9 );

return int1;
}

int question(int a)
{
printf("How much is %d times %d? (Enter EOF to end) ",
firstnum, secondnum );
scanf("%d", &a );

return a;
}
 
R

Rob van der Leek

I created a program which uses the srand function to generate random
numbers. The program is suppose to help an elementary student learn
multipliation and ask a question, "How much is 6 times 7?". The
problem is the random numbers are not showing up when I run the
program. Only two 0s are shown on the screen where the random numbers
should be, however it does appear to generate the random numbers when
I use a printf statement to see what they are. Also the program
should continue until the EOF is entered but it ends when the answer
is correct. Any suggestions would be appreciated.
int main()
{
int response = 0; //return value of question
int resp1; //assign value of repsonse to this variable
int firstnum = random();
int secondnum = random();

You've made a typo here, you need to call your own random function here
which is called 'random1'.

Regards,
 
D

Drew MacDonald

Richard said:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int random1(void); //function prototype for 1st random number
int question(int); //function prototype for question
int firstnum; //variable for 1st random number
int secondnum; //variable for 2nd random number

GLOBAL declaration/scope
int main()
{
int response = 0; //return value of question
int resp1; //assign value of repsonse to this variable
int firstnum = random();
int secondnum = random();

LOCAL declaration/scope
int answer = firstnum * secondnum;

This is a variable scope issue. If two variables have the same
name but one is decalred globally and the other locally, the local
variable is used. In your case, the global firstnum and secondnum
(declared OUTSIDE main()) do not get initialized by random1() but
the local firstnum and secondnum (declared INSIDE main()).

If you have K&R2, re-read the section on variable scope.

Drew
 
K

Keith Thompson

int random1(void) //function definition for 1st random number
{
int int1;
srand( time( NULL ) );
int1 = 1 + ( rand() % 9 );
return int1;
}
[...]

You're calling srand() once for each call to rand(). Don't do that.
Call srand() once at the beginning of your program to initialize the
generator, then call rand() as many times as you need to.

(I think the C FAQ covers this, but I can't get to it at the moment;
try <http://www.eskimo.com/~scs/C-faq/top.html>.)
 
E

Emmanuel Delahaye

In said:
I created a program which uses the srand function to generate random
numbers. The program is suppose to help an elementary student learn
multipliation and ask a question, "How much is 6 times 7?". The
problem is the random numbers are not showing up when I run the
program. Only two 0s are shown on the screen where the random numbers
should be, however it does appear to generate the random numbers when
I use a printf statement to see what they are. Also the program
should continue until the EOF is entered but it ends when the answer
is correct. Any suggestions would be appreciated.

Try this. Feel free to ask for details.

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

#if 0
/* unix-like */
#define S_QUIT " (or Ctrl-D to quit)"
#else
/* wintel */
#define S_QUIT " (or Ctrl-Z to quit)"
#endif

/* random number (1 to 9) */
int random1to9 (void)
{
/* -ed- enhancable. See the FAQ */
return 1 + (rand () % 9);
}

/* -ed-
* get a line properly from the user
*/
int get_s (char *s, size_t size)
{
int err = fgets (s, size, stdin) == NULL;

if (!err)
{
char *p = strchr (s, '\n');

if (p != NULL)
{
*p = 0;
}
else
{
int c;

while ((c = fgetc (stdin)) != '\n' && c != EOF)
{
}
}
}
return err;
}

/* -ed-
* get an int from the user
*/
int get_i (int *pa)
{
char s[16];
int err = get_s (s, sizeof s);

if (!err)
{
if (pa != NULL)
{
*pa = (int) strtol (s, NULL, 10);
}
}
return err;
}

int question (int firstnum
,int secondnum
,int *p_end)
{
int a = 0;

printf ("How much is %d times %d ? " S_QUIT
,firstnum
,secondnum);
fflush (stdout);

{
int err = get_i (&a);

{
if (p_end)
{
*p_end = err;
}
}
}
return a;
}

int main (void)
{
srand ((int) time (NULL));

{
int firstnum = random1to9 ();
int secondnum = random1to9 ();
/* 1st number times 2nd number */
int answer = firstnum * secondnum;
int end;

/* use a do-while loop to check the answer of the question */
/* if user enters the correct answer, end program, else keep asking */

/* the same question until user get the question correct. */
do
{
/* assign the value of repsonse to resp1 */
int resp1 = question (firstnum, secondnum, &end);

if (!end)
{
if (resp1 != answer)
{
printf ("Wrong answer ! Please try again.\n\n");
}
else if (answer == resp1)
{
printf ("Very good!\n\n");
break;
} /* end else */
}
else
{
printf ("Gave up...\n");
}
}
while (!end);
}

return 0;
}
 

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
474,145
Messages
2,570,824
Members
47,371
Latest member
Brkaa

Latest Threads

Top