# Random selection from a pool of questions

1 view (last 30 days)
Hello, I am having a bit of trouble with my code. The purpose is for it to randomly select a question from a pool of questions and the user inputs the answer. The code works to where a question is randomly selected but once the user inputs an answer, it says its incorrect even when the answer is correct. I think the issue is that the questions are being randomized but the answers that match the question are not. How can I fix this?
QandA={'what is 6x8? ', '48';
'what is 12x12? ', '144';
'what is 60/12? ', '5';
'what is 5x6? ', '30';
'what is 8x4? ', '32';
'what is 7x9? ', '63';
'what is 54/9? ', '6';
'what is 2x12? ', '24';
'what is 5x10? ' '50';
'what is 9x3? ', '27';
'what is 22/2? ', '11';
'what is 3x10? ', '30'};
qorder = randperm(numel(QandA(:,1)));
QandAreordered = QandA(qorder, :);
for qidx=1:size(QandAreordered, 1)
answer=input(QandAreordered{qidx, 1}, 's');
if strcmp([answer ' '], QandAreordered{qidx, 2})
fprintf('Correct\n', '%s');
else
fprintf('Incorrect\n', '%s');
end
end
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Edited: Bastian Arturo Romero Mardones on 2 Dec 2020
I changed 2 lines and it works
QandA={'what is 6x8? ', '48';
'what is 12x12? ', '144';
'what is 60/12? ', '5';
'what is 5x6? ', '30';
'what is 8x4? ', '32';
'what is 7x9? ', '63';
'what is 54/9? ', '6';
'what is 2x12? ', '24';
'what is 5x10? ' '50';
'what is 9x3? ', '27';
'what is 22/2? ', '11';
'what is 3x10? ', '30'};
qorder = randperm(numel(QandA(:,1)));
QandAreordered = QandA(qorder, :);
for qidx=1:size(QandAreordered, 1)
answer=input(QandAreordered{qidx, 1}); %double
if (answer == str2double(QandAreordered{qidx, 2})) %string to double
fprintf('Correct\n', '%s');
else
fprintf('Incorrect\n', '%s');
end
end
##### 2 CommentsShowHide 1 older comment
what would be your parameters?

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!