Simple serial carry chain simulation using Arrays

Source: Internet
Author: User

# Include <iostream>
Using namespace STD;

Int * array_carry_chain (int * a, int * B, int N );
Int main ()
{
Int A [10] = };
Int B [10] = };
// 1001000100
Int * c = array_carry_chain (a, B, 10 );
If (C! = NULL)
{
For (INT I = 0; I <10; I ++)
{
Cout <C [I];
}
}
Return 0;
}

Int * array_carry_chain (int * a, int * B, int N)
{
If (A [0] + B [0]> 1)
{
Cout <"overflow! "<Endl;
A = NULL;
Return;
}
For (INT I = n-1; I> 0; I --)
{
If (A [I] + B [I]> 1)
{
Int J = I;
Do
{
J --; // obtain the location of the first place

// If the carry has reached the highest bit, and the carry overflows
If (j = 0 & A [J] + 1> 1)
{
Cout <"overflow! "<Endl;
A = NULL;
Return;
}
// The first digit of the chain in which the user stores the returned value is 0. The position 1 is not carried.
If (A [J] = 0)
{
A [J] = 1;
Break;
}
// The first digit of the chain in which the user stores the returned value is 1, and the position is 0, then 1 to the second digit
Else if (a [J] = 1)
{
A [J] = 0;

A [J-1] ++;
}

// The first digit of the chain in which the user stores the returned value is 2 (the first digit is 1, and the first digit of the carry is not processed yet), and the position is 0,

// Enter 1 to the next level

Else
{
A [J] = 1;

A [J-1] ++;
}
} While (A [J-1] <= 1 & J-1> = 0 );
// Save the base value after carry
A [I] = A [I] + B [I]-2;
} // If
Else
{
A [I] = A [I] + B [I];
}
}
Return;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.