% program quiz1
% This program reads in a file of quiz questions and then uses the MATLAB
% menu command to ask the questions.  The question file must be a MATLAB
% file named A.mat.  This file is to contain a single cell array of the
% quiz questions with one row for each question and the columns as follows
%   column 1    The question contained in single quotes
%   columns 2-5 Four possible answers only one of which is correct 
%   column 6    The number of the correct answers possition (1-4)
% S. Scott Moor                                    November 2006

% Variables used 
%   A = the array of questions, answer options and correct answers
%   d = the number of rows and columns in A
%   rows = the number of rows in A
%   score = the total score on the quiz
%   k = the index variable for stepping through questions
%   q = the current question with question number (text)
%   x = a switch to leave while loop (1 = stay, 0 = leave)
%   qscore = the score on the current question
%   a = number of the menu option chosen (1-4) for current try

% load in the question file and determine its size
load A
d = size(A); 
rows = d(1);
score = 0;          % initialize the quiz score

% ask the questions in turn
for k = 1:rows  
    % label the command window with the question name and number
    q=A{k,1};
    question = [num2str(k),'. ',q];
    disp(question);
   % initialize variables x & qscore
    indicate = 1;
    qscore = 3;
    
    % ask the question with the menu command and then 
    % do not allow user to go on until the choose the correct question
    while indicate ==1
        a = menu(A{k,1},A{k,2},A(k,3),A(k,4),A(k,5));   % asks user for an answer
        if a==A{k,6}                                    % compares users answer to correct answer
            indicate = 0;                                    % set switch variable to leave loop
            % display congratulations, score and correct answer
            disp(['Congratulations you got it! ','Score on this question ',num2str(qscore),' out of 3'])
            disp(['Answer: ', A{k,a+1}])
            disp(' ')
            score = score + qscore;                     % adds score on this question to total
            
        else
            disp('Try Again')                           
            qscore = qscore -1;                         % lose one point for wrong answer
        end
    end 
end
% desplay the final score for the quiz 
disp(['Your total score is ', num2str(score),' out of ',num2str(d(1)*3)])
