how to assign numbers to a list of questions

3 views (last 30 days)
function [p] = claim( q, choices )
q=('An electronic tool that allows information to be input, processed, and output')
choices={'Operating system', 'Motherboard', 'Computer', 'CPU'};
arrayfun(@(x)fprintf(' %c. %s\n', x-1+'a', choices{x}), 1:numel(choices));
q=('Which of the following is an input device?')
choices={'Monitor', 'Mouse', 'Projector', 'hdd'};
arrayfun(@(x)fprintf(' %c. %s\n', x-1+'a', choices{x}), 1:numel(choices));
while 1
p= lower(input('Your choice>', 's'));
if numel(p)==1 && p >= 'a' && p < 'a' + numel(choices)
p= p - 'a' + 1;
break;
end
end
end
  7 Comments
Walter Roberson
Walter Roberson on 19 Apr 2018
You also need to track the known answers in the loop, or track the score in the loop
n = 0;
numright = 0;
for k = randperm(length(S), num_to_ask);
n = n+1
P = claim(n, Sr(k).q, Sr(k).c);
numright = numright + P == known_answers(k);
end

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 15 Apr 2018
Here is one simple solution using a small function claim:
function p = claim(N, q, choices)
fprintf('%d. %s\n',N,q)
V = char('A'-1+(1:numel(choices)));
C = [cellstr(V(:)),choices(:)]';
fprintf(' %s. %s\n',C{:})
p = upper(input('Your choice: ','s'))-'A'+1;
end
and a script that loops over each question:
S(1).q = 'In literature,repetition is used essentially for';
S(1).c = {'a monologue','allusion','emphasis','ryhme'};
S(2).q = 'The performers in a play constitute the';
S(2).c = {'Chorus','Characters','Audience','Cast'};
S(3).q = 'An electronic tool that allows information to be input, processed, and output';
S(3).c = {'Operating system', 'Motherboard', 'Computer', 'CPU'};
N = numel(S);
P = nan(1,N);
for k = 1:N
P(k) = claim(k, S(k).q, S(k).c);
end
When the script is run it looks like this:
1. In literature,repetition is used essentially for
A. a monologue
B. allusion
C. emphasis
D. ryhme
Your choice: c
2. The performers in a play constitute the
A. Chorus
B. Characters
C. Audience
D. Cast
Your choice: d
3. An electronic tool that allows information to be input, processed, and output
A. Operating system
B. Motherboard
C. Computer
D. CPU
Your choice: c
The script stores all of the choices in one vector P:
>> P
P =
3 4 3
  7 Comments
Walter Roberson
Walter Roberson on 18 Apr 2018
Run the below ask_me_another . Notice that it asks only two of the three questions. For example,
>> ask_me_another
1. In literature,repetition is used essentially for
A. a monologue
B. allusion
C. emphasis
D. ryhme
Your choice: c
3. An electronic tool that allows information to be input, processed, and output
A. Operating system
B. Motherboard
C. Computer
D. CPU
Your choice: b
The only difference between Stephen's code and what I just attached is that I made the number of questions to ask into a named variable an I used the randperm that I showed above.
Stephen's code line
P(k) = claim(k, S(k).q, S(k).c);
is an example of calling claim()

Sign in to comment.

More Answers (1)

Asamoah Andrews
Asamoah Andrews on 18 Apr 2018
thank you very much Mr.Roberson i appreciate your effort a lot. Please one last favour I want to print out the scores at the end of the quiz

Categories

Find more on Thermodynamics and Heat Transfer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!