Jump to content


Photo

Help Translating perl to php

perl help php translation perl to php

  • Please log in to reply
1 reply to this topic

#1 RonMoevius

RonMoevius

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 03 April 2014 - 11:56 AM

I was tasked out with converting perl subroutines to php, I'm not even a novice I have no clue how to even code this properly. I was given a four hour deadline please help.

 

print "Enter any amount of test scores to find the average, median and standard deviation\nEnter your numbers below, Press Enter after each set, your last set of digits\nPress CTRL-D to receive your information or CTRL-C to cancel.\n";

 

@gnom = <STDIN>;

 

sub avg {

 

$tot = 0;

foreach $num (@gnom) {

$tot += $num;

$mean =  $tot/(scalar @gnom);

}

print "This the average of your grade are: $mean\n";

 

}

avg;

 

sub med {

@gnom = sort(@gnom);

if(@gnom % 2 == 0 or 1){

$sum = @gnom[(@gnom/2)-1] + @gnom[(@gnom/2)];

$med = $sum/2;

print "The median grade is: $med\n";

}

    

}

med;

sub stdv {

$tot = 0;

$tot1 = $tot;

foreach $num (@gnom) {

$tot += $num;

$m =  $tot/(scalar @gnom);

$tot1 +=($mean-$num)**2;

}

$n2 = $tot1 / (scalar @gnom);

$stderd = sqrt($n2);

print "The standard deviation of your grade is: $stderd\n"

}

stdv;



#2 RonMoevius

RonMoevius

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 03 April 2014 - 12:07 PM

so the way I'm seeing this portrayed is that you start with a function

 

Does this work?

<?php

@gnom = (1..100);

function avg (){

$tot = 0;

foreach $num (@gnom) {

$tot += $num;

$mean =  $tot/(scalar @gnom);

}






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com