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

humblefool
  • 31
  • 4

1 Answers1

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270