ijustdidit2011 Posted March 2, 2008 Share Posted March 2, 2008 Im trying to get the average of these scores for school could some one tell me what im doing wrong? Thanks! <?php //mainline $scores=array("6.7","8.1","5.8","7.0","6.6","6.0","7.6","6.1","7.2","7.0"); $judges=array("America","Germany","France","China","Japan","Russia","Chek Republic","Trinidad","Cuba","Jamaica"); $average=0; doCalc($scores, $average); //end mainline function doCalc($scores, $average) { $scores[$average] = $scores[$average] / 10; $average = $scores[$average]; } print("The scores are: "); for($i=0; $i<10; $i++) { print("\n\n{$judges[$i]}"); print("\n{$scores[$i]}"); } print("\n\nThe Average score is $average"); ?> Quote Link to comment Share on other sites More sharing options...
maexus Posted March 2, 2008 Share Posted March 2, 2008 You're numbers are strings in the array. The first thing I might try is: $scores = array(6.7, 8.1, 5.8, 7.0, 6.6, 6.0, 7.6, 6.1, 7.2, 7.0); Quote Link to comment Share on other sites More sharing options...
QuietWhistler Posted March 2, 2008 Share Posted March 2, 2008 You are not returning any values in your function, so the $scores and $average variables are terminated at the closing } of your function. To prevent this you could pass the parameters like this: function doCalc( &$scores, &$average ) This is called passing variables by reference. It means that everything you do to those variables in your function, will stay that way, even when the function is closed. Quote Link to comment Share on other sites More sharing options...
shocker-z Posted March 2, 2008 Share Posted March 2, 2008 Try this mate. <?php //mainline $scores=array(6.7,8.1,5.8,7.0,6.6,6.0,7.6,6.1,7.2,7.0); $judges=array("America","Germany","France","China","Japan","Russia","Chek Republic","Trinidad","Cuba","Jamaica"); $average=0; $average=doCalc($scores, $average); //end mainline function doCalc($scores, $average) { $total=0; foreach($scores as $score) { $total=$total + $score; } $result=($total / 10); return $average; } print("The scores are: "); for($i=0; $i<10; $i++) { print("\n\n{$judges[$i]}"); print("\n{$scores[$i]}"); } print("\n\nThe Average score is $average"); ?> Set it to loop through the array and add it all up and then return the average. Regards Liam Quote Link to comment Share on other sites More sharing options...
ijustdidit2011 Posted March 2, 2008 Author Share Posted March 2, 2008 thanks guys! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.