-5

Here is my code.

#include<stdio.h>
main()
{
    float H,U,D,F;
    int x=0,i=0;
    scanf("%f %f %f %f",&H,&U,&D,&F);
    while(H>x){
        x=x+U-D;
        U=U-(F/100*U);
        i++;
        printf("%d\t%d\t%2lf\t%2lf\t%2lf\n",i,x,U,D,F);
    }
    printf("%d",i);
}

It has fallen in an infinite loop. What's the problem here?

3 Answers3

0

Depending on what your input values are, an infinite loop might be expected.

If your F is positive, it looks like your U is set up for exponential decay, from its initial value towards 0. If your D is also positive, then at some point x will be consistently decreasing, not increasing: if it fails to exceed H before then, it never will...

You may want to add an additional exit condition, such as if(x<0)

comingstorm
  • 25,557
  • 3
  • 43
  • 67
0

This will fall in the infinite case only in some cases. And it is because of rounding off you should take x as float so you will get appropriate answer. In your case first of all U-D will be computed and let's take a case if U-D=0.65235 Then.. It will be converted to 0 and added to x So X will be as it is.. So your loop will go to infinite state.. I have posted a photo below which can help you to understand the reason..

Yash Ganatra
  • 468
  • 2
  • 12
  • 1
    Yes! Exactly. Thanks. – Rashed Khan Jan 25 '16 at 20:01
  • 1
    @RashedKhan did you even test what he wrote? what he said is not true. In his code `j` will be 1. (after correcting all the errors like writing `Int`and `Float` and `J` capital – Flikk Jan 25 '16 at 20:06
  • I changed 'int x' into 'float x' and I have got my expected output. – Rashed Khan Jan 25 '16 at 20:09
  • 1
    While it is true that equality (==) can be a problem between int and float, the other comparison operators (>, <) should not be. It's probably more the `x=x+U-D` which doesn't work as expected for that reason? Making x a float fixes that, too – Ctx Jan 25 '16 at 20:10
  • I have just given an example even I know that's not right but just to make him understand I took it. – Yash Ganatra Jan 25 '16 at 20:11
  • And sometimes in turbo c++ @Fikk you will get this type of errors – Yash Ganatra Jan 25 '16 at 20:12
  • As I said I don't know the reason may be a bug or something else.. – Yash Ganatra Jan 25 '16 at 20:13
  • So you're saying you guessed without any basis? – dbush Jan 25 '16 at 20:14
  • 1
    @YashGanatra Perhaps you should update your answer to clarify the real root cause of the endless loop? – Ctx Jan 25 '16 at 20:15
  • see this http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm – Yash Ganatra Jan 25 '16 at 20:22
  • http://stackoverflow.com/questions/1161199/is-relational-comparison-between-int-and-float-directly-possible-in-c – Yash Ganatra Jan 25 '16 at 20:23
  • 1
    you should update your answer and say what the real reason is. First of all this is not turbo c++ but c. then the reason that fixed the issue, was because `x=x+U-D` will round down the result since x is int. The problem with comparing floats is doing nothing in this case. it is only a problem if you do `float a=1.23456; if (a==1.23456)` because `a` is 1.2345600128173828125 – Flikk Jan 25 '16 at 20:25
  • @Fikk you are right i was totally at wrong track even my disscussion was right but not for this case – Yash Ganatra Jan 25 '16 at 20:32
  • @RashaedKhan see why my solution worked correct this was the reason i have posted a photo.. – Yash Ganatra Jan 25 '16 at 20:36
  • How did this end up being an accepted answer?! "J=(a==b); Then j will be zero always.." This is completely wrong. J will not evaluate to a false, when float a = 1.0 and int b = 1. Reason: According to IEE754, all integers can be represented exactly as floats, up to about 2^23. We try to avoid comparing ints to floats since it involves a loss of precision. – Debosmit Ray Jan 25 '16 at 21:43
  • @DebosmitRay it ended up this way because changing the integer to float fixed the code. But the reasoning is, like you allready said, completely wrong. The reason it worked out, was because it fixed int rounding that was not wanted `x = x + U - D` which is `int = int + float - float` I allready tried and asked him to change the answer so it's clear. – Flikk Jan 25 '16 at 22:06
  • @Flikk Precisely. I am afraid it will be very misleading for someone unless s/he goes through the comment thread. – Debosmit Ray Jan 25 '16 at 23:09
  • Sorry I have edited my answer.. – Yash Ganatra Jan 26 '16 at 03:51
0

enter image description herethis is the right answer in first case my x is int while in second case it is float

Yash Ganatra
  • 468
  • 2
  • 12
  • in first case first of all U-D is getting counted then it is converted to int and added to x so x will be as it is... – Yash Ganatra Jan 25 '16 at 20:37
  • still your reasoning is wrong. you're saying it is because the while loop compares an int with a float. but that is not the reason it wasnt working. The reason was, that he assigned a float to a int like: `int x = 1.777;` if you now check what is inside x, it will be 1. – Flikk Jan 25 '16 at 21:13
  • I am saying d same firstly U-D will be computed and if its 0.7766633 then 0 will be added to x.. – Yash Ganatra Jan 25 '16 at 21:16
  • So you were right in this case I was wrong but really once I compared 4.0000> 4 nd it was returning 1 – Yash Ganatra Jan 25 '16 at 21:19
  • 1
    @Yash Your reasoning is completely wrong. Just because it works for one case, doesn't mean it will work for all cases. Please refrain from posting such content as an answer. It can be a comment or an addendum to a problem, at best. Your previous answer stated something completely wrong and the OP somehow decided to mark it correct just because it worked for his one case! Now that you have further knowledge, you should edit your original answer, instead of arguing how it works in that one case that you are talking about. – Debosmit Ray Jan 25 '16 at 23:14