By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,938 Members | 1,578 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,938 IT Pros & Developers. It's quick & easy.

Learn Multiplicatin Program

P: n/a
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;
}
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <a6**************************@posting.google.com >, Richard wrote:
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. <snip> 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,
--
Rob van der Leek | rob(at)ricardis(dot)tudelft(dot)nl
Nov 14 '05 #2

P: n/a
Richard wrote:
#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
Nov 14 '05 #3

P: n/a
ra******@excite.com (Richard) writes:
[...]
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>.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4

P: n/a
In 'comp.lang.c', ra******@excite.com (Richard) wrote:
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;
}

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.