Calculating population growth based off of user input. Not getting what I want

I am having trouble getting my population and change look like the results on the assignment. I am not entirely sure what I am doing wrong. Any help would be appreciated.
Screenshot of assignment, my code and results of my code.

In the line:

$change = $newpop - $pop;

What is $pop? You do not show what it has. Oh, wait; maybe there is nothing to show and $pop is a problem.

Look at your code. Simulate what happens. The code calculates $newpop from $curpop. What happens the next time around? It calculates $newpop from $curpop again. What value is in $curpop the second time around?

Also, I am not sure but you might need to convert an integer to a float to do the multiplication (and/or division) with a float result. I am not experienced enough with PHP to know for sure.

Your code should work if you fix those two or three issues. I have tested it but since this is homework I should not give the code.

I see another problem. I think your code will show a population of 1,080 for 2020. Probably you should do the print before the calculation and print the current population instead of the new population. I am not sure about PHP but you probably need to initialize change to 0.

I have a suggestion. Calculate the change first (you can do that) then add the change to the current population to get the new population.

1 Like
  • Calculate Growth Rate once, rather than every loop. It should be a number just north of 1. It doesnt change. You can then multiply your current population by the growth rate. (X + X(Y/Z) can be factored in terms of X)

  • Change will need to be initialized if you flip the print and calculation. Otherwise it’s undefined at the point it’s called.

  • PHP will handle the conversion from int to float, but you will need to round it back out to an integer afterward unless you want fractions of people starting in 2022.

  • I found your homework assignment, formatting and all, on Chegg with a simple google search, multiple times over. Tell your professor they should stop reusing assignments, as students are getting their answers online. I shall now feed the search engine algorithm.

Transcribed image text: Overview: Write a program to project several years of population growth. The user should be able to enter in the number of years, the current population, and the growth rate. Your output screen should print out the numbers the user entered, as well as the results in a table. Style the table as shown below (though exact colors don't have to match). Include a link back to the input form. Format the numbers in your output. Input Form: Fileme Available Actions 61 Delta Opel Open Here 05-put 05- ci per Last Updated May 05 2020 10:07:31 May 03 2020 10:07:31 May 03 2020 10:07:31 Delete Bursa wa Mare Delete Vw Bora wscas -New File hw05 - Population Growth Years to forecast: 10 Current Population: 10000 Growth Rate: 8 Calculatel Output: Filename Last Updated Available Acties s-resultsple bw03-input html May 05 2020 10:11:13 May 03 2020 10 08 45 May 03 2020 10:07:31 Delete Voce Delale Open Here Delete Vw Sensor Med Delet View hrs.css New File Output: Filename Last Updated Available Actions Delete View Source Open In New Tab Open Here Delete View Source Open In New Tab Open Here hw0S-results.phpMay 0S 2020 10:11:13. hwos-input html May 05 2020 10:08:45 hwo5.css May 05 2020 10:07:31 -New File DeleteView Source hw05 - Population Growth Starting population : 1,000 Growth rate: 8.0 To perform another calculation click here. Year Population Change 2020 1,080 80 2021 1,166 86 2022 1,260 93 2023 1,360 101 2024 1,469 109 2025 1,587 118 2026 1,714 127
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.