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; Hi all, I'm new to programming and been using this website to get information for a while now...I must say ...

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

    developing an easy CAI programm in C

    Hi all, I'm new to programming and been using this website to get information for a while now...I must say this is a really good website...
    now I have a question about a program I need to write:
    here is the questionsorry if it's too long lol)

    Computer-Assisted Instruction) The use of computers in education is referred to as computer-assisted instruction (CAI). Write a program that will help an elementary school student learn multiplication. Use the rand function to produce two positive one-digit integers. The program should then prompt the user with a question, such as
    How much is 6 times 7?
    The student then inputs the answer. Next, the program checks the studentís answer. If itís correct, display the message ďVery good!Ē and ask another multiplication question. *If the answer is wrong, display the message ďNo. Please try again.Ē and let the student try the same question repeatedly until the student finally gets t right. 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.
    (Computer-Assisted Instruction: Reducing Student Fatigue) One problem in CAI environments is student fatigue. This can be reduced by varying the computerís responses to hold the studentís attention. Modify the program of above problem so that various comments are displayed for each answer as follows:
    Possible responses to a correct answer:
    Very good!
    Excellent!
    Nice Work!
    Keep up the good work!

    Possible responses to an incorrect answer:
    No. Please try again.
    Wrong. Try once more.
    Donít give up!
    No. Keep trying.

    Use random-number generation to choose a number from 1 to 4 that will be used to select one of the four appropriate responses to each correct or incorrect answer. Use a switch statement to issue responses

    now my program is like this:

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

    int main()
    {

    int a,b,c;
    a=rand();
    b=rand();
    printf("Hello, this is computer assited instruction for multiplication...\nHow much is the product of %d and %d? \n",a,b);
    scanf("%d",&c);
    if(c==a*b)
    printf("Very Good!!!");
    else
    printf("no please try again");
    return 0;

    don't laugh, I know it's not close to the answer at all!!!
    I needed to know how I can use the rand() to take only 1-10?!
    and then about the second part, how to use rand() to make that sentences!!! I've already read the rand() on c++ in this website but it wasn't much help in writing it in C, though it really helped in getting the concept of the function rand()
    anyone have any idea please let me know, would really appreciate it

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    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.
    Ashl7 likes this.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    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.
    Ashl7 likes this.
    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.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Ashl7 View Post
    I needed to know how I can use the rand() to take only 1-10?!
    and then about the second part, how to use rand() to make that sentences!!! I've already read the rand() on c++ in this website but it wasn't much help in writing it in C, though it really helped in getting the concept of the function rand()
    There is a FAQ on this site which covers random numbers in C. It will tell you that you should also seed the random number generator using srand().

    Reading your assignment ("Use a switch statement to issue responses"), I guess you haven't learned about arrays yet, have you?
    Do you know how to use the switch statement?

    And please use code-tags when you post your code.

    Bye, Andreas
    Ashl7 likes this.

  5. #5
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    Thanks for the response guys...I guess I can write my assignment(other assignments) easier now that I know about rand()...I have some about lottery...

    Quote Originally Posted by AndiPersti View Post
    There is a FAQ on this site which covers random numbers in C. It will tell you that you should also seed the random number generator using srand().

    Reading your assignment ("Use a switch statement to issue responses"), I guess you haven't learned about arrays yet, have you?
    Do you know how to use the switch statement?

    And please use code-tags when you post your code.

    Bye, Andreas
    no not yet...but I've read about switch before...why does it have to do something with arrays? even in the tutorial, it doesn't say something about arrays!

  6. #6
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    btw I was wondering how to write a code in my program.....
    I wanted to make my program in a way that when I use scanf("%c",&c), and then someone inputs integers(like 7)when it's compiling, instead of giving me a weird number, it actually say that you have to input a character!!! or when it is needed to input only numbers, it just accept numbers...is it even possible to do in C?

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    Quote Originally Posted by Ashl7 View Post
    ...why does it have to do something with arrays? even in the tutorial, it doesn't say something about arrays!
    Arrays are just another way to solve the problem instead of "switch()" (see Salem's post #3 above).

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Code:
    int number;
    char choice;
    scanf("%d", &number);  // read the next integer
    scanf(" %c", &choice); // read the next NON-WHITESPACE character.
    Note carefully the leading space in the %c case.

    scanf is a little bit tricky for newbies, and %c is one of the most troublesome to get right.
    Ashl7 likes this.
    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.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    Quote Originally Posted by Ashl7 View Post
    btw I was wondering how to write a code in my program.....
    I wanted to make my program in a way that when I use scanf("%c",&c), and then someone inputs integers(like 7)when it's compiling, instead of giving me a weird number, it actually say that you have to input a character!!! or when it is needed to input only numbers, it just accept numbers...is it even possible to do in C?
    Yes - input validation is a very important concept in programming.

    For starts, "scanf()" has a return value - it returns the number of arguments successfully read. So you can do something like for reading in a single integer:

    Code:
    if(scanf("%d",&var) == 1)
        printf("You entered %d\n",var);
    else
        printf("Bad input!");
    [edit] As Salem indicates above, "scanf()" can appear to behave strangely when you're new to it and don't know all the little details about that function. I'd recommend reading the link above in this post.
    Last edited by Matticus; 10-12-2012 at 11:24 AM.
    Ashl7 likes this.

  10. #10
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    thanks for the quick response...
    yeah my concern is about writing a program that can accept a password...that's why I wanted to know how to write a program that only accepts integers when needed...
    also, is there a way for the program to recognize numbers...let's say I have 75 in my program, and ask the user to input a number...the number he gives me is 54...then it says, you have one digit correct(which is 5)...
    I know if he enters the exact number, then we just have to use if(a==75)...but to check only for one digit in a number, I'm not sure how to write a program!

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Ashl7 View Post
    I know if he enters the exact number, then we just have to use if(a==75)...but to check only for one digit in a number, I'm not sure how to write a program!
    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
    Ashl7 likes this.

  12. #12
    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.
    thx for the help...two things I'm still a little bit confused :
    1-the expression "const char *responses[]"...what is the * doing there?!
    2- why did you use %s in printf function instead of %c?!
    3- where, or how did u called the function "correct"?

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    1-the expression "const char *responses[]"...what is the * doing there?!
    Code:
    char string_1[] = "This string can be modified";
    
    // The above declares an array of characters - a string in 'C' is an array
    // of characters that is terminated with a null character '\0'
    
    const char *string_2 = "This string cannot be modified";
    
    // The above line declares a character pointer that points to the memory location
    // where the beginning of the string resides.  When a string is declared this way,
    // it cannot be modified as it can be when declared as a character array (above).
    
    const char *responses[] = {
        "Very good!",
        "Excellent!",
        "Nice Work!",
        "Keep up the good work!"
      };
    
    // The above lines declare an array of character pointers, which is an array
    // of strings, each string like the one in the previous example.
    
    // Note that when declaring a character pointer, the string must be defined immediately.
    2- why did you use %s in printf function instead of %c?!
    %c will print a single character. %s will print a string (an array of characters terminated with a null character '\0').

    Code:
    char string_4[6] = "Hello";  // contains six characters: 'H' 'e' 'l' 'l' 'o' '\0'
    int i;
    
    // print a string character by character
    for(i=0; i<6; i++)
        printf("%c",string_4[i]);
    
    // print a string all in one go
    printf("%s",string_4);  // "string_4" behaves like a pointer to the first element, string_4[0]
    3- where, or how did u called the function "correct"?
    This function can be called from any other function, same as all functions.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // "correct()" function defined here
    
    int main(void)
    {
        correct();
    
        return 0;
    }
    Last edited by Matticus; 10-12-2012 at 01:59 PM. Reason: added <stdlib> for "rand()" =)
    Ashl7 likes this.

  14. #14
    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

    yea I'm starting to feel it man it
    tnx

    Matticus: very helpful man
    Last edited by Ashl7; 10-12-2012 at 03:01 PM.

  15. #15
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    ok, based on what you guys said, my program turned out to be something like this...everything's right until the IF statement"
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    void correct ( void );
    
    int main()
    {
        srand(time(NULL));
        int a,b,c,d;
         a= rand() % 10;
         b= (rand() % 10);
        printf("Hello, this is computer assited instruction for multiplication...\nHow much is the product of %d and %d? \n",a,b);
        scanf("%d",&c);
        printf("you said the answer is %d",c);
        d=a*b;
        if(c==d)
            
        void correct(void)
        {
        const char *responses[] = {
        "Very good!",
        "Excellent!",
        "Nice Work!",
        "Keep up the good work!"
      };
      printf("%s\n", responses[rand()%4] );
    }
        return 0;
    }

    now I don't know how to call the function correct()...(I guess I have some problem with function declaring, defining and calling, got to see more examples)...in order to write those 4 statements randomly!!!!

Page 1 of 3 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