# developing an easy CAI programm in C

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 10-12-2012
Matticus
The function definition of "correct()" goes after and outside of "main()".

 Rather than retype it, see post #4 here: http://cboard.cprogramming.com/c-pro...n-please.html#
• 10-12-2012
Ashl7
Quote:

Originally Posted by JohnGraham
Use rand() with the modulo operator (%) - e.g:

Code:

```int x = rand() % 10;  // x is 0-9. int y = (rand() % 10) + 1;  // y is 1 - 10.```
To select a random string, make an array of strings and use the method above to get a random index into that array.

this was a very easy way of using rand()...I wanted to make a 2 digit number and I just made a rand() function like this: a=(rand() % 89) + 10;
this will make 2 digits only right?! I read the FAQ section and it was kind of confusing!
• 10-12-2012
Ashl7
Quote:

Originally Posted by AndiPersti
You can use a combination of modulo (%, remainder of division) and integer division:
123 % 10 = 3
123 / 10 = 12

Can you imagine how this may work in a loop?

Bye, Andreas

I figured this all out... thanks...but I was just wondering, how would I get the real answer of for example a=78/10 then?!
I mean I know since a is an int, then it just gives us 7...but I changed the int to float (or double), and it gave me the result of the fraction(78/10) 7.00000!!!!!! which is obviously not!!!
(if someone going to to the favor and explain that to me, please explain what's the difference between double and float!!!!!! I just seem to understand you guys language better than my book( C by Discovery!!!, it sucks!) )

Code:

```int main() {     float  a,b;     a=76;     b= 76/10;     printf("%f",b);     return 0; }```
• 10-12-2012
laserlight
Quote:

Originally Posted by Ashl7
this was a very easy way of using rand()

Yes, though it does have the weakness of introducing a slight bias in most cases.

Quote:

Originally Posted by Ashl7
I wanted to make a 2 digit number and I just made a rand() function like this: a=(rand() % 89) + 10;
this will make 2 digits only right?

Yes, though you probably wanted (rand() % 90) + 10 unless you want to exclude 99.

Quote:

Originally Posted by Ashl7
I mean I know since a is an int, then it just gives us 7...but I changed the int to float (or double), and it gave me the result of the fraction(78/10) 7.00000!!!!!! which is obviously not!!!

You probably did not print the result correctly.
• 10-12-2012
Ashl7
tnx...yea I made mistake on the range of rand() carelessly!!!

Quote:

Originally Posted by laserlight
You probably did not print the result correctly.

but the program I have is the one on post #18...is that wrong?!?!
• 10-12-2012
laserlight
Quote:

Originally Posted by Ashl7
but the program I have is the one on post #18...is that wrong?!?!

Yes, because contrary to your claim that you "changed the int to float (or double)", you didn't: 76/10

You probably intended to write: 76.0f / 10.0f
• 10-12-2012
Ashl7
Quote:

Originally Posted by laserlight
Yes, because contrary to your claim that you "changed the int to float (or double)", you didn't: 76/10

You probably intended to write: 76.0f / 10.0f

oooooooooh wait a second!!! I got it!!! thanks a lot!!! it makes sense
but didn't I define a and b as float at the start of the program?! why do I have to do the change of variable again?!
• 10-12-2012
Ashl7
Finally!!!
ok, with the help of you guys and this website...I finally managed to write this damn code...all the answers I got lead to this...so thank you so much

this was the question:
Suppose you want to develop a program to play lottery. The program randomly generates a Lottery of a two-digit number, prompts the user to enter a two-digit number, and determines whether the user wins according to the following rule:

1. If the user matches the lottery in exact order , the awards is \$10,000.
2. If the user input matches the lottery, the awards is \$3,000.
3. If one digit in the user input matches a digit in the lottery, the awards is \$1,000.

this is the code I wrote:
Code:

```#include <stdio.h> #include <stdlib.h> int main() {     srand(time(NULL));     int lott,guess,ldiglot,rdiglot,ldigguess,rdigguess;     lott=(rand() % 90) + 10;     printf("what's your guess for the lottary number?! It's a TWO digit number...\n");     scanf("%d",&guess); rdiglot=lott % 10;     ldiglot= lott/10;     rdigguess=  guess %10;     ldigguess= guess/10;     if(guess==lott)     printf("WoW, you've won \$10,000\n");     else if( rdiglot==ldigguess && ldiglot==rdigguess)     printf("hmmmm, close...you won \$3000");     else if(rdiglot==rdigguess || rdiglot==ldigguess || ldiglot==ldigguess || ldiglot==rdigguess )     printf("not the right answer,but you won \$1000");     else     printf("wrong answer");     return 0; }```
thanks thanks thanks...more questions to come :)
however I'm still struggling with the question I asked at the beginning of the thread...
• 10-13-2012
Ashl7
Quote:

Originally Posted by Salem
Consider something like this
Code:

```void correct ( void ) {   const char *responses[] = {     "Very good!",     "Excellent!",     "Nice Work!",     "Keep up the good work!"   };   printf("%s\n", responses[rand()%4] ); }```
Each time you call correct(), it will generate a random congratulations message.

could someone please rewrite this program(make the function correct() as the above made it) without using the arrays?! by using switch statement?! cuz I haven't studied arrays yet!! :(
• 10-13-2012
Salem
You mean like
Code:

```switch ( rand()  % 4 ) {   case 0:     // is it obvious yet? }```
• 10-13-2012
Ashl7
Quote:

Originally Posted by Salem
You mean like
Code:

```switch ( rand()  % 4 ) {   case 0:     // is it obvious yet? }```

well the thing is, that piece of program should be able to get a number, and if that number is the one we want, then it say:

"Very good!", "Excellent!",
"Nice Work!",
"Keep up the good work!"

one of the above, randomly...we have to use rand() function here with the help of switch!!!!!! :/
the one u gave wasn't really obvious( to me, a retarded guy ;) ) to be honest
• 10-13-2012
Salem
Print something.

Then case 1 and print something else.
• 10-13-2012
Ashl7
alright...with the help of u guys, I wrote this program for the question at the beginning of the thread:

Code:

```#include <stdio.h> #include <stdlib.h> #include <time.h>   void func(void) {     srand(time(NULL));     int a,b,c;     a= rand() % 10;     b= (rand() % 10);     printf("How much is the product of %d and %d? \n",a,b);     scanf("%d",&c); } void wrong ( void ) {     const char *responses[] = {         "No. Please try again.",         "Wrong. Try once more.",         "Nope,Don’t give up!",         "No...Keep trying." }; printf("%s\n", responses[rand()%4] ); } void correct( void ) {     const char *responses[] = {     "Very good!",     "Excellent!",     "Nice Work!",     "Keep up the good work!"   };   printf("%s\n", responses[rand()%4] ); } int main() {     int a,b,c,d;     printf("Hello, this is computer assited instruction for multiplication...\n");     func();     d=a*b;     if(c==d)     correct();     else     wrong();     return 0;     }```
now a few questions:
my program fails the part of the exercise that says "A separate function should be used to generate each new question. This function should be called once when the application begins execution and each time the user answers the question correctly"...I'm trying to make the func() in my codes the function we want, but the answer is going to be always wrong!!! even when I enter the write answer it says you put the wrong answer....what am I doing wrong here?!
the other part I have problem with is "and let the student try the same question repeatedly until the student finally gets it right."
how am suppose to make a piece of code to enable this in my program?!
• 10-13-2012
Salem
The 'c' in func and 'c' in main are two different variables.

Try something in main like
c = func();

And make func() return a result.
• 10-13-2012
Ashl7
I can't quite understand that, could u pls explain more?!
I guess that way the function will not be the one the assignment needs it to be!!
Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last