Home > Enterprise >  How to generate a random mathematical operator
How to generate a random mathematical operator

Time:10-07

I have an assignment that requires me to make a quiz which generates random math questions. I'm fine with everything but i'm struggling to find a way to randomly choose between the mathematical operators " " and "-".

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(){
    int choice = 0;
    int lives = 0;
    int question = 1;
    int a;
    int b;
    int answer = 0;
    int ans = 0;
    int correct = 0;
    printf("\n Welcome to Maths Tester Pro.");
    printf("\n Please Select a difficulty:");
    printf("\n 1) Easy");
    printf("\n 2) Medium");
    printf("\n 3) Hard \n");
    scanf("%d%*c",&choice);
    switch(choice)
    {
        case 1:
        printf("You have selected Easy mode!");
        lives = lives 3;
        while ((lives !=0)&&(question !=6)){
            if(question !=5){
                                    //Ask Question
                printf("\nQuestion %d of 5.  You have %d lives remaining", question, lives);
                srand(time(NULL));
                a = (rand() % (10 - 1   1))   1;    //make the sign random
                b = (rand() % (10 - 1   1))   1;
                printf("\n%d   %d = ",a ,b);
                scanf("%d", &answer);
                ans = a b;
                                    //If answer is correct
                if((a b) == answer){
                    printf("Correct!\n");
                    correct = correct   1;
                }
                                    //If answer is incorrect
                else{
                    printf("Incorrect!  The correct answer was %d\n",ans);
                    lives=lives-1;
                }
                question = question   1;
            }

In my code I have it written as ans=a b but I want it to be able to randomly pick either " " or "-".

CodePudding user response:

The easiest way to go would be to change the sign of b. To do so, simply multiply it by either 1 (keeps positive sign) or -1 (makes it a negative):

b = b * ((rand() - (RAND_MAX / 2)) > 0 ? 1 : -1);

Upon execution, you will randomly get a b or a (-b).

Example print of the resulting operator:

printf("%d%s%d = %d\n", a, (b > 0 ? " " : ""), b, a   b);

Note: as pointed in earlier comments, you may also want to randomize the seed in order to prevent you application to keep providing the "same" random numbers with:

/* Randomize seed (needed once). */
srand(time(NULL));

/* Then make your calls to `rand()`. */
...

CodePudding user response:

I offer this as an example of how to cleanly generate and print the sort of problems that you seem to want.

This produces 20 'sum' and/or 'difference' equations. You can simply suppress printing the total in order to pose the question to the user.

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

int main() {

    srand( time( NULL ) );

    for( int i = 0; i < 20; i   ) {
        int a = (rand() % 10)   1; // 1 <= a <= 10
        int b = (rand() % 20) - 9; // -9 <= b <= 10

        printf( "%d %c %d = %d\n", a, " -"[b<0], abs( b ), a b );
    }

    return 0;
}
1   3 = 4
1 - 6 = -5
4   10 = 14
8   2 = 10
10   0 = 10
4   4 = 8
8   10 = 18
8   9 = 17
8 - 2 = 6
8 - 4 = 4
2   1 = 3
8 - 5 = 3
2 - 6 = -4
4   9 = 13
6   6 = 12
5   0 = 5
3   4 = 7
1   0 = 1
9 - 5 = 4
8   8 = 16

The keen eyed reader will notice that even "10 0" is a reasonable problem. Zero is the identity operator(?) for addition (like 1 being the identity operator(?) for multiplication.)

You're free to adjust the ranges of the terms to suit your questions.

  • Related