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.

example: input: Please enter a number 6 output: (1+3+5)/3 = 3
Here is the fixed code - it works perfectly - but would you guys have done anything differently?
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";
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).
example: input: Please enter a number 52 output: 52 42 32 22 12 2
Here's my code (would you guys have done anything differently?):
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";
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 "-".
example: input: Enter 4 numbers: 20 55 72 100 output: 55-72-100
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.
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";
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):
example: input: Please enter 4 numbers: 1 2 4 9 output: 2 4
-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: 6 output: * ** *** **** ***** ****** input: 3 output: * ** ***
-With this one I don't even know where to start. Help would be much appreciated. Thank you!


In reply to New Perl user - help with my homework by Eardrum

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.