Help with maths

This is a discussion on Help with maths within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm new to C++ (only started on saturday), and I'm trying to make my first program that is actually ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    New Zealand
    Posts
    4

    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:

    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++?

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    3
    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. ^_^

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    New Zealand
    Posts
    4
    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.

  4. #4
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    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.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Location
    New Zealand
    Posts
    4
    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.

    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.

  7. #7
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    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?

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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.
    Last edited by Cat; 11-14-2006 at 01:32 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  9. #9
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    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?

  10. #10
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  11. #11
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    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.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You shouldn't worry about how numbers are stored. In a good, portable program, it shouldn't matter.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maths
    By AcerN30 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-03-2008, 01:13 PM
  2. maths???
    By nerdyneo in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2003, 12:04 PM
  3. Is maths REALLY required for programming?
    By FloatingPoint in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 07-12-2003, 01:18 PM
  4. maths in a program
    By anthonye in forum C Programming
    Replies: 2
    Last Post: 05-24-2002, 09:08 AM
  5. More Maths :(
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-20-2002, 09:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21