Borrow in C++

I am subtracting first number form second by using borrow it is giving right answer from 20th to 15th and wrong values from 15th to 0th
plzzz help me

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# include <iostream>
# include <conio.h>
# include <string>
using namespace std;
int main()
{


	int start = 0;
	string sub1[3];
	string sub2[3];
	int bigint1[3];
	int bigint2[3];
	string s1 = "245469345927452978149";

	string s2 = "927987192697643467899";

	for (int i = 0; i < 3; i++)
	{
		sub1[i] = s1.substr(start, 9);
		start = start + 9;

	}
	
	start = 0;
	for (int i = 0; i < 3; i++)
	{
		sub2[i] = s2.substr(start, 9);
		start = start + 9;

	}
	
	int final1[20];
	int final2[20];
	
	for (int i = 0; i < 3; i++)
	{
		bigint1[i] = stoi(sub1[i]);
	}

	for (int i = 0; i < 3; i++)
	{
		bigint2[i] = stoi(sub2[i]);
	}
	
	int j = 0;

	int l = 20;

	for (int i = 2; i >= 0; i--)
	{

		while (bigint1[i] != 0)
		{
			final2[l] = bigint1[i] % 10;

			bigint1[i] = bigint1[i] / 10;
			l--;
			j++;
		}
	}


	int z = 20;
	for (int i = 2; i >= 0; i--)
	{

		while (bigint2[i] != 0)
		{
			final1[z] = bigint2[i] % 10;
			bigint2[i] = bigint2[i] / 10;
			z--;
		}
	}


	cout << "\n\n";
	for (int i = 0; i <= 20; i++)
	{
		cout << final1[i];
	}


	cout << "\n\n";
	for (int i = 0; i <= 20; i++)
	{
		cout << final2[i];
	}
	
	int final3[20];

	for (int i = 20; i >= 0; i--)    //this for loop is used for subtraction by using borrow but its not giving rigth value from 15th to 0th index
	{
		if (final1[i] >= final2[i])
		{
			final3[i] = final1[i] - final2[i];
		}
		else
		{
			for (int j = i - 1; j >= 0; j--)
			{
				
				if (final1[j] > final2[j])
				{
					final1[j] = final1[j] - 1;
					

					for (int z = j - 1; z <= i; z++)
					{
						if (z == i)
						{
							final1[i] = final1[i] + 10;
							final3[i] = final1[i] - final2[i];
						}
						else
						{
							final1[z] = final1[z] + 9;
						}
					}
					
					break;
				}
				
			}
		
		}
	}                                     
	cout << "\n\n";
	for (int i = 0; i <= 20; i++)
	{
		cout << final3[i];
	}                                     //it is giving write value from 20th to 15th 
	
	_getch();
	return 0;
}
Last edited on
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
#include <cctype>

// true if every character in num_str is a decimal digit
bool is_number( const std::string& num_str )
{
    for( char c : num_str ) if( !std::isdigit(c) ) return false ;
    return true ;
}

// prefix the shorter string with '0's so that a and b are of the same size
void make_same_size( std::string& a, std::string& b )
{
    while( a.size() < b.size() ) a = '0' + a ;
    while( b.size() < a.size() ) b = '0' + b ;
}

std::string minus( std::string a, std::string b )
{
    if( is_number(a) && is_number(b) ) // if both are numbers
    {
        make_same_size( a, b ) ; // make them to be of the same size

        if( a == b ) return "0" ;

        // note that it is guaranteed that '3' > '2' etc.
        if( b > a ) return "error: negative" ; // can't subtract, return error string

        // convert both strings to hold integer values of the digits
        for( char& c : a ) c -= '0' ; // '7' - '0' == 7 etc
        for( char& c : b ) c -= '0' ;

        // perform the subtraction a -= b
        int borrow = 0 ;
        for( int i = a.size() - 1 ; i >= 0 ; --i )
        {
            const int r = a[i] - b[i] - borrow ;

            if( r < 0 ) { /* negative r, there is a borrow */ a[i] = r+10 ; borrow = 1 ; }
            else { /* non-negative r, no borrow */ a[i] = r ; borrow = 0 ; }
        }

        // convert a from integer values back to characters
        for( char& c : a ) c += '0' ; // 5 + '0' == '5' etc.

        // return a after removing leading zeroes
        int pos = 0 ;
        while( !a.empty() && a[pos] == '0' ) ++pos ;
        return a.substr(pos) ;
    }

    return "not a number" ; // not number, return error string
}

int main()
{
    std::string s1 = "245469345927452978149";
	std::string s2 = "555237987192697643467899";

	std::cout << s2 << " -\n" << std::string( s2.size() - s1.size(), ' ' ) << s1
	          << "\n----------------------------\n" << minus(s2,s1) << '\n' ;
}

http://coliru.stacked-crooked.com/a/5172a3319bdfb226
Topic archived. No new replies allowed.