I was trying to write a code that will tell me the exponential growth using the unit amount of time (round) and population (pop) but ended up with a code that calculates value from the original variable. For example, when I input 10000 for pop and 3 for round, it should first calculate the the %5 of 10000, add it to original value, then calculate again with the new value for 2 more times. I cannot seem to find a way to update pop everytime the for loop is repeated. Can someone help me?
#include <iostream>
using namespace std;
int main() {
int pop, new_people;
int i, round;
cout << "Population: ";
cin >> pop;
cout << "Round: ";
cin >> round;
new_people = (pop * 5) / 100;
for (i = 1; i <= round; i ) {
pop = new_people;
cout << pop;
}
return 0;
}
CodePudding user response:
You need to put the new_people updation inside the for loop so that it gets updated each round.
#include<iostream>
using namespace std;
int main() {
int pop, new_people;
int i, round;
cout << "Population: ";
cin >> pop;
cout << "Round: ";
cin >> round;
for(i = 1; i<=round; i ){
new_people = (pop*5)/100;
pop = new_people;
cout << pop;
}
return 0;
}
CodePudding user response:
You can put "new_people = (pop * 5) / 100" and "pop = new_people" in cycle so as to update the value of "new_people" and "pop".
#include<iostream>
using namespace std;
int main() {
int pop, new_people;
int i, round;
cout << "Population: ";
cin >> pop;
cout << "Round: ";
cin >> round;
for (i = 1; i <= round; i ) {
new_people = (pop * 5) / 100;
pop = new_people;
cout << "round" << i << ": " << pop << endl;
}
return 0;
}