Re: Find the biggest number from three numbers
by Corion (Patriarch) on Dec 22, 2010 at 15:05 UTC
|
Your last print statement is guarded by an else clause. Think about whether you want to always execute that statement, and also look at the sequence of conditions that leads up to that statement.
Also, in Perl, eq and == are different. eq is for string comparison, while == is for numbers. Only in very rare cases should you mix eq with < or >.
| [reply] [d/l] [select] |
|
|
Thanks for pointing out the difference between eq and ==. Thinking again. Will report here when I solve this :)
| [reply] |
Re: Find the biggest number from three numbers
by JavaFan (Canon) on Dec 22, 2010 at 15:16 UTC
|
say $a == $b && $b == $c ? "All equal"
: $a >= $b ? $a >= $c ? "Biggest is $a"
: "Biggest is $c"
: $b >= $c ? "Biggest is $b"
: "Biggest is $c";
| [reply] [d/l] |
Re: Find the biggest number from three numbers
by jethro (Monsignor) on Dec 22, 2010 at 15:16 UTC
|
The last print statement is only reached when C is not the biggest number and they are not all equal. If you put the last print outside of the else clause it will get executed more often
PS: Check out what gets printed if A and B are some random but different numbers and C==0
PPS: You should always have a line 'use warnings;' in your code (right after '#!/usr/bin/perl'). It helps you with finding bugs (not this one though).
| [reply] |
|
|
Will be using it in my next experiments. Thank you for the suggestion
| [reply] |
|
|
Hey i gotta think again what's causing for that all numbers are equal when entered 0 for C
Checking it out. Thanks ;)
| [reply] |
Re: Find the biggest number from three numbers
by ww (Archbishop) on Dec 22, 2010 at 15:27 UTC
|
Using prints (as, for example print $a, $b, $c, $big, $equal;) just before line 20 would help you debug this.
And, free, at no extra charge (shipping and delivery, $19.97 or three easy payments of just $99.99 per month for four months), a couple hints:
- Since you want to deal with numbers, use == rather than eq to test equality.
- Format your code for readability -- chiefly by using indentation ... for example,
if($first == $second) { # Indentation for readabili
+ty
$big = $first;
$equal = $first;
} elsif($first > $second) { # but cuddled elses are mer
+ely my preference
$big=$first;
} else {
$big = $second;
}
- Don't routinely use $a and $b as variables; they're special to sort. While they do no harm here, they would if you attacked this task by using sort.
| [reply] [d/l] [select] |
|
|
Oh ok. And I did put wrong codes in previous posts which I've sorted out myself.
And I will be taking your suggestions while writing perl again. I would certainly format my code well.
And will make sure to use better variables :)
Thank you.
| [reply] |
|
|
| [reply] |
Re: Find the biggest number from three numbers
by roboticus (Chancellor) on Dec 22, 2010 at 16:15 UTC
|
shrsv:
I'm in a rambling mood right now, so this'll be a bit long.
First, I'd suggest that you pay a little more attention to the formatting of your code. I've reformatted it in two ways. First I put a blank line between the input section, the processing section and the output section to make the boundaries between them a bit more obvious. I also indented the nested code blocks:
Having done so, it's easier to see at least one of the errors: In your reporting section, one of your if branches doesn't print anything, so if you entered 1, 2, 3 as your numbers, then $big would be less than $c, and nothing would be printed:
$ perl 878546.pl
Enter a number
1
Enter second number
2
Enter third number
3
$
The way I'd suggest repairing it would be to move the middle case into the processing section, so you complete all your processing before reporting anything:
The next immediately apparent error is that you're trying to use $equal for two purposes, which is often a bit tricky. For example, check out this test case:
$ perl 878546.pl
Enter a number
1
Enter second number
2
Enter third number
0
All numbers are same
The problem is that you're trying to use $equal to be a flag to tell when the numbers are equal and to hold the value. But since you can enter any value for $c, you can get stuck. So I'd suggest using $equal as a simple yes/no flag. You can use $big to hold the value when they're equal, since if the values are equal, then $big will hold the correct value anyway. So to fix this bug, I'd change the code to something like:
#!/usr/bin/perl
print "Enter a number\n";
chomp($a=<stdin>);
print "Enter second number\n";
chomp($b=<stdin>);
print "Enter third number\n";
chomp($c=<stdin>);
$big=0;
$equal=0;
+
if($a eq $b){
$big = $a;
$equal = 1;
}
elsif($a > $b){
$big=$a;
}
else{
$big = $b;
}
if ($big < $c) {
$big = $c;
}
if ($big != $c) {
$equal = 0;
}
if($equal == 1){
print "All numbers are same";
}
else{
print "The biggest number is $big \n";
}
Here's a test case that'll show you another error, but I'll leave the solution to you, as I don't want to spoil the fun. Enter " 1", "1" and "1 " (without the quotes--they're just there to show you which blanks you'll want to enter), and you'll see that the program won't tell you that the numbers are all the same.
Finally, I'll make a suggestion: Make your program take an arbitrary number of input values.
Have fun!
...roboticus
When your only tool is a hammer, all problems look like your thumb. | [reply] [d/l] [select] |
|
|
It was fun and informative reading your detailed reply roboticus :)
It was clever to use $equal as a yes/no flag. I had solved the program like this, yesterday night(I'm entering different no.s and thinking ways where the program breaks)But now I consider your example superior. Anyway look at this:
#!/usr/bin/perl
use warnings;
print "Enter a number\n";
chomp( $a = <stdin> );
print "Enter second number\n";
chomp( $b = <stdin> );
print "Enter third number\n";
chomp( $c = <stdin> );
$big = 0;
$equal = 0;
if ( $a == $b ) {
$big = $a;
$equal = $a;
}
elsif ( $a > $b ) {
$big = $a;
}
else {
$big = $b;
}
if ( $equal == $c ) {
print "All numbers are same";
}
elsif ( $big < $c ) {
$big = $c;
print "The biggest number is $big \n";
}
else {
print "The biggest number is $big \n";
}
Having done so, it's easier to see at least one of the errors: In your reporting section, one of your if branches doesn't print anything, so if you entered 1, 2, 3 as your numbers, then $big would be less than $c, and nothing would be printed:
I completely agree. I would've spotted that error if i had formatted well.
A big thank you to PerlMonk for correcting my errors patiently. :)
| [reply] [d/l] |
Re: Find the biggest number from three numbers
by oko1 (Deacon) on Dec 22, 2010 at 16:29 UTC
|
In addition to the coding problems that others have pointed out, you also have a bit of a tool/problem mismatch: the job you're trying to do with your script is best handled by other, more suitable Perl control structures (it would be even easier with other functions and variable types, but we'll ignore that for now.) As a result, whatever solution you come up with is likely to be somewhat over-complicated and non-intuitive - i.e., a "complicated" if/then tree (which is where you got hung up.)
Here's an example that would do the job, although it may be outside the current scope of what you know. I've kept it as simple as possible.
#!/usr/bin/perl
use warnings;
use strict;
print "Please enter one number per line (empty line ends input):\n";
my $different = 0;
my $biggest = 0;
# Loop and read user input
while (1) {
chomp(my $input = <STDIN>);
if ($input eq ''){
last;
}
if ($biggest == 0){
$biggest = $input;
}
if ($input != $biggest){
$different = 1;
}
if ($input > $biggest){
$biggest = $input;
}
}
if ($different == 0){
print "All numbers are the same\n";
}
print "The biggest number is $biggest\n";
--
"Language shapes the way we think, and determines what we can think about."
-- B. L. Whorf
| [reply] [d/l] |
|
|
It looks astonishing to me that there are so many ways to solve a single problem. I haven't understood your example at full now, but I will eventually. I have only learned if, elsif and else and how they work. :-D
I won't be asking here what this and that is.The next things I will be searching:
And I will start studying the Learning perl book which a friend of mine has directed. This is my first ever program(except from hello world and similar programs from net)
| [reply] |
|
|
Heh. The motto of Perl is 'TMTOWTDI' - "There's More Than One Way To Do It". If you had asked to see different ways that people could implement that problem, you would have seen an amazing variety of ways to do it; in my experience, our fellow monks here are endlessly inventive. Here's a somewhat amusing one, off the top of my head:
#!/usr/bin/perl -l
use warnings;
use strict;
my @x;
print "Please enter one number per line (empty line ends input):";
while (<STDIN>){ last if /^$/; push @x, $_; }
print "The biggest number is ", (sort {$b<=>$a} @x)[0],
"@x" =~ /^([-0-9]+)\s+(?:\1\s+)*$/ && "All numbers are the same";
(Now don't let that distract you. "Learning Perl" is an excellent start for a Perl novice, so go study. :)
--
Education is not the filling of a pail, but the lighting of a fire.
-- W. B. Yeats
| [reply] [d/l] |
|
|
$biggest = ($a+$c+$b+$c+abs($a-$b)+abs($c-$a+$c-$b-abs($a-$b)))/4;
| [reply] [d/l] |
|
|
use List::Util qw( max );
($a, $b, $c) = (3, 12, 42);
print +(sort { $b <=> $a } $a, $b, $c)[0];
print max ($a, $b, $c);
These both work, but can you - as a perl beginner - still read it? Perl is so much fun due to TIMTOWTDI
Enjoy, Have FUN! H.Merijn
| [reply] [d/l] [select] |
Re: Find the biggest number from three numbers
by eff_i_g (Curate) on Dec 22, 2010 at 15:05 UTC
|
| [reply] |
Re: Find the biggest number from three numbers
by Anonymous Monk on Dec 22, 2010 at 15:10 UTC
|
| [reply] [d/l] |
|
|
Thank you very very much sir. Solved it in 5seconds. Actually it was a mistake by me. New to this programming thing.
Thank you again.
This made everything OK
#!/usr/bin/perl
print "Enter a number\n";
chomp($a=<stdin>);
print "Enter second number\n";
chomp($b=<stdin>);
print "Enter third number\n";
chomp($c=<stdin>);
$big=0;
$equal=0;
if($a == $b){
$big = $a;
$equal = $a;
}
elsif($a > $b){
$big=$a;
}
else{
$big = $b;
}
if($equal == $c){
print "All numbers are same";
}
elsif($big < $c){
$big = $c;
print "The biggest number is $big \n";
}
| [reply] [d/l] |