473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Learn Multiplicatin Program

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
4 1380
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Aziz McTang | last post by:
Hi Group, I am not an experienced programmer at all. I've learned html and css well enough to hand-write simple websites. I'm now looking to move to the next step. Initially, I'd like to do 3...
55
by: Elijah | last post by:
I have read many of the topics on learning C++ or Java first. It seems like everyone says something different. I would like to know if I should learn C++ or Java. First a little about myself. I...
3
by: Chris | last post by:
Wait.. before you flame.. If someone can program in Java, or Javascript, or C, or (insert your language here that uses basically the same syntax as C#), and that person knew how to program in...
31
by: anand devarajan | last post by:
hi friends, im anand im just a beginner in c learning for the past two weeksnow i can write simple prgs can anyone help me to get well known to c lang so that i should able to write even tough...
34
by: pandit | last post by:
hai all, i want to become a good programmer. one of my friends ( named "arnuld", he posts here infrequently), taught me Lisp. so i am not a programming beginner. i have heard these 2 points....
5
by: Brian Blais | last post by:
Hello, I was wondering what the approximate minimum age to learn python is. Has anyone had experience teaching middle school students, or elementary school students Python? What brought this up...
21
by: Lee.kain | last post by:
I want to learn C++! does anyone have any advice? Lee
65
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective...
7
Banfa
by: Banfa | last post by:
Posted by Ganon11 So, you want to learn how to program! Good for you! Programming is a very intruiging and fun activity to pick up, and it's also a great career choice if you like it! Finally,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.