从Perl的人口随机数组计算平均 [英] Perl calculate average from randomly populated array

查看:129
本文介绍了从Perl的人口随机数组计算平均的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一所学校分配我需要帮助。现在,我知道你在想什么,但我的老师是出城,并没有回答我的电子邮件,我的同班同学不知道如何来解决这个问题。一些指针和提示将不胜AP preciated!

I have a school assignment I need help with. Now, I know what you're thinking, but my teacher is out of town and hasn't answered my e-mails, and my other classmates have no clue how to solve this problem. A few pointers and tips would be greatly appreciated!

分配是这样的:

我们应该创建一个数组,在1-100的范围内20至30随机整数填充它。然后,通过子程序的帮助下,我们应该计算出这些数字的平均值。

We're supposed to create an array, populate it with 20 to 30 random integers in the range of 1-100. Then, by the help of subroutines, we're supposed to calculate the average of these numbers.

当我调用子程序回来了,这使我可以选择接听0或错误的0非法师出现问题。在code我现在是这样的:

The problem occurs when I'm calling the subroutine back, which gives me either answer 0 or the "Illegal division by 0" error. The code I have right now is this:

#! /usr/bin/perl -w

use warnings;
use strict;

# Declaring the scalar variables and arrays.
my @AllNumbers;

# Create random data set of between 20 to 30 measures used for the 100 random intigers.
my $random_number = int( rand( 31 - 20 ) ) + 20;

# Create random data set of 100 intigers.
for ( my $i = 0 ; $i <= $random_number ; $i++ ) {
    $AllNumbers[$i] = int( rand( 100 - 1 ) );
}

# Print the 20 to 30 random numbers (range 1 - 100).
print("Your random numbers are: \n@AllNumbers");

sub average {
    my $ArrayAverage = @_;
    for (@_) {
        my $total += $_;
    }
    return $ArrayAverage / $random_number;
} ## end sub average

# Print the mean/average of the 20-30 numbers.
print( "\n\nThe average of those random numbers is: ", &average, "\n\n" );

现在,正如我所说,我想解决这个问题我自己,我已经花了一整个两天这一点,但似乎无法来解决它,不管我做什么。任何提示将不胜AP preciated。附:应该指出的是,我们不应该使用任何模块/插件。

Now, as I said, I'd like to solve this problem myself, and I've spent an entire two days with this, but can't seem to solve it no matter what I do. Any tips would be greatly appreciated. P.S. It should be mentioned that we're not supposed to use any modules/plugins.

推荐答案

不错的工作,到目前为止!这几乎确定。但你调用你的平均无功能
任何参数(和平均)。试一试像这样:

Nice work, so far! It's almost ok. But you are calling your average function without any parameters (&average). Try it like so:

sub average{
    my @Array_To_Average = @_; # this is now a local copy of the global @AllNumbers
    my $total = 0;
    for (@Array_To_Average) # iterate over the local copy ...
    { 
        $total += $_; # ...and sum up
    }
    return $total / $random_number;

    # instead of $random_number you could also use
    # scalar(@Array_To_Average). That's the number of items
    # in the array, i.e.
    #   return $total / scalar(@Array_To_Average);
}

print ("\n\nThe average of those random numbers is: ", average(@AllNumbers), "\n\n");

请省略&安培; 调用函数时。这是很老的风格。
preFER在大多数其他语言中使用的风格:

Please omit the & when calling functions. That's very old style. Prefer the style used in most other languages:

my $result1 = some_function();
my $result2 = some_other_function($param1, $param2);

这篇关于从Perl的人口随机数组计算平均的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆