Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

rand problem v2.0
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";




Report this thread to moderator Post Follow-up to this message
Old Post
Jeff Borders
10-28-04 08:57 PM


Re: rand problem v2.0
Jeff Borders wrote:
> Hello,  (revised to include entire program, input file, and output)

Okay, that's better. :)

> 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?

Your problem seems to have little to do with the rand() function, and
much to do with how you create and use the arrays.

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

Your sample data include 8 questions, but here you make both @answer and
@question contain 9 elements, of which the first is undef. That's
because the index of the first element in an array is 0, not 1.

> do {
> $total++;
> $random_question = int(rand($number_of_questions));

Since $number_of_questions is 8, $random_question may be something
between 0 and 7, while you need 1 - 8 considering how you designed the
arrays.

$random_question = 1 + int(rand($number_of_questions));

> 		print "$i.  $answer[int(rand($number_of_questions))]
\n";
> #print incorrect value from array.

print "$i. $answer[1 + int(rand($number_of_questions))]\n";

Optionally you can prevent the undefined first element when creating the
arrays.

A style thing you should reconsider is the declaration of variables.
Lexical variables should preferrably be declared in the smallest
possible scope when you first use them. So variables that are only used
in the do {} block should be declared within the do {} block instead of
in the beginning of the program, etc.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
10-28-04 08:57 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:22 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.