How to make a quiz program in turbo pascal? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to make a quiz program in turbo pascal?

JesseTJesseT Posts: 4Member

I need to make a quiz with 30 question (15 multiple choice, 15 cloze questions), and have the order of all questions randomized. It also must calculate the result as a percent and display the qeustions answered wrongly with the correct answer. Even just a sample with 3 or 4 example questions would be fine. I can almost do it, but i cant seem to randomize the order, and so that I dont get the same question appearing more than once. Any help would be [b]GREATLY[/b] appreciated.

Comments

  • JesseTJesseT Posts: 4Member
    I made a very dodgy program for this, but it doesnt stop a question being asked more than once. I know this is a very dodgy way of doing it, but its the only way i can do it(im pretty new to programming).
    here it is...


    program assessment;

    uses wincrt;

    type

    question = record
    ask, correctanswer, useranswer: string;
    count: integer;
    end;

    var

    counter: integer;
    questions: array[1..3] of question;
    correctscore, incorrectscore: integer;
    number: array[1..3] of integer;



    procedure Q1;
    begin
    questions[1].ask:= 'What is an integer? 1. A fraction 2. A whole number 3. A decimal';
    writeln (questions[1].ask);
    readln (questions[1].useranswer);
    questions[1].correctanswer:= ('2');
    if questions[1].useranswer = questions[1].correctanswer
    then correctscore := correctscore + 1
    else incorrectscore := incorrectscore + 1;
    end;

    procedure Q2;
    begin
    questions[2].ask:= 'What does BNF stand for? 1. Backus-Naur Form 2. Backus-Noir Form 3. Big Naughty Farmer';
    writeln (questions[2].ask);
    readln (questions[2].useranswer);
    questions[2].correctanswer:= ('1');
    if questions[2].useranswer = questions[2].correctanswer
    then correctscore := correctscore + 1
    else incorrectscore := incorrectscore + 1;
    end;

    procedure Q3;
    begin
    questions[3].ask:= 'An example of an object driven programming language is? 1. Pascal 2. C 3. Java';
    writeln (questions[3].ask);

    readln (questions[3].useranswer);
    questions[3].correctanswer:= ('3');
    if (questions[3].useranswer) = questions[3].correctanswer
    then correctscore := correctscore + 1
    else incorrectscore := incorrectscore + 1;
    end;

    begin
    correctscore:= 0;
    incorrectscore:= 0;



    randomize;
    number[1]:= random(3) + 1;
    number[2]:= random(3) + 1;
    number[3]:= random(3) + 1;
    writeln ('Welcome to the QUIZ, by Jesse');
    writeln ('To select an answer, type the number of the answer');
    writeln ('eg If the answer is 2. (answer), type 2');
    writeln ('');

    if number[1] = 1 then
    Q1
    else if number[1] = 2 then
    Q2
    else if number[1] = 3 then
    Q3;
    if number[2] = 1 then
    Q1
    else if number[2] = 2 then
    Q2
    else if number[2] = 3 then
    Q3;
    if number[3] = 1 then
    Q1
    else if number[3] = 2 then
    Q2
    else if number[3] = 3 then
    Q3;

    writeln ('');
    writeln ('');
    writeln ('Answers correct = ', (correctscore),' ');
    writeln ('Answers incorrect = ', (incorrectscore),' ');
    writeln ('');
    writeln (' YOUR ANSWER CORRECT ANSWER');
    writeln ('Question 1 ', (questions[1].useranswer) ,' ', questions[1].correctanswer,'');


    end.
  • _Atex__Atex_ Posts: 163Member
    [code][color=Blue]{$apptype console}
    {$type turbo} // <-- turn on TP compability

    uses crt;

    const n{umber of questions}=5;
    q{uestions}:array[1..n] of string[60]=
    ('Question 1',
    'Question 2',
    'Question 3',
    'Question 4',
    'Question 5');
    a{nswers}:array[1..n,1..3] of string[60]= // first entry is considered to be the right one
    (( 'Q1 Right answer' , 'Q1 Wrong answer 1' , 'Q1 Wrong answer 2' ),
    ( 'Q2 Right answer' , 'Q2 Wrong answer 1' , 'Q2 Wrong answer 2' ),
    ( 'Q3 Right answer' , 'Q3 Wrong answer 1' , 'Q3 Wrong answer 2' ),
    ( 'Q4 Right answer' , 'Q4 Wrong answer 1' , 'Q4 Wrong answer 2' ),
    ( 'Q5 Right answer' , 'Q5 Wrong answer 1' , 'Q5 Wrong answer 2' ));
    l:array[1..3] of char='ABC'; // hotkeys for answers


    procedure swap_(var a,b:word); // a quick way to swap two integers
    begin if a=b then exit;a:=a xor b;b:=a xor b;a:=a xor b;end;


    var i,j,hits:word;
    o{ questions order }:array[1..n] of word; // <-- this allows for max. 65565 of q.'s
    oa{ answers order }:array[1..3] of word; // <-- for randomizing the answers, because the good one is stored 1st
    t{riggers}:array[1..n] of boolean; // <-- to see which question got bad answer
    ch:char;

    begin
    hits:=0; // <-- number of good answers
    fillchar(t,sizeof(t),1); // init triggers with "true" values
    randomize; // init random no. generator
    for i:=1 to n do o[i]:=i;
    for i:=1 to 3 do oa[i]:=i; // initial values
    for i:=1 to n*2 do // n*2 swaps to randomize order, for high n values use: sqr(n)/2
    swap_(o[random(n)+1],o[random(n)+1]);
    i:=1;
    {###### main loop ############}

    repeat
    writeln(#13#10#13#10'Question no. ',i,' of ',n,' Press ',l[1],',',l[2],',',l[3],' for correct answer, Esc to quit'#13#10);
    writeln(' ',q[o[i]]); // display question
    for j:=1 to 6 do // randomize answers order
    swap_(oa[random(3)+1],oa[random(3)+1]);
    for j:=1 to 3 do // display answers
    writeln(#13#10,' ',l[j],') ',a[o[i],oa[j]]);

    repeat
    ch:=upcase(readkey); // wait for user input + capitalize
    until ((ch=l[1]) or (ch=l[2]) or (ch=l[3]) or (ch=#27)); // only the the hotkeys and Esc can pass here, everything else is ignored
    delay(100); // slight delay
    while keypressed do readkey; // flush keyboard buffer in case of multiple keypresses

    for j:=1 to 3 do // check if user answered right
    if ((ch=l[j]) and (oa[j]=1)) then begin
    inc(hits); // increase hit number
    t[o[i]]:=false; // turn off trigger
    end;

    inc(i); // go to next question
    until ((ch=#27) or (i>n)); // user abort or reached max. number of questions

    {############################}
    dec(i);
    writeln(#13#10#13#10);
    if i>> The correct answer is:');
    writeln(' ',a[o[j],1],#13#10);
    end;
    end;
    readln;
    end.
    [/color][/code]
Sign In or Register to comment.