简体   繁体   中英

How to shuffle array questions?

I'm just new to C++. Can someone help me randomized this questions when I cout? Is there a way to randomize this?

I have this code. Can someone tell me how to randomize these?

string questionpart[20]={"What is the square root of 4?",
                            "What is the square root of 6?",
                            "What is the square root of 16?",
                            "What is the square root of 25?",
                            "What is the square root of 36?",
                            "What is the square root of 42?",
                            "What is the square root of 48?",
                            "What is the square root of 81?",
                            "What is the square root of 100?",
                            "What is the square root of 121?",
                            "What is the square root of 144?",
                            "What is the square root of 169?",
                            "What is the square root of 196?",
                            "What is the square root of 225?",
                            "What is the square root of 256?",
                            "What is the square root of 289?",
                            "What is the square root of 324?",
                            "What is the square root of 361?",
                            "What is the square root of 400?",
                            "What is the square root of 1?",
                            };

string partans[20]={"2",
                        "3",
                        "4",
                        "5",
                        "6",
                        "7",
                        "8",
                        "9",
                        "10",
                        "11",
                        "12",
                        "13",
                        "14",
                        "15",
                        "16",
                        "17",
                        "18",
                        "19",
                        "20",
                        "1"};

THANKS IN ADVANCE!

you can create a vector of indexes and use for it the std::shuffle

also you can use half-manual shuffling. Example:

srand(time(NULL));
rand();

unsigned indexes[cnt];
unsigned fact = 0;

while(fact < cnt)
{
    const unsigned r = rand() % cnt;
    bool was = false;
    for(unsigned i = 0; i < fact; ++i)
    {
        if(indexes[i] == r) {
            was = true;
            break;
        }
    }
    if(!was)
    {
        indexes[fact] = r;
        ++fact;
    }
}

for(unsigned i = 0; i < cnt; ++i)
{
    const unsigned j = indexes[i];
    cout << "Q: " << questions[j] << "; A: " << answers[j] << endl;
}

As mentioned in the comment on the question you can use the rand() from cstdlib and to limit the returned random number, use a modulus(%) .

srand(time(NULL));
index = rand() % array_size;

then use the that index to access the question in the array.

cout << questionpart[index] << endl;

EDIT: you may need to use the ctime for the srand

EDIT2: to randomize without repeating the same question, you may have to store the questions that were already used to keep track of it or simply remove it totally from the array if you don't need it anymore.

for a more advanced approach you can define an object that will hold everything (the question, answer, and state) something like:

class Question {
    string question;
    string answer;
    bool wasAsked = false;
}

then create an array out of it(or preferably vector for dynamic array support)

Question questions[array_size];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM