Eardrum has asked for the wisdom of the Perl Monks concerning the following question:
Hey guys! Edited: Write a program that calculates the average number from 1 to the number that the user inputs (in jumps of 2) and prints it to the screen.
Here is the fixed code - it works perfectly - but would you guys have done anything differently?example: input: Please enter a number 6 output: (1+3+5)/3 = 3
Second question: Write a program that prints (in a downgrading order - 10 each time) the numbers, starting with the number that the user inputs (until it reaches 0).print "Please enter a number: \n"; my $limit = <STDIN>; chomp $limit; my $sum = 0; my @array = (); for (my $i = 1; $i <= $limit; $i+=2) { $sum += $i; push(@array,$i); #Just a note: print "\n i: $i, sum: $sum, array: @array, scalar: ", scalar(@ +array), "\n"; } my $x = scalar(@array); my $average = $sum/$x; my $final = join("+",@array); print "\n($final)/$x=$average\n";
Here's my code (would you guys have done anything differently?):example: input: Please enter a number 52 output: 52 42 32 22 12 2
Third question: Please enter a program that receives 4 number from the user. The program will put in an array only the numbers that are above 50. Print to the screen the array variables, seperated by "-".print "Please enter a number: \n"; my $limit = <STDIN>; chomp $limit; my $number = $limit; my @array = (); for (my $i = 0; $i <= $limit; $i+=10) { push(@array,$number); $number -= 10; } print "@array\n";
I wrote the code using "if", and it works as it should - but I'm required to use "for" loop. Can't seem to figure it out.example: input: Enter 4 numbers: 20 55 72 100 output: 55-72-100
Fourth question: Write a program that receives 4 numbers from the user, and print to the screen only the even numbers (use operator % and "for" loop):print "Please enter 4 numbers: \n"; my @numbers = <STDIN>; chomp @numbers; my @array = (); if ($numbers[0] > 50) { push (@array,$numbers[0]) } if ($numbers[1] > 50) { push (@array,$numbers[1]) } if ($numbers[2] > 50) { push (@array,$numbers[2]) } if ($numbers[3] > 50) { push (@array,$numbers[3]) } my $final = join("-", @array); print "$final\n";
-Same as the previous question. ~~~~~~~~~~~~~~~~~~~~~~~ Fifth question: Write a program that receives from the user a number from the command line (@ARGV). The program will print to the screen a "triangle" with the same size as the number from the input:example: input: Please enter 4 numbers: 1 2 4 9 output: 2 4
-With this one I don't even know where to start. Help would be much appreciated. Thank you!example: input: 6 output: * ** *** **** ***** ****** input: 3 output: * ** ***
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: New Perl user - help with my homework
by haukex (Archbishop) on Dec 24, 2018 at 12:24 UTC | |
|
Re: New Perl user - help with my homework
by haj (Vicar) on Dec 24, 2018 at 11:55 UTC | |
|
Re: New Perl user - help with my homework
by Corion (Patriarch) on Dec 24, 2018 at 11:27 UTC | |
|
Re: New Perl user - help with my homework
by soonix (Chancellor) on Dec 24, 2018 at 12:24 UTC | |
by Eardrum (Initiate) on Dec 24, 2018 at 14:53 UTC | |
|
Re: New Perl user - help with my homework
by BillKSmith (Monsignor) on Dec 24, 2018 at 19:03 UTC | |
by Eardrum (Initiate) on Dec 25, 2018 at 10:34 UTC | |
|
Re: New Perl user - help with my homework
by choroba (Cardinal) on Dec 25, 2018 at 21:27 UTC | |
|
Re: New Perl user - help with my homework
by soonix (Chancellor) on Dec 27, 2018 at 15:43 UTC |