Perl program needed to predict size of populatio of organisms. Program needs to use loop for output.
1
0
Entering edit mode
10.3 years ago
Sidney • 0
print"Please enter the starting number of organisms:";
$organisms=<STDIN>;
chomp($organisms);
}

print"Please enter the average daily population increase (as a percentage):";
$increase_perc=<STDIN>;
chomp($increase_perc);

if ($increase_perc==0) {}

print"Please enter the number of days they will multiply:";
$days=<STDIN>;
chomp($days);
}

$count=$organisms;
foreach ($days){
    $count++;
    if ($days==1) {
        print"Day\tOrganisms\n\n";
        print"---------------------\n\n";
        print"1\t\t$organisms\n";
    }
    if ($days==2) {
        $result=int(($organisms * $increase_perc)/100)+$organisms;
        $count++;
        print"Day\tOrganisms\n\n";
        print"---------------------\n\n";
        print"1\t\t$organisms\n";
        print"2\t\t$result\n";
    }
    if ($days==3) {
        $result3=int($result)+$organisms;
        print"Day\tOrganisms\n\n";
        print"---------------------\n\n";
        print"1\t\t$organisms\n";
        print"2\t\t$result\n";
        print"3\t\t$result3\n";
    }
    if ($days==4) {
        my$result_4=($line3 + $organisms)+ $organisms;
        $line4=($result + $organisms) * 4;
        print"Day\tOrganisms\n\n";
        print"---------------------\n\n";
        print"1\t\t$organisms\n";
        print"2\t\t$line\n";
        print"3\t\t$line3\n";
        print"4\t\t$line4\n";
    }
}

None of my output is working. Please help

Output should be:

Day          Organisms
-----------------------------
1            2.0
2            3.0
3            4.5
4            6.75
5            10.125
6            15.1875
7            22.78125
population perl • 2.7k views
ADD COMMENT
1
Entering edit mode
10.3 years ago
russhh 5.7k
foreach($days)

is wrong. You could use

while($count < $days){$count++; ... logic ...}

To see why foreach($scalar) is wrong type this example into your command line:

perl -le 'foreach(10){print $_}'

with 10 as a scalar and compare with the following:

perl -le 'foreach(1..10){print $_}'

where the numbers are treated in list context.

Nonetheless, there are other issues that you will have to address..

ADD COMMENT
0
Entering edit mode

Thanks for looking at my question. I have updated the program to reflect the change you indicated: while($count < $days){$count++}

But I'm still not sure how to format the output, without repeating each value over and over?

ADD REPLY
0
Entering edit mode

is this an assignment? It certainly sounds like one, since this problem would typically be solved analytically. I'm afraid I shall not be giving you anymore help

ADD REPLY
0
Entering edit mode

I am simply trying to understand how I can go about solving the problem. Any direction you can point me in would be greatly appreciate. I am not looking for an answer, but a way I can solve this on my own. When you said there are other issues, could you please explain?

ADD REPLY
0
Entering edit mode

Try writing a program using a while loop or a for loop but without any if / else that prints out the folowing (with the header):

THE NUMBERS FROM 1 TO 5
1
2
3
4
5
ADD REPLY
0
Entering edit mode

Maybe you could go through couple of "Introduction to Perl" manuals before trying to solve real problems and asking for help.

ADD REPLY

Login before adding your answer.

Traffic: 2193 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6