Help with maths

• 11-14-2006
Coritani
Help with maths
Hi, I'm new to C++ (only started on saturday), and I'm trying to make my first program that is actually useful. I'm trying to chart population growth using the formula:

Quote:

P = N(1+R)^A
(Where N is the starting population, R is the growth in %, A is the length of time of the growth, and P is the population after the growth has happened)

Anyway, I want to just start off with it being over 1 year, which eliminates A from the equation.

Here is my code:

Code:

```int X, Y, Z, A, B; int C = 100; cout<<"What is the population that is going to grow? \n"; cin>>X; cin.ignore(); cout<<"\nWhat is the growth rate, in %?\n"; cin>>Y; cin.ignore(); Y/=C; X*(1+Y)=Z; cin.get();```
The declared-but-unused variables may be used by me later on. I used:

Code:

```int C = 100 Y/=C```
To get around the problem of "error C2106: '=' : left operand must be l-value"

But now, with the 'X*(1+Y)=Z' I've hit that problem again, and I don't know how to get around it. If it matters, my compiler is Visual C++ Express Edition.

Anyone know how, or can someone point me towards a guide to doing maths in C++?
• 11-14-2006
Darklighter137
Your best bet would be to learn C++ essentials before you start coding things. There are lots of helpful guides on an array of topics, but they all assume you have a fair handle on C++. Starting on Saturday is only a very small beginning. Learning any language, whether spoken or written, takes a lot of time and effort. ^_^
• 11-14-2006
Coritani
Quote:

Originally Posted by Darklighter137
Your best bet would be to learn C++ essentials before you start coding things. There are lots of helpful guides on an array of topics, but they all assume you have a fair handle on C++. Starting on Saturday is only a very small beginning. Learning any language, whether spoken or written, takes a lot of time and effort. ^_^

Well, this is only going to be 30 lines or so, and is very much a learning project for me.

I got it working, by rearranging it to "Z = X*1+Y" .

But now it's saying that 5 divided by 100 = 0. Which is messing it up. It seems that it doesn't like decimals, nor does it like fractions.
• 11-14-2006
dac
you dont need the cin.ignore(), just the 'cin >>' is adequate. and if you just use '"abcdef? ";' at the end of your questions, the input will be at the end of the question.
• 11-14-2006
vart
Quote:

5 divided by 100 = 0. Which is messing it up
because it is int
you cannot store something like0.05 in the int variable.
use double
• 11-14-2006
Coritani
Quote:

Originally Posted by vart
because it is int
you cannot store something like0.05 in the int variable.
use double

Ah, thanks, I got it working.

I obviously have alot to learn about variables.

Thanks for the help.

Quote:

you dont need the cin.ignore(), just the 'cin >>' is adequate. and if you just use '"abcdef? ";' at the end of your questions, the input will be at the end of the question.
Yeah, I'm going to tidy up the code.
• 11-14-2006
dac
Quote:

Originally Posted by vart
because it is int
you cannot store something like0.05 in the int variable.
use double

that would be a "float" wouldnt it?
• 11-14-2006
Cat
double works too.

float = single precision (typically 32 bit floating-point)
double = double precision (typically 64 bit floating-point)

You may as well use doubles; the FPU on your PC can handle them just as well as single precision floats.
• 11-14-2006
dac
Quote:

Originally Posted by Cat
double works too.

float = single precision (typically 32 bit floating-point)
double = double precision (typically 64 bit floating-point)

You may as well use doubles; the FPU on your PC can handle them just as well as single precision floats.

ok, i take it that using two's complement?
• 11-14-2006
Cat
Quote:

Originally Posted by Coritani
I got it working, by rearranging it to "Z = X*1+Y" .

Yes, every = sign does the following:

1. Evaluates the expression on the right-hand side
2. Assigns the results to the left hand side.

So:

int x;
x = 3; // This is valid
3 = x; // This is not.

It's needed to have some way to figure out what assignment is being made. For example:

int x = 2, y = 3;

x = y; // This sets x = 3.
• 11-14-2006
Cat
Quote:

Originally Posted by dac
ok, i take it that using two's complement?

It's actually IEEE 754, which does not use two's compliment (2's compliment is how integers are stored).

It basically stores numbers with a sign bit, an exponent (in excess-1023) and a mantissa.
• 11-14-2006
jafet
You shouldn't worry about how numbers are stored. In a good, portable program, it shouldn't matter.