Implement operator calculations

Pages: 12
Hey folks, I'm trying to find out how to implement the actual calculation in operator overloading using friend functions. So far I manage to implement the friend functions in header file and now kind of stuck in implementation part. I try to implement this but this is absolutely non-sense cause it'll do the addition or subtraction in each index element of array not the whole number so if I put 5 + 1 answer is 6 and it shows me six which is right but if I put 16 plus 14 it shows me this 2: instead of adding the actual 14 and 16 and come up with 30 it adds the first index of two array operands (n1 and n2) add them to gather and show me 2: which is not right. Any new ideas or new implementation is appreciated.
Last edited on
I have not understood what you need. Do you need to add coresponding elements of two arrays and write the result in a third array?

If so then you can write

1
2
3
4
5
6
7
Bigint operator +(const Bigint& number1, const Bigint& number2) {

        Bigint result;
        for (int i = 0; i < DIGITS; i++)  result.numerals_[i] = number1.numerals_[i] + number2.numerals_[i];

        return result;
}


Or you can use standard algorithm transfrom

1
2
3
4
5
6
7
8
9
Bigint operator +(const Bigint& number1, const Bigint& number2) {

        Bigint result;
        std::transform(  number1.numerals_, number1.numerals_ + DIGITS,
                         number2.numerals_, result.numerals_,
                         [] ( int x, int y ) { return ( x + y ); } );

        return result;
}

Last edited on
Hey Vlad, I did the same thing (just like your first solution). basically what I meant was when you are adding elements of two arrays together and then strong them in a third array there might be problems which occurs as an overflow or good old remainder stuff so assume that in index 0 of array a you have 3 and in index 0 of array b you have 6 when you are adding these element of the same index together you'll store them as 9 in index 0 of the array c. but what if index 0 and 1 of array a contains 14 and index 0 and 1 of array b contains 16 and then you end up saving 30 in index 0 of array c now my arrays doesn't perform addition the way you think they do cause 14 is stored in two indexes (0 and 1) not one index (just 0) so when it adds it self with 16 (which is index 0 and 1) in array b the result in array c is not 30 in just one index and it add the 1 of 16 and 1 of the 14 in index 0 off array c and then add 6 and 4 to index 1 of array c but since 6 + 4 is 10 it overflows because it is two digits and each index in my array is capable to hold one digit. I hope u don't get more confused than I thought!
Last edited on
I have not understood again.

Either you want to add corresponding elements of two arrays and write the result in corresponding elements of a thurd array. Or you want to add all elements of two arrays and write the result into one element of a third array.
In your example 14 is stored in two elements of the first array/ And what is the problem? So the result also will be in two elements of the third array.

a[0] + [b[0] = c[0]
a[1] + b[1] = c[1]

If you bother about overflows then the third array shall be at least one element greater than the size of any of the first two array and you shall add elements starting from last elements.
Either you want to add corresponding elements of two arrays and write the result in corresponding elements of a thurd array.
(Negative).

Or you want to add all elements of two arrays and write the result into one element of a third array.
(Affirmative)


If you bother about overflows then the third array shall be at least one element greater than the size of any of the first two array and you shall add elements starting from last elements.


I did start from end to beginning but the problem yet stands!

let me simplify my meaning:

define DIGITS 2

char arr1 [DIGITS] = {'5', '6'};
char arr2 [DIGITS] = {'1', '7'};

In my arrays situation after adding theses arrays together and store them in third array it should be like this:

int arr3 [DIGITS] = {'6', '1', '3'} // out of bounds!

now first the out of bounds problem second for some reason even with manually expanding the array size the result of this expression 14 + 16 is 2: where (:) is just some funny character occupies the array element! (so addition doesn't occur properly for second index in both arr1 and arr2 since adding values resulted to be more than two digits)!


Last edited on
When use std::vector instead of arrays.
Last edited on
Not a solution!
Why?! The problem with overflow will be resolved.
Here is a demonstrrative code of the idea


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{
	std::vector<int> v1( 10 );

	std::iota( v1.begin(), v1.end(), 0 );
	std::random_shuffle( v1.begin(), v1.end() );

	std::copy( v1.begin(), v1.end(), std::ostream_iterator<int>( std::cout, " " ) );
	std::cout << std::endl;

	std::vector<int> v2( 20 );
	std::iota( v2.begin(), std::next( v2.begin(), 10 ), 0 );
	std::iota( std::next( v2.begin(), 10 ), v2.end(), 0 );
	std::random_shuffle( v2.begin(), v2.end() );

	std::copy( v2.begin(), v2.end(), std::ostream_iterator<int>( std::cout, " " ) );
	std::cout << std::endl;

	std::vector<int> v3( std::max( v1.size(), v2.size() ) );

	std::copy( v1.begin(), v1.end(), v3.begin() );
	int overflow = 0;

	std::transform( v3.begin(), v3.end(), v2.begin(),
                        v3.begin(),
	                [&] ( int x, int y ) -> int
                        {
	                    int digit = x + y + overflow;
		            overflow = ( digit > 9 ) ? 1 : 0;
	                    digit %= 10; 
			    return ( digit );
                        } );
	if ( overflow ) v3.push_back( overflow );

	std::copy( v3.begin(), v3.end(), std::ostream_iterator<int>( std::cout, " " ) );
	std::cout << std::endl;
}



The output is

8 1 9 2 0 5 7 3 4 6
1 4 4 6 3 0 1 8 2 5 2 6 5 9 3 0 9 8 7 7
9 5 3 9 3 5 8 1 7 1 3 6 5 9 3 0 9 8 7 7
Last edited on
closed account (DSLq5Di1)
Wouldn't you need to add the numbers from right to left?
@sloppy9
Wouldn't you need to add the numbers from right to left?


It is not important how to add the numbers whether from right to left or from left to right because you always can obtain the reverse order of a container.:)
Last edited on
closed account (DSLq5Di1)
I'm fairly sure it is important, those numbers you've output do not add up correctly.

                    8 1 9 2 0 5 7 3 4 6
1 4 4 6 3 0 1 8 2 5 2 6 5 9 3 0 9 8 7 7  +
---------------------------------------
1 4 4 6 3 0 1 8 2 6 0 8 5 1 3 6 7 2 2 3  =
@sloppy9

I'm fairly sure it is important, those numbers you've output do not add up correctly


One more it is unimportant because you can use any container in reverse order. Why did you decide that the adding shall be as

8 1 9 2 0 5 7 3 4 6
1 4 4 6 3 0 1 8 2 5 2 6 5 9 3 0 9 8 7 7 +
---------------------------------------
1 4 4 6 3 0 1 8 2 6 0 8 5 1 3 6 7 2 2 3 =


Who did say you that the least significant digit is in the right side?! Can not you imagine that the least significant digit is in the left side, can you? And what about reverse iterators?
Last edited on
closed account (DSLq5Di1)
I was just trying to illustrate, that your code does not output the expected result. As you say though, it is easily fixed by reversing the algorithm.
Thanks Vlad, I didn't mean that it not a solution as whole, your vector solution is good but I think the problem is easy to solve without vectors.

Sum of int array elements are stored as char that's why digit length is important in each array elements. for instance result of this 9 + 5 produce14 the second digit should be in the carry variable to be carried to the next digits addition and on and on and on until we reach the maximum iteration. I just want a simple solution to get this carry working. This code is fairly right but it only works for addition with result of one digit for instance 4 + 5 which is 9 no these kinds of addition 9 + 1, 5 + 9 and etc with two digits as result (second digit of these kinds of results should be in carry if I'm not mistaken.
Last edited on
Any Ideas folks?

@sloppy9
I was just trying to illustrate, that your code does not output the expected result


Why did you decide that your result is expected?! One more why did you decide that the less significant digit is in the right side?!

@johncplusplus
How to assign that overflowed digit (second one) into the carry?


I already showed that in my example

1
2
3
	                    int digit = x + y + overflow;
		            overflow = ( digit > 9 ) ? 1 : 0;
	                    digit %= 10; 


Applied to your code it will look the following way

1
2
3
int digit = number1.numerals_[i] + number2.numerals_[i] + carry;
result.numerals_[i] = digit % 10;
carry = digit / 10;

Last edited on
thanks Vlad :) it worked but carry needs to be 1 (carry = 1), subtracting algorithm is a bit harder cause if we subtract 555 - 499 and we start to subtract from right to left and since 99 in 499 is bigger than 55 in 555 we need to do it just the way we do it on paper (adding ten to digits with smaller values and subtract one from its left neighbor). what do you think about this, how to manipulate the carry in subtract operator to make it working for subtractions? its sort of interesting.
Last edited on
@johncplusplus
it worked but carry needs to be 1 (carry = 1),


carry = digit / 10; gives that carry equal to either 1 or 0.

As for the substruction then you can use the following approach

left -= carry;
carry = left < right;

where left is a digit from the first number and right is a digit from the right number

result_digit = ( carry ) ? left + 10 - right : left - right;
This is the vector way of doing it, eh? how to do it with array way then?
Last edited on
Pages: 12