简体   繁体   English

如何改组数组问题?

[英]How to shuffle array questions?

I'm just new to C++. 我只是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 您可以创建索引向量,并为其使用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(%) . 如对问题的评论中所述,您可以使用cstdlib中rand()来限制返回的随机数,请使用系数(%)

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 编辑:您可能需要使用ctime作为标记

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. EDIT2:要随机化而不重复相同的问题,您可能必须存储已经用于跟踪它的问题,或者如果不再需要它,则可以将其从阵列中完全删除。

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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