5 Replies - 1243 Views - Last Post: 18 November 2012 - 03:10 PM Rate Topic: -----

#1 Sizza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 12

How to increment the result of a calculation inside a loop

Posted 18 November 2012 - 06:53 AM

Hi,
Basically i have a loop, and i need to increase z by 30 every time y-x is greater than or equal to 1000.
X and Y are inputted by the user
the value of Z depends on the difference of x and y,
the loop only works when Y is greater than or equal to X
If there is another way to do this without loops, I'd be happy to consider it.

so for example

4500 - 4000 = 500, so Z = 0
4500 - 3500 = 1000, so Z = 30
2000 - 947 = 1053, so Z = 30
3500 - 1000 = 2500, so Z = 60
4000 - 900 = 3100, so Z = 90
10000 - 500 = 9500, so Z = 270

the answer should be the same for most C languages,
this is what i have - sometimes works but is very hit and miss:


//x and y already inputted 
double z = 0;
while (y >= x)
    {
        y = y - x;
        if (y >= 1000)
        {
            z+=30;
        }

    }



but it doesn't work, even though I've been told it should work
any help?

Is This A Good Question/Topic? 0
  • +

Replies To: How to increment the result of a calculation inside a loop

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,653
  • Joined: 05-May 12

Re: How to increment the result of a calculation inside a loop

Posted 18 November 2012 - 07:18 AM

At what times do you see your loop missing?

Personally, I wouldn't use a loop. I would use some simple integer division and multiplication.
Was This Post Helpful? 0
  • +
  • -

#3 Sizza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 12

Re: How to increment the result of a calculation inside a loop

Posted 18 November 2012 - 08:12 AM

Well, I input 147 as X, and 2560 as Y,
Z should come out as 60, but it comes out as 300 instead.

also if X= 0 and y = anything > x the program just hangs in the loop and doesn't break.
really confusing piece of code
Was This Post Helpful? 0
  • +
  • -

#4 Sizza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 12

Re: How to increment the result of a calculation inside a loop

Posted 18 November 2012 - 08:45 AM

sub = Y - X;

            if (sub >= 1000 && sub < 2000)
                Z += 30;
            if (sub >= 2000 && sub < 3000)
                Z += 60;
            if (sub >= 3000 && sub < 4000)
                Z += 90;
            if (sub >= 4000 && sub < 5000)
                Z += 120;



There has to be a more practical way than this,
i need to deal with any numbers that are input
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,415
  • Joined: 23-August 08

Re: How to increment the result of a calculation inside a loop

Posted 18 November 2012 - 10:58 AM

Have you heard of the modulus operator?
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: How to increment the result of a calculation inside a loop

Posted 18 November 2012 - 03:10 PM

double z = (((int)Math.Abs(x - y)) / 1000) * 30;

If you just care when y > x (instead of the difference no matter which is larger) then use

double z = y > x ? ((int)(y-x)) / 1000 * 30 : 0;

This post has been edited by Momerath: 18 November 2012 - 03:13 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1