Subtraction in Parts

I want to Perform subtraction in parts
For Example Subtract 1237123 from 2134342
Subtract it By making 3 digits pair and then combine
342-123
134-237
002-001
combine their answers
I need this code
I need this code


What have you coded so far?
But why do you want this? Can you be more specific with your requirements, do you want just the computation or you want 3 vectors holding (1) first number strings (2) second number strings (3) resultant number strings?

I'll try it anyway a bit later.
# inlcude <iostream>
using namespace std;
int main()
{
int array1[5]={1232144,23421,23423423,6799679,3254322};

int array2[5]={989769,567567,546543,3245,6436436};

//Overload operator and subtract these values and store in an array

return 0;

}
Hope this was what you wanted. If you want changes let me know. And also can you please tell us what the application of this is, maybe we can help you with a better way..

Oh and also if anybody can find fault/improve my code then feel free to correct me I would appreciate it.
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main() {

	int first_number, second_number, digit = 0, number;
	std::vector <int> first_number_list;
	std::vector <int> second_number_list;
	std::vector <int> result_list;

	std::cout << "Type the first number: ";
	std::cin >> first_number;
	std::cout << "Type the second: ";
	std::cin >> second_number;

	number = first_number;
	while (number > 0) {
		digit = number % 1000;
		number /= 1000;
		first_number_list.push_back(digit);
		digit = 0;
	}
	number = second_number;
	while (number > 0) {
		digit = number % 1000;
		number /= 1000;
		second_number_list.push_back(digit);
		digit = 0;
	}

	//for (auto& i : first_number_list) {
	//	std::cout << i << "\n";
	//}
	//std::cout << "\n\n\n\n";
	//for (auto& i : first_number_list) {
	//	std::cout << i << "\n";
	//}

	std::cout << "(1) Do you want to subtract " << first_number << " from " << second_number << " or\n";
	std::cout << "(2) Do you want to subtract " << second_number << " from " << first_number;
	std::cin >> digit;

	if (digit == 1) {

		for (int i = 0; i < first_number_list.size() || i < second_number_list.size(); i++) {
			if (first_number_list[i] && second_number_list[i]) {
				result_list.push_back(second_number_list[i] - first_number_list[i]);
			}
			else if (second_number_list[i]) {
				result_list.push_back(second_number_list[i]);
			}
			else {
				result_list.push_back(first_number_list[i] - (first_number_list[i] * 2));
			}
		}

	}
	if (digit == 2) {

		for (int i = 0; i < first_number_list.size() || i < second_number_list.size(); i++) {
			if (first_number_list[i] && second_number_list[i]) {
				result_list.push_back(second_number_list[i] - first_number_list[i]);
			}
			else if (first_number_list[i]) {
				result_list.push_back(first_number_list[i]);
			}
			else {
				result_list.push_back(second_number_list[i] - (second_number_list[i] * 2));
			}
		}
	}
	else
		std::cout << "Did not recognize input :[";

	if (digit == 1 || digit == 2) {
		std::reverse(result_list.begin(), result_list.end());

		for (auto& i : result_list) {
			std::cout << i;
		}
	}

	std::cin.get();
	std::cin.get();
	return 0;
}


edit: fixed a small mistake in the for loops parameters
Last edited on
Topic archived. No new replies allowed.