Question

Really Need Help Since The bug Is minor And is Hardly of 6-7 code lines.. I have Attached the .cpp file link which is well commented to explain the error occurrence In the code1 [click her for code].. Please Do help since im stuck on this since about an hr or more.. Need to Submit This iN college tomorrow!

/*Multiplication Of 2-Bit Binary Numbers Without Using Multiplication Operator*/

/*
    1   1
    1   1
    -----
    1   1
1   1   0
---------
(By Full Adder)
1   0   0   1

*/

#include<stdio.h>
#include<conio.h>

void main()
{

    int k=0,a[4],b[4],i,x,y,temp=0,temp1=0,temp2=0,ans[2],j=1,c=0,sum=0;

    clrscr();

    printf("Enter The 2-Bit Binary Number(1):\t");
    scanf("%d",&x);

    printf("Enter The 2-Bit Binary Number(2):\t");
    scanf("%d",&y);


    for(i=10;i<=100;i=i*10)
    {
     temp=y%i;
     temp=temp/(i/10);

     if(temp==1)
        {
         ans[j]=x;
        }

     else
        {
        ans[j]=0;
        }

     j++;

    }


    ans[2]=ans[2]*10;


    //Implementing Full Adder

    for(i=10;i<=10000;i=i*10)

        {

         //if anyone in comment, correct output

         /* considering second commented and input for x and y as 11
            the output for //1 would be
            0
            1
            1
            0
            0
         */  


        //if both //1 //2 uncommented, wrong output
        /*  0
            1
            0
            1
            0
            0
            0
            0
            0
            0
        */ 


         // 1
         temp2=ans[2]%i;
         temp2=temp2/(i/10);
         printf("%d\n",temp2);

         // 2
         temp1=ans[1]%i;
         temp1=temp1/(i/10);
         printf("%d\n",temp1);


         sum=(temp1^temp2)^c;
         c=(temp1&temp2)|((temp1^temp2)&c);

         b[k]=sum;
         k++;
        }


    for(i=0;i<4;i++)
    {
     printf("%d",b[i]);
    }


    getch();

}
Was it helpful?

Solution

void main()

should be

int main()

Return type of main is int.

Edit:

After analyzing the code, it seemed to be that you should be doing the XOR in a bottom-up order in the last loop. Basically, you're printing the answer in reverse.

so:

for(i=0;i<4;i++)

should be:

for(i=3;i>=0;i--)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top