Re: Simple Arithmetic Question
by GrandFather (Saint) on Nov 05, 2014 at 22:11 UTC
|
Show us what you have tried so we can tell what you are having trouble with. Doing your homework for you won't help you learn much.
What does "using selection" mean? Is it a module?
Perl is the programming world's equivalent of English
| [reply] |
|
|
"... using selection, determine the largest number of $a and $b[;] using selection, determine the smallest number of $b and $c" seems to imply Selection sort (but with only two numbers, just do inequality tests ... oh, those would already be in the sort algorithm; glad that that's sorted out).
| [reply] |
|
|
| [reply] |
|
|
| [reply] |
Re: Simple Arithmetic Question
by Eily (Monsignor) on Nov 05, 2014 at 22:19 UTC
|
| [reply] |
|
|
| [reply] |
Re: Simple Arithmetic Question
by davido (Cardinal) on Nov 05, 2014 at 22:12 UTC
|
Post the code you've started with so we know where you're stuck. Ask an actual question about the code you're working on.
| [reply] |
Re: Simple Arithmetic Question
by Laurent_R (Canon) on Nov 05, 2014 at 23:22 UTC
|
OK, just to get you started, this one possible way (untested, but hopefully right) to do the beginning:
use strict;
use warnings;
print "Please enter three numbers separated by spaces (complete by hit
+ting the enter key)\n";
my $user_input = <>; # get user input
chomp $user_input; # removing trailing newline from us
+er input
my ($nr1, $nr2, $nr3) = split /\s+/, $user_input; # splitting
+ user input on spaces to get the 3 numbers
print "The sum of the three numbers is equal to ", ($nr1 + $nr2 + $nr3
+), "\n";
print "The product ...
This is not really the way I would personally write such a program, but I tried to make it quite simple for a beginner like you to understand it. Hopefully you should be able to complete by yourself.
I asked the user to enter the three numbers on the same line because I was too lazy to ask 3 times for a number and and get the user input three times in a row, but if you haven't covered yet the split function, you might as well change it to three questions, three standard input captures and three chomp's, once for each of the 3 variables.
I also changed the name of your variables because $a and $b are special variables (used for sorting data) that it would be better not to use for other purposes, although this has probably no importance in the case of your assignment (but it would be better not to get into the habit of using them for something else). But, of course, if your instructor is asking you to use $a, $a and $c, do it in accordance to his or her wishes.
One additional advise: when you'll try to code the division ($a / $c) and the remainder of the division ($a % $b), check before that $c and $b are not zero (to avoid a division by 0 error).
| [reply] [d/l] [select] |
|
|
#!/usr/bin/perl
use Modern::Perl;
use strict;
use warnings;
print "input three numbers separated by spaces\n";
my $user_input = <>;
chomp $user_input;
my ($nr1) = $user_input, "$a";
chomp $user_input;
my ($nr2) = $user_input, "$b";
chomp $user_input;
my ($nr3) = $user_input, "$c";
print "The sum of the three numbers is equal to", ($a+$b+$c),"\n";
| [reply] [d/l] |
|
|
print "input three numbers separated by spaces\n";
my $user_input = <>;
This is good as far as it goes: you're getting something | a string from STDIN that is terminated by a newline.
chomp $user_input;
This is good too, except you do it twice more and this is where you start to go off the rails. chomp removes a newline (if present) from the end of a string (well, it's a little more involved than that, but let it stand for now), $user_input in this case, so doing it repeatedly will not usually get you anything. For learning/development/debugging, I would advise placing a print statement after this step so you can be sure exactly what you are dealing with:
print "A: '$user_unput' \n";
(In general, develop your code step-by-step and use print statements at each successive step to see just what is happening. If weird stuff starts to happen, understand the problem before proceeding.)
my ($nr1) = $user_input, "$a";
This has no meaning. You are assigning $user_input to $nr1 (why?) and interpolating $a (which has no value at this point) into a string, which you then throw away: what do you think is happening here? (Update: Two other, similar statements have the same problem.)
Please consider these and other points raised so far, write some more code and post again.
Update: Do continue to use strict; and use warnings; in your code (use Modern::Perl; includes both of these). They will make learning Perl much easier.
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
Either you get all 3 numbers separated by spaces on one line, chomp it once and then you have to use split or some other mechanism to get the individual numbers (the way I did it in my previous post), or you ask three times for one indivudal number from your user, get three times the input, chomp the input three times.
print "give me a number\n";
my $nr1 = <>;
chomp $nr1;
print "give me a number\n";
my $nr2 = <>;
chomp $nr2;
print "give me a number\n";
my $nr3 ...
| [reply] [d/l] [select] |
Re: Simple Arithmetic Question
by Discipulus (Canon) on Nov 06, 2014 at 15:01 UTC
|
just for fun, and not really aderent to your request(use suggestion from wiser monks) but in TimToady spirit..
perl -MEnglish -e "map { $LIST_SEPARATOR=$_; print qq(@ARGV=), eval qq
+(@ARGV),qq(\n)} qw(+ - * / %)" 1 2 3
1+2+3=6
1-2-3=-4
1*2*3=6
1/2/3=0.166666666666667
1%2%3=1
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
| [reply] [d/l] |