AllExperts > Experts 
Search      

Pascal

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More Pascal Answers
Question Library

Ask a question about Pascal
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Francis Kong
Expertise
Pascal programming. I would be best at solving logic and syntax errors with some given code, or some programming theory and specific task. I have done mostly my pascal within a machine(meaning, not networking and serial cable yet)and I am not able to solve things in ObjectWindows... I should be able to answer most of the thing done in console(dos and x systems like linux) platform and in Mac(but for Mac I can only handle nonplatform-specific questions). Feel free to ask me, because if I am not able to answer it I might be able to tell where to get the specific material that you need

Experience
teached a classmate with an exam mark of 56% and the second shot with a higher difficulty the tutee gets more than 80%
 
   

You are here:  Experts > Computing/Technology > Pascal > Pascal > Turbo Pascal - Quiz program

Topic: Pascal



Expert: Francis Kong
Date: 8/11/2007
Subject: Turbo Pascal - Quiz program

Question
I'm trying to write a Turbo Pascal program that will serve as a way for me to quiz myself in various subjects.

The idea is that I will enter questions and answers into an Excel Spreadsheet and save it as a tab delimited file.

For example:
50th State? [tab] 3 [tab] Ohio [tab] Iowa [tab] Hawaii [tab] Alaska [tab] Maine

The number 3 indicates the position of the correct answer.

I want the program to randomly choose 25 questions, randomize the order of them, randomize the position of the answers and know the new position of the correct answer afterwards.

My first hurdle is to be able to go into the file and store the questions, answer position, and answers into arrays.

que[1..25] of string
ans[1..25,1..5] of string
pos[1..25] of integer

There may be hundreds of questions to choose the 25 from so I don't know how to go about randomly choosing 25 lines from the file.  I also wouldn't want to repeat any questions.

If you'd like, i will write up a sample text file from Excel for you to work with.

Thanks in advance, all help is appreciated.

-Dave

Answer
Assuming you know the number of lines in the file (by using readln repeatly till end of file and count, reset the file afterwards)then you can do the follwoing:

uses crt;

var
f: text;  {assume this is your source file variable}
maxline: longint;
qnum: array [1..25] of longint;
q,w: longint;
rpt: boolean; {indicate where question has repeated}

begin
randomize;
{insert code to determine number of lines and assign that to maxline}
for q := 1 to 25 do
begin
 repeat
 rpt := false;
 qnum[q] := random(maxline) +1;
 for w := 1 to q-1 do
 begin
   if qnum[q] = qnum[w] then
     rpt := true;
 end;
 until not rpt;
end;

{insert code to sort qnum}

end;

that should take care of the repeat question part.  For the extraction:

begin
reset(f);
q := 1;
for w:= 1 to 25 do
 begin
   while q < qnum[w] do
      begin
        readln(f);  {this move the current char in the file handle to the next line wihtout entering the data.  This only works if you have exactly 1 line per entry}
        q:= q+1;
      end;
   
   {insert code to extract the stuff from the file to the variable with subscript w}
   
   q := q+1;
 end

one of the way to read the line into the element of the array is to readln, get the position of the tab character using <pos>, and use <copy> to copy the strings, and remove the copied strings from the beginning of the string using <delete>.  use <val> if you need to convert the string into a number.  Read the help for function with names in <>


Let me know if you need further help.

Francis

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.