Random generation Arithmetic test questions written in C language

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.