开发者

Randomizing an Array with indexes grouped (i.e. 0-5 should be together)

I am trying to create a multiple choice exam type app. I have the questions and choices and answer of each problem in a .txt file structured like so:

Question#1
choice A
choice B
choice C
choice D
Answer#1
Question#2
choice A
choice B 
etc. etc.

The goal is to have this as a question bank with over a hundred questions. I have an array set up to read all this info. I need to randomize it but I need to do so in a way that will keep Question#1 to Answer#1 intact (so that Question 2 with its choices and answers right beneath it might come first). Is this possible at all?

The idea was to randomize a hundred questions and say take the first 50 questions (with their choices and answers intact) for a "practice session."

Thank you very much.

Here's the code:

  if ([typeOfTest isEqualToString:@"SelectedExam"]) {
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"SelectedExam" ofType:@"txt"];

        NSString *SelectedExamBank = [[NSString alloc] initWithContentsOfFile:filePath
                                                  encoding:NSUTF8StringEncoding error:NULL];

        NSString* theBank = SelectedExamBank;


 ...

 NSArray *multipleChoicePractice = [theBank componentsSeparatedByString:@"\n"];

    //Calculating indexes while Question number would be in increments of 6 
    //(question 1 = index 0,  question 2 = index 6, question 3 = index 12 etc)

    choiceAindex = questionNumber + 1;
    choiceBindex = questionNumber + 2;
    choiceCindex = questionNumber + 3;
    choiceDindex = questionNumber + 4;
    theAnswer = [multipleChoicePractice objectAtIndex:answerChecker];

    answerChecker = questionNumber + 5;

...

//q1 is the question and cA ... cD are choices

    q1 = [multipleChoicePractice objectAtIndex:questionNumber];
    cA = [multipleCh开发者_如何学编程oicePractice objectAtIndex:choiceAindex];
    cB = [multipleChoicePractice objectAtIndex:choiceBindex];
    cC = [multipleChoicePractice objectAtIndex:choiceCindex];
    cD = [multipleChoicePractice objectAtIndex:choiceDindex];


Without seeing the code, it would be tough to figure out a solution. Personally, I would suggest creating a new Question class that holds the question, choices, and answer. Then, you could simply randomize the questions within each instance of the class, and keep an array of Questions.


It is obviously possible but how to do it depends on what language are you using and how are you storing the information in the array.

I personally would create an array of "QA" objects (or structures/dictionaries/... depending on your language) and simply shuffle it. Because the QA object contains both the question and the answers, the association is maintained.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜