Title: Write a arithmetic test question program that requires each question to be randomly generated
Problem Solving Ideas:
1. Write a test question, and 30, you need to use the loop function, so think of using the for () function
2. Randomly generate two numbers and think of the rand () function.
Note: the 1.rand () function should be used with a header file #include<stdlib.h> The program has been unable to run since this header file was not used in class
2. I am debugging the process, the random number has been unchanged, through the data I learned that in order to change the random number, the need to provide a seed, the same seed will produce the same random number, so must precede the rand () function with Srand (Time (NULL)); Add #include<time.h> to the header file, so that two random numbers can be changed.
3. Randomly wrapped arithmetic to each question +,-, *,/are random, I think of the branch function, by using the IF statement, I feel a little trouble, and then I think of the switch () function, and by randomly generated 0,1,2,3 four number to branch, it is more convenient
4. Considering that pupils have not learned negative numbers, when doing subtraction, I think, I want to let the large number to reduce the number to calculate. There is division, divisor can not be 0, so I think if the divisor is 0, then re-out a problem, in order to ensure that the title of 30 questions, to let i+1.
Here is a small program I have written:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int x,y,z,t;
Srand (Time (NULL));
cout<< "Second grade arithmetic test question" in primary school <<endl;
for (int i=0;i<30;i++)
{
X=rand ()%100;
Y=rand ()%100;
Z=rand ()%4;
Switch (z)
{
Case 0:
cout<<x<< "+" <<y<< "=" <<endl;
Break
Case 1:
if (x<y)
{
T=x;
X=y;
y=t;
}
cout<<x<< "-" <<y<< "=" <<endl;
Break
Case 2:
cout<<x<< "*" <<y<< "=" <<endl;
Break
Case 3:
if (y!=0)
{
cout<<x<< "/" <<y<< "=" <<endl;
}
Else
{
I=i-1;
}
Break
Default
cout<< "Beyond Test Range" <<endl;
Break
}
}
cout<< "Congratulations on the completion of the 30 math quiz" <<endl;
return 0;
}
Run:
Random generation Arithmetic test questions written in C language