Like Tree9Likes

developing an easy CAI programm in C

This is a discussion on developing an easy CAI programm in C within the C Programming forums, part of the General Programming Boards category; The function definition of "correct()" goes after and outside of "main()". [edit] Rather than retype it, see post #4 here: ...

  1. #16
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    The function definition of "correct()" goes after and outside of "main()".

    [edit] Rather than retype it, see post #4 here: need help calling a function, please.
    Ashl7 likes this.

  2. #17
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    Quote Originally Posted by JohnGraham View Post
    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!

  3. #18
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    Quote Originally Posted by AndiPersti View Post
    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;
    }

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,272
    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.
    Ashl7 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    tnx...yea I made mistake on the range of rand() carelessly!!!


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

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

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,272
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    Quote Originally Posted by laserlight View Post
    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?!
    Last edited by Ashl7; 10-13-2012 at 12:17 AM.

  8. #23
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57

    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...
    Last edited by Salem; 10-13-2012 at 06:21 AM. Reason: snipped opinion

  9. #24
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    Quote Originally Posted by Salem View Post
    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. #25
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    You mean like
    Code:
    switch ( rand()  % 4 ) {
      case 0:
        // is it obvious yet?
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #26
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    Quote Originally Posted by Salem View Post
    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

  12. #27
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Print something.

    Then case 1 and print something else.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #28
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    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?!
    thanks in advance

  14. #29
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #30
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    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!!

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Developing GUI app with C
    By elf1984 in forum C Programming
    Replies: 2
    Last Post: 10-23-2008, 10:58 AM
  2. Seg fault in easy, easy code
    By lisa1901 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2007, 05:28 AM
  3. Further developing C for the web
    By bjdea1 in forum C Programming
    Replies: 24
    Last Post: 12-25-2002, 01:49 PM
  4. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 10:36 PM
  5. Replies: 6
    Last Post: 11-28-2001, 05:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21