Public Group

# Basic C++ program help.

This topic is 4130 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I have written this program as homework. I've got most of it working and have struggled for past two hours with the last bit of it. Here is the problem: 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, thier average daily population increase(as a percentage of current population),and the number of days they will multiply.A loop should display the size of the population for each day. O.K. so I got most of it but it will not update the increase each day. Math is not my greatest skill right now.
#include <iostream>
using namespace std;

void main(){

int startNum = 0;
double avgDailyIncrease = 0.0;
int numDays = 0;
double newPopulationPercentage = 0.0;
double newPopulation = 0.0;

cout << "Please enter the starting number of organisms ";
cin >> startNum;
while(startNum < 2){
cout << "Please enter a number greater than or equal to 2\n";
cout << "Please enter the starting number of organisms ";
cin >> startNum;
}
cout << "Please enter the average daily Population increase as a percentage\n";
cout << "of the current population ";
cin >> avgDailyIncrease;
while (avgDailyIncrease < 0){
cout << "Please enter a positive number\n";
cout << "Please enter the average daily Population increase as a percentage\n";
cout << "of the current population ";
cin >> avgDailyIncrease;
}
cout << "Please enter the number of days they will multiply ";
cin >> numDays;
while(numDays < 1){
cout << "Please enter a number greater than or equal to one\n";
cout << "Please enter the number of days they will multiply ";
cin >> numDays;
}
newPopulationPercentage = (startNum * avgDailyIncrease) / 100;
newPopulation = newPopulationPercentage + startNum;
cout <<"Day		Population\n";
cout <<"--------------------------\n";

int count = 1;
while(count <= numDays){
cout << count << "\t\t" << newPopulation << endl;
count++;
}
}


##### Share on other sites
while(count <= numDays){
cout << count << "\t\t" << newPopulation << endl;
count++;
}

You're printing out the newPopulation, but you never actually calculate it.

Edit: Or do you know this and just need to know _how_ to do this? Since this is homework, we cannot help too much. However, we can give some points. So, you know the population of the previous day (either newPopulation, or startNum depending on whether it's day one or not). You know the percentage increase each day. So you need to calculate the newPopulation by adding the previous population and the population that was added. Each time you a percentage of the last population.

##### Share on other sites
Moved to For Beginners

##### Share on other sites
You should probably use int main() and return 0 at the bottom. Its the C++ way to do it.

int main()
{

return 0;
}

##### Share on other sites
You are looping over days, so the code that represents things that happen every day should go within the loop over days.

Think: What is the code that represents the calculation of how many organisms there are on a given day? There are a different number for each day, so that calculation should happen every day, right? So where does that code go? Next, when you calculate the number of organisms for the current day, you calculate based off of how many there were previously, right? So, what you need to do is, each time through the loop, multiply the "current number of organisms" by whatever value - before the multiplication, the variable holds the number of organisms from the previous day, and after, the number for the current day.

##### Share on other sites
Thanks for all the help. I managed to get it worked out finally.

##### Share on other sites
Quote:
 Original post by Maverick_24You should probably use int main() and return 0 at the bottom. Its the C++ way to do it.int main(){ //your code here return 0;}

To be a bit pedantic, there's no need to return 0; at the end of main. Main is a special case where if the compiler hits the end of it and doesn't find a return statement, it will by default return 0.

• 11
• 19
• 12
• 9
• 34
• ### Forum Statistics

• Total Topics
631398
• Total Posts
2999840
×