Carry-on

hi uhmm i have a question
what statement do i use in making a program that reads the total carry operation used in adding numbers?

this is suppose to be the out put:
output
555+555 3 carry operation
123+456 0 carry operation
146+932 1 carry operation
632+439 2 carry operation


can any one help me whit this program?
There is no such statement, because the computers do not use decimal numerals to represent their values. You have different options:

- Search in google for "extracting digits from a number" to see how to take the digits from the two numbers using division and remainder. Then perform addition with carry manually in your program using those digits.
(You can also study the idea behind positional numerals if you want, say here: http://en.wikipedia.org/wiki/Positional_notation)

- You can use the stringstream library class to perform the conversion to decimal numeral automatically and then perform the addition with carry yourself.
http://www.cplusplus.com/reference/iostream/stringstream/
Example code:
1
2
3
4
    int number = 555;
    ostringstream convertor;
    convertor << number;
    string numeral(convertor.str());

Note: the numeral string will contain the digits of the numeral as characters - i.e. it will contain '0', not 0.

- To use stringstream as before, but determine the number of carries with inner_product and custom function object:
http://www.cplusplus.com/reference/std/numeric/inner_product/
Example code:
1
2
3
inner_product(augend_numeral.rbegin(), augend_numeral.rend(),
                             addend_numeral.rbegin(), 0,
                             plus<int>(), CarryDetector())

CarryDetector should be function object that you have written to test two (character) digits and return 1 if there is carry and 0 if there isn't. It must also remember the carry (or lack of carry) from the last operation.

Hint:
1
2
3
4
5
6
7
8
struct CarryDetector {

    //define members here ... like carry info

    int operator() (char augend_digit, char addend_digit)
    {
    }
};

If you don't know function objects use the second solution. If you don't know stringstream try looking up in google for the first method. This have been done many times before.

Regards
uhm i mange to know how it works but i kinda run in to some errors can any one help me out on fixing this?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>                                                                                                                                                                                    #include<iostream>
#include<fstream>
using namespace std;
int dec2bin(int dec1) 
{ 
    int bin=1110,pos=1110; 
    while(dec > 0){
    bin=bin+(dec%555)*pos; 
    dec = dec / 3; 
    pos *= 10; 
    } 
return dec; 
} 
main(){
       int i=1,x;
       ifstream fileko("carry.txt");
       while(fileko>>i){
       x=dec2bin(i);
       cout<<x<<endl;}
       cin.get();
       return 0;                                                                                    
i mange to know how it works
I'm glad you did, because I certainly don't. I really don't understand why you apply the dec2bin function. If nothing else, the name suggests that it is intended for converting numbers from the decimal to binary in one sense or another. And I don't see how that fits with your problem. I have hard time understanding dec2bin as it is.

Let me explain how to extract the decimal digits out of some variable that stores a number.

Positional numerals are those written in the form: d_n...d_2d_1d_0, where d_0...d_n are the digits, as in 1234 (one thousand two hundred and thirty four). Here, the digits are d_0 = 4, d_1 = 3, d_2 = 2, d_3 = 1. In this case, even though I wasn't explicit, the radix was 10. This means that there are 10 possible digits: 0..9.

Each digit from a numeral corresponds to a quantity. If you know the quantities that correspond to the digits in some positional numeral, and if you know the radix, there is a way to compute (or at least formulate) the quantity that corresponds to the entire numeral.

People have learned to associate decimal numerals with quantities, from memory and from intuition. That is why, to most, when you say numeral or quantity, it is difficult to distinguish between the two ideas. But in computing, the numeral is what denotes a quantity. The numeral is just one way to express the quantity. Like, you can use roman numerals too, and the latter are even not positional. That is, they don't abide the following formula.

If you have the numeral d_n...d_2d_1d_0, and the radix (also called base) was b, then the formula for the corresponding quantity is (you can check in wikipedia):
Number = d_n * (b ^ n) + ... + d_2 * (b ^ 2) + d_1 * b + d_0 = sum { d_i * (b ^ i) }, i = 0..n
Here I use the caret (^) to denote exponentiation. In our case the radix is 10, but that is not particularly interesting.

What you need though, is the inverse. Given the quantity Number, to find the quantities for the digits that would be part of its positional numeral. Again, you have to know the radix (10 in this case).

For this purpose, you need two operations. One that shifts the quantity to the right, and one that extracts d_0 every time (called the least significant digit).

Shifting right means that you take the original quantity that has some representation d_n...d_2d_1d_0 and produce another quantity that has representation d_n...d_2d_1. That is, you produce the quantity that would result if you stripped the least significant digit from the corresponding numeral. This is easy to do, because:
Original = d_n * (b ^ n) + ... + d_2 * (b ^ 2) + d_1 * b + d_0
Shifted = d_n * (b ^ (n - 1)) + ... + d_2 * (b ^ (2 - 1)) + d_1
The last formula you should check. Anyways, this means that Shifted = Original / b. It is important that the last division is integer division. Or in terms of ordinary division: Shifted = floor(Original / b).

Taking the last digit is also easy. You have that:
Original % b
= (d_n * (b ^ n)) % b + ... + (d_2 * (b ^ 2)) % b + (d_1 * b) % b + d_0 % b
= 0 + ... + 0 + 0 + d_0 = d_0
Here % is the remainder operation, as in C++. So, you have Original % b = d_0. You should note that this works partly because the last digit d_0 is quantity smaller than the base. For example, in base 10, the digits 0..9 correspond to quantities smaller than 10.

So, having those two operations, you can consecutively extract the least significant digit, then shift the quantity right, then extract the next to least significant digit, etc. In radix 10, this means to first take % 10, which gives you d_0, then to divide by 10, which removes d_0 from the quantity and shifts the numeral to the right. You do this, until there is nothing left to shift right, i.e.
1
2
3
4
5
6
while (n)
{
    d = n % 10;
    n = n / 10;
    //do something with d
}

In your case, it is preferable to simultaneously extract the least significant digits from both numbers. Then add them, see if there is a carry. Increment the carries counter if there is, and also remember if you had a carry or not. In the next step, again add the digits, but also, if you had carry from the last step, add 1. Now, this is elementary school arithmetic rules, so I am sure I don't have to remind you. You can terminate doing this when either quantity had been shifted right to 0, i.e. there is nothing left to extract.

Again, google for details. I even think that you could find this information on your own. But seeing what you came up with, I thought you need some direction.

Regards
Topic archived. No new replies allowed.