Saturday, May 9, 2009

C math quiz program Help?

Hello, I'm learning to Program in C and I have to create a math quiz. The user is supposed to enter the amount of questions they want to answer. I'm trying to get a Random number generator to produce two random numbers and then a random sign (+ - * /) so basically it looks like this (Ex. 4+4 = ) Then the user inputs the answer and it goes on to the next question till the person answered all of them. Then I wanted a text file that containted all the questions and answers..





Here is what I have so far... which is reall slim..any ideas??





#include%26lt;stdio.h%26gt;


#include%26lt;stdlib.h%26gt;


#include%26lt;time.h%26gt;


#include%26lt;string.h%26gt;





int randomNumbers(){





rand() % 100 + 1;


}


int num(){


randomNumbers();


}


char sign(){


}


int equation(int a, int b, char c){


}


int randomSign(){


int sign = 1 + rand() % 5;


if (sign == 1)


return '+';


if (sign == 2)


return '-';


if (sign == 3)


return '*';


if (sign == 4)


return '/';


if (sign == 5)


return '%';


}





int main(){











int correct = 0;


int x, y, answer, correctanswer;


char z;





x = num();


y = num();


z = sign();





printf(" %d %s %d =", x,y,z);





return 0;


}

C math quiz program Help?
I won't attempt do the whole program for you as its quite involved, I will get you to the point of getting the formulas and random numbers working and hopefully you'll work out the rest.





According to the documentation: The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Use the srand function to seed the pseudorandom-number generator before calling rand, as you can see I do in the main() function.





The code below accepts the amount of questions you want and spits out random maths questions and their answer.





To get your version working you need to ask the user to answer each question as they are asked, read the value they type (hint use scanf) and store the value in a text file along with the answer for comparison. Read about File Pointers, fopen(), fprintf(), fclose() etc. It shouldnt be too hard to get it working how you want it.





#include%26lt;stdio.h%26gt;


#include%26lt;time.h%26gt; // we are using time()





int num(void)


{


int num = (rand()%100+1);





// uncomment this line to see the randon number generated


//printf("num=%d\n", num);


return (num);


}





char sign(void)


{


int sign = rand()%5 + 1;





// uncomment this line to see the randon number generated


//printf ("sign=%d\n", sign);





// convert that randon number into a sign character


// the code below should be rewritten as a


// switch() statement with a return (char) at the end


// but it works as is.





if (sign == 1)


return '+';





if (sign == 2)


return '-';





if (sign == 3)


return '*';





if (sign == 4)


return '/';





if (sign == 5)


return '%';


}





void main(void)


{


float answer=0;


int x=0;


int y=0;


char z;


int NumberOfQuestions=0;


int Loop=0;





/*


* Seed the random-number generator with the


* current time so that the numbers will be


* different every time we run.


*/


srand( (unsigned)time(NULL) );





printf ("How many questions do you wish to answer?\n");





scanf("%d", %26amp;NumberOfQuestions);





for (Loop=1; Loop%26lt;=NumberOfQuestions; Loop++)


{


x = num();


y = num();


z = sign();





if (z == '+')


{


answer = (float)x + (float)y;


}


else if (z == '-')


{


answer = (float)x - (float)y;


}


else if (z == '*')


{


answer = (float)x * (float)y;


}


else if (z == '/')


{


answer = (float)x / (float)y;


}


else if (z == '%')


{


answer = (float)(x % y);


}





printf("Question %02d: %d %c %d = %.2f\n\n",


Loop,x,z,y, answer);


}


}
Reply:You are on the right track. Ditch your num() function and just call randomNumbers() directly.





To compute your answer you are going to have to know which operator you selected, or you can compute your answer at the same time as you select your operator. Something like this will return the character, and the answer all at the same time:





char *pick_oper( int a, int b, int *result )


{


switch( rand() % 4 )


{


case 0: *result = a + b; return "+";


case 1: *result = a - b; return "-";


case 2: * result = a / b; return "/";


default: * result = a * b; return "*";


}


}





Using a function like this saves you from computing the operator, and then having to use another switch statement to compute the answer.





You also need to consider converting your answer to double so that the result of a division problem can be properly represented.





Your printf that prints your equasion will likely bomb because you specify %s for the sign, but you have declared it as a character. You need to return a string from your character selection function and declare 'z' to be char *z.


No comments:

Post a Comment