rand problem v2.0

From: Jeff Borders (jeff_at_jeffborders.com)
Date: 10/28/04


To: beginners@perl.org
Date: Thu, 28 Oct 2004 13:05:21 -0400

Hello, (revised to include entire program, input file, and output)

I've written a simple quiz program and I've run into a problem with rand
and the integer it returns. It seems that it will eventually choose
either 0 or n+1 (I'm guessing) which blows up my array. I want to make
sure it chooses from the entire pool of questions. ie. Don't skip the
first and last array item. What suggestions can anyone give me?

Thanks- Jeff Borders (perl newbie)

### Sample Input File: korean.txt ###
One = Hana
First = Il
Two = Dool
Second = Yi
Three = Set
Third = Sahm
Four = Net
Fourth = Sa

### Sample Output ###
Question 20: Olgul Dwi Chaki
 
1. Back pivot kick to the up
2. Ridgehand
3. Roundhouse kick to the middle
4. Leg
 
press q to quit
Enter your answer: 1

Question 21: Son
 
Use of uninitialized value in concatenation (.) or string at ./korean.pl
line 58, <STDIN> line 21.
1.
2. Master Instructor
3. Middle section
4. Hand
 
press q to quit
Enter your answer:

### korean.pl ###
#!/usr/bin/perl

use warnings;
use strict;

my $number_of_questions=0;
my $random_question=0;
my $random_order=0;
my $i=0;
my $response=0;
my $total=0;
my $incorrect=0;
my $percent=0;
my @answer;
my @question;
my $answer=0;
my $question=0;

open DICT, '<', 'korean.txt' or die "Cannot open 'korean.txt' $!";

while (<DICT>) {
        chomp;
        $number_of_questions++;
        ($answer, $question) = split /\s*=\s*/;
        $answer[$number_of_questions] = $answer;
        $question[$number_of_questions] = $question;
        }
close DICT;

open ERRFILE, '>', 'errfile.txt' or die "Cannot open 'errfile.txt' $!";

sub check_vars
        {
        print ERRFILE "Question Number: $number_of_questions\n";
        print ERRFILE "Number of Questions: $number_of_questions\n";
        print ERRFILE "Random Question: $random_question\n";
        print ERRFILE "Random Order: $random_order\n";
        print ERRFILE "i: $i\n";
        print ERRFILE "Response: $response\n";
        print ERRFILE "Total: $total\n";
        print ERRFILE "Incorrect: $incorrect\n";
        print ERRFILE "Percent: $percent\n";
        print ERRFILE "Answer: $answer\n";
        print ERRFILE "Question: $question\n";
}

do {
$total++;
$random_question = int(rand($number_of_questions));
$random_order = int(rand(4)); #returns an integer from 0-3.
$random_order++; #increment to get 1-4.
print "\n\nQuestion $total: ";
print "$question[$random_question]\n\n";
for $i (1..4) {
        if ($i == $random_order) {
                print "$i. $answer[$random_question]\n"; #print correct value from
array.
        } else {
                print "$i. $answer[int(rand($number_of_questions))]\n"; #print
incorrect value from array.
        }
&check_vars();
  }
print "\npress q to quit\n";
print "Enter your answer: ";
chomp($response=<STDIN>);
if ($response eq 'q') {
        print "\n";
        $total--;
        #if ($total = 0) { abort; }
        }
elsif ($response != $random_order) { print "-----> Answer is
$random_order <-----\n"; $incorrect++; } #print correct value from
array.

} until ($response eq 'q');

$percent=eval (($total-$incorrect)/$total)*100;
printf "Out of %2d questions, you missed %2d for a grade of %3d%%\n",
$total,$incorrect,$percent;
print "Total number of questions is $number_of_questions\n";

close(ERRFILE) || die "Couldn't close file properly";



Relevant Pages

  • Inputstream e BufferedReader....help me!
    ... I have to develop a JSF application that allow user to upload a txt ... Later the application has to store the file into a byte's array in ... Populate an HashMap from an input file. ... first value on the line is a String. ...
    (comp.lang.java.programmer)
  • Re: I sure hope someone can help with this. how to use getline and multible arrays
    ... A slight adjustment allows you to store them into an array. ... string, ... > I first thought that I could open 2 streams to the input file. ... Losing the pointer argument isn't hard... ...
    (alt.comp.lang.learn.c-cpp)
  • Re: I/O file in two-dimensional array
    ... > What I want is to read in an input file from notepad.txt ... we declare constants [i.e. unchangeable data - it remains the same ... is to create a two dimensional array [conventiopnally refered to ... Among other things, this stream object instantiation ...
    (alt.comp.lang.learn.c-cpp)
  • Re: NumberFormatException:please help me......!!!
    ... Record that has string and array of double as members) and file could ... follows it on a line of the input file, you could use the String as a key to ... contain String representations of doubles. ...
    (comp.lang.java.programmer)
  • Re: Reading a table
    ... Resulting array: ... void no_free; ... ** Close temp input file after reading. ... if (rc!= EOF) { ...
    (comp.lang.c)