1

My operating system : Windows 64 bit

Under codeblocks : When I go to Settings -> Compiler -> The selected compiler is "GNU GCC Compiler"

But , under the tab toolchain executables

I see the following information :

Compiler's Installation directory : C:\Program Files (x86)\CodeBlocks\MinGW

C compiler : mingw32-gcc.exe

C++ compiler : mingw32-g++.exe

I am trying to run the following C++ program :

#include<iostream>
#include<math.h>
using namespace std;
int find_size(int a,int b)
{
    int lena=0;
    int lenb=0;
    while(a>1)
    {
        lena++;
        a/=10;
    }
     while(b>1)
    {
        lenb++;
        b/=10;
    }
    if(lena%2!=0)
        lena+=1;
    if(lenb%2!=0)
        lenb+=1;
    return (lena>lenb?lena:lenb);
}
int karatsuba (int x , int y)
{
    if(x<=10 && y<=10)
        return x*y;
    int n=find_size(x,y);
    int p1 = pow(10,n/2);
    int p2 = pow (10,n);
    int a = x/p1;
    int b = x%p1;
    int c = y/p1;
    int d = y%p1;
    int pdt = p2*karatsuba(a,c)+p1*(karatsuba(a,d)+karatsuba(b,c))+karatsuba(b,d);
    return pdt;
}
int main()
{
    int x,y;
    cout<<"Enter the numbers"<<endl;
    cin>>x>>y;
    int ans=karatsuba(x,y);
    cout<<"\n Product : "<<ans<<endl;
    return 0;
}

The same code when run on an online compiler , gives me the correct output . But , codeblocs gives me a different output . What could be the issue with my compiler ? The same problem with my local Visual Studio code . It gives the same wrong output.

Example : Inputs , x =2023 , y=3003

Correct answer(And the output of online compiler) : 6075069

But My codeblocks' and VSC's output : 6132081

I have no idea what is wrong with my Codeblocks, VSC or my compiler . I've been using the same codeblocks for years and I have never encountered such a problem .

One information I can provide is that a few weeks back I installed MinGW , could that have caused a problem with my compiler settings ?

Can someone please help me rectify this problem ?

AnonymousMe
  • 509
  • 1
  • 5
  • 18
  • 1
    Floating-point calculation may coutain some errors. What if you use `int p1 = pow(10,n/2) + 0.5; int p2 = pow (10,n) + 0.5;` instead of `int p1 = pow(10,n/2); int p2 = pow (10,n);`? – MikeCAT Sep 25 '20 at 13:25
  • 1
    Two different compiler give you unexpected output and you still think the issue is with the compilers? Can I suggest that perhaps it's your code. – john Sep 25 '20 at 13:25
  • Remove code until you find the cause of discrepancy. For the code presented, I only get `6075069`. What if you remove `cin>>x>>y;` and hardcode the values in the code (makes it easier to test!)? – KamilCuk Sep 25 '20 at 13:30
  • 1
    @john , if the problem is my code , how does it work in the online compiler ? – AnonymousMe Sep 25 '20 at 14:09
  • 1
    @KamilCuk I tried hardcoding the values . Sadly , I still get the wrong output. – AnonymousMe Sep 25 '20 at 14:09
  • @MikeCAT , great answer ! I made the change and it seems to work . But do you have any idea why this works fine in one compiler(online) and not in codeblocks? What could be the problem in my system that I must address ? – AnonymousMe Sep 25 '20 at 14:12

0 Answers0