212k views
1 vote
Write a program that will predict the size of a population of organisms. The program should ask the user for the starting number of organisms, their average daily population increase (as a percentage, expressed as a fraction in decimal form: for example 0.052 would mean a 5.2% increase each day), and the number of days they will multiply. A loop should display the size of the population for each day.

Input Validation.Do not accept a number less than 2 for the starting size of the population. If the user fails to satisfy this print a line with this message "The starting number of organisms must be at least 2.", display the prompt again and try to read the value . Similarly, do not accept a negative number for average daily population increase , using the message "The average daily population increase must be a positive value ." and retrying. Finally, do not accept a number less than 1 for the number of days they will multiply and use the messag.The number of days must be at least 1.

User Sguha
by
5.7k points

1 Answer

3 votes

Answer:

Here is the C++ program:

#include<iostream> // to use input output functions

using namespace std; //to identify objects like cin cout

int main() { // start of the main() function body

// holds the value for starting number of organisms and number of days

int start_size, days;

/* stores value of daily population increase. The data type is set to double to take value of population increase as a fraction */

double pop_increase;

//prompts user to enter starting no of organisms

cout << "Enter the starting number of organisms: ";

cin >> start_size; //reads the value of starting number of organism

/* if the user enters start size value less than 2 this loop keeps executing until the user enters a value of start_size greater than or equal to 2*/

while(start_size < 2){

//displays message for the user to enter a start_size greater or equal to 2

cout<<"The starting number of organisms must be at least 2"<<endl;

cout << "Enter a number greater than or equal to 2: ";

cin >> start_size; } //reads the value of start_size entered by user

//prompts user to enter value for avg daily population increase

cout << "\\Enter average daily population increase as percentage: ";

cin >> pop_increase; //reads the value of population increase

/* if the user enters pop_increase value less than 0 this loop keeps executing until the user enters a positive value of pop_increase */

while(pop_increase < 0){

//prompts user to enter the valid positive value for population increase

cout<<"The average daily population increase must be a positive value"<<endl;

cout << "Please enter a positive value for population increase: ";

cin >> pop_increase; } //reads the value of pop_increase from user

cout << "\\Enter the number of days: "; //prompts to enter number of days

cin >> days;//reads the value of days input by user

/* if the user enters days value less than 1 this loop keeps executing until the user enters a value of days greater than or equal to 1*/

while(days < 1){

//prompts user to enter a valid value for number of days

cout<<"The number of days must be at least 1"<<endl;

cout << "Please enter a number of days greater than or equal to 1: ";

cin >> days; } //reads days input from user

//prints the message below

cout<<"\\Size of population for each day is given below";

cout << "\\Day\t\tPopulation"<<endl;

//prints the Day and Population separated by tab spaces

for(int i = 1; i <= days; i++){ //loop displays size of population for each day

start_size = start_size * (1 + pop_increase / 100.0);

//calculates size of population of organisms

//displays the values of i and start_size

cout << i << "\t\t" << start_size << endl; } }

Explanation:

the loop variable i starts from 1 and this loop body stops to execute when the value of i exceeds the value of number of days. The loop body has a statement start_size = start_size * (1 + pop_increase / 100.0); that computes the size of a population of organisms. The starting number of organism is multiplied by ( 1 + the increase in population) and this increase in population is divided by 100 to be expressed as a fraction. At the end, the result of this whole equation is stored in start_size variable. The last print statement prints the values of i and values of start_size computed by the above formula.

The screenshot of the program along with its output is attached.

Write a program that will predict the size of a population of organisms. The program-example-1
Write a program that will predict the size of a population of organisms. The program-example-2
User Bryan
by
5.4k points