0

Below program is giving different output in codeblocks and visual studio for same input

Input: 5 4 1 2 1

codeblocks

output: 0.00000000

#include<bits/stdc++.h>
using namespace std;

int main()
{
    double l,d,v,g,r;
    cin>>l>>d>>v>>g>>r;
    if(g*v>d) printf("%.8lf\n",(double)l/v);
    else
    {
        printf("%.8lf\n",ceil(d/v/(g+r)) * (g+r)+(l-d)/v);
    }
    return 0;
}

visual studio

output: 7.00000000

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;

int main()
{
    double l, d, v, g, r;
    cin >> l >> d >> v >> g >> r;
    if (g*v > d) printf("%.8lf\n", (double)l / v);
    else
    {
        printf("%.8lf\n", ceil(d / v / (g + r)) * (g + r) + (l - d) / v);
    }
    return 0;
}

Is it due to headers or something else

3
  • What compiler does codeblocks use? Commented Apr 24, 2019 at 4:17
  • #include<bits/stdc++.h> is a bad practice Commented Apr 24, 2019 at 4:17
  • when comparing compilers, please use exactly same code whenever possible. Commented Apr 24, 2019 at 4:43

1 Answer 1

1

First of all, don't use

#include <bits/stdc++.h>

See Why should I not #include <bits/stdc++.h>? for details.

Secondly, the output from Visual Studio looks correct to me.

Update your code for CodeBlocks to use the same as the what you have used in Visual Studio and give it another try. Perhaps use of #include <bits/stdc++.h> has some unwanted side effects.

If that does not resolve your problem, add some diagnostic output to figure out where things could be going wrong. E.g.

int main()
{
   double l, d, v, g, r;
   cin >> l >> d >> v >> g >> r;

   printf("l: %f\n", l);
   printf("d: %f\n", d);
   printf("v: %f\n", v);
   printf("g: %f\n", g);
   printf("r: %f\n", r);
   printf("\n");

   printf("(d / v / (g + r)): %f\n", (d / v / (g + r)));
   printf("ceil(d / v / (g + r)): %f\n", ceil(d / v / (g + r)));
   printf("ceil(d / v / (g + r)) * (g + r): %f\n", ceil(d / v / (g + r)) * (g + r));
   printf("(l - d) / v: %f\n", (l - d) / v);
   printf("ceil(d / v / (g + r)) * (g + r) + (l - d) / v): %f\n", ceil(d / v / (g + r)) * (g + r) + (l - d) / v);

   return 0;
}

Useful link: How to debug small programs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.