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 message "The number of days must be at least 1."

Respuesta :

Answer:

// using c++ language

#include "stdafx.h";

#include <iostream>

#include<cmath>

using namespace std;

//start

int main()

{

 //Declaration of variables in the program

 double start_organisms;

 double daily_increase;

 int days;

 double updated_organisms;

 //The user enters the number of organisms as desired

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

 cin >> start_organisms;

 //Validating input data

 while (start_organisms < 2)

 {

     cout << "The starting number of organisms must be at least 2.\n";

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

     cin >> start_organisms;

 }

 //The user enters daily input, here's where we apply the 5.2% given in question

 cout << "Enter the daily population increase: ";

 cin>> daily_increase;

 //Validating the increase

 while (daily_increase < 0)

 {

     cout << "The average daily population increase must be a positive value.\n ";

     cout << "Enter the daily population increase: ";

     cin >> daily_increase;

 }

 //The user enters number of days

 cout << "Enter the number of days: ";

 cin >> days;

 //Validating the number of days

 while (days<1)

 {

     cout << "The number of days must be at least 1.\n";

     cout << "Enter the number of days: ";

     cin >> days;

 }

 

 //Final calculation and display of results based on formulas

 for (int i = 0; i < days; i++)

 {

     updated_organisms = start_organisms + (daily_increase*start_organisms);

     cout << "On day " << i + 1 << " the population size was " << round(updated_organisms)<<"."<<"\n";

     

     start_organisms = updated_organisms;

 }

 system("pause");

  return 0;

//end

}

Explanation: