Problem

Hi,guys.my girlfriend give me this question,please any one to help me on how to start.
1.Write a program to compute the rate of growth,expressed as % of an
insect population,take as input the initial size of the population and
its size,one week later,then compute the rate of growth and predict
the size of populate in yet another weeks,assuming that growth
continous at the same rate.
I hope that's all she gave you.
i wrote this but it give me 0% percentage rate:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*===================For Sweetie of Mine======================*/

#include<iostream>
using namespace std;

int main() {
//Tuna declare variable zote hapa kwanza
	int population = 100;
	int populationSize = 200;
	int weeks;
	int rate;
	int ratePercent;
//Part One==============
//Here program will calculate rate% of growth
	 rate=populationSize-population;
	 ratePercent=(rate/populationSize)*100;
	 cout<<"Rate in % of growth is: "<<ratePercent<<"%"<<endl;
//Part two==============
//Process
	 cout<<"For how many weeks you need to predict population?"<<endl;
     cin>>weeks;
	 int size_for_weeks=(rate*weeks)+population;
//The calculation was easy because growing rate was the same for any week. ref.QTN
	 cout<<"The size will be " <<size_for_weeks<<" insects for "<<weeks<<endl;
//Futa 'system("PAUSE")' kama unatumia codeBlock
	 system("PAUSE");
	 return 0;

}
Due to integer division (rate/populatioSize) actually results to 0, and anything multiplied with 0 is 0.

EDIT* ... Therefore you have to change your variables to double/float.
Last edited on
I'm afraid it's impossible to write this program without any bugs. 8^D
I'm afraid it's impossible to write this program without any bugs. 8^D

What?
Don't forget you can add your own debug output (or use a debugger) to help you trace through the program and find the problem areas. Divide and conquer.
Last edited on
Mazd: are you sure your girlfriend isn't your teacher and this is a homework assignment?
 
Mazd: are you sure your girlfriend isn't your teacher and this is a homework assignment? 


lol
Last edited on
yah.am sure,she found this problem in some c++ book

i got what i need after writing
1
2
3
ratePercent=(rate*100)/populationSize
//instead of 
ratePercent=(rate/populationSize)*100;


thanks guys
i'd still say you have to convert your ints into floats/doubles...
what if the rate is something like 1 and the populationsize is 3? you'd get a decimal number,
which would automatically be converted into an integer. In this example that wouldn't happen (since .33 is rounded downwards), but with other examples that could give wrong results :)

and its all just changing a few words :P
thanks-its real!
I'm afraid it's impossible to write this program without any bugs. 8^D

What?


A joke. Insect population... Bugs... Get it???
Oh dear lol...
Topic archived. No new replies allowed.