SolvedAdding 2 huge numbers using vectors

Posts 16–18 of 18 · Page 2 of 2
Quote Originally Posted by robater View Post
Okay, well I tried to do the whole correct location thing. What am I doing wrong now?

Code:
#include <iostream>
#include <vector>
#include <cctype>
#include <stdexcept>
#include <algorithm>
#include <numeric>
#include <string>
#include <functional>
#include <assert.h>
#include <sstream>
using namespace std;

int addition(string input1, string input2, int &result, vector<int> &largeInt1, vector<int> &largeInt2, vector<int> &finalInt);


int main() {
	vector<int> largeInt1 (12, 0); // initialize to zero
	vector<int> largeInt2 (12, 0); // initialize to zero
	vector<int> finalInt (13, 0); // initialize to zeros

	int result, flength;

	string input1, input2, results;

	cin >> input1;
	cin >> input2;
	for(int i = 0; i < input1.size(); i++) {
		cout << input1.at(i);
	}

	for(int i = 0; i < input2.size(); i++) {
		cout << input2.at(i);
	}
	largeInt1.resize(input1.size());
	largeInt2.resize(input2.size());

	flength = largeInt1.size()+largeInt2.size();

	finalInt.resize(flength);
	result = addition(input1, input2, result, largeInt1, largeInt2, finalInt);

	stringstream convert;
	convert << result;
	results = convert.str();

	cout << "String Conversion is: " << results << endl;
	for (unsigned i=0; i<input1.length(); ++i)
		{
			finalInt[i] = input1.at(i);
		}

	for(int i = finalInt.size(); i <= 0; i--) {
		cout << finalInt[i];
	}

	cin >> flength;
	return 0;
}

int addition(string input1, string input2, int &result, vector<int> &largeInt1, vector<int> &largeInt2, vector<int> &finalInt){

	int length, base, last, remainder;
	bool isOdd;

	for (int i=0; i<input1.length(); ++i)
	{
		for(int n = 0; n < 12; n++) {
		if(input1.at(i)%(n+1)*10 == input1.at(i)) {
			largeInt1[12-n] = input1.at(i);
		}
	}
	}

	for (int i = 0; i<input2.length(); i++) {
		for(int n = 0; n < 12; n++) {
			if(input2.at(i)%(n+1)*10 == input2.at(i)) {
				largeInt2[12-n] = input2.at(i);
			}
	}
	}

	cout << "Input to LargeInt1: ";
	for(int i = 0; i < largeInt1.size(); i++) {
		cout << largeInt1[i] << " ";
		cout << endl;
	}
	cout << endl;
	cout << "Input to LargeInt2: ";
	for(int i = 0; i < largeInt2.size(); i++) {
		cout << largeInt2[i];
		cout << endl;
	}
	cout << endl;
	length = input1.size()+input2.size();
	if((length & 1) == 0) {
	} else {
		isOdd = true;
	}

	last = largeInt1.size();
	for(int i = 0; i <= length; i++) {
		base = largeInt1[12-i]+largeInt2[12-i];
		cout << "Base in << " << i << ": " << base << endl;
		if(base >= 10) {
			remainder = base-9;
			finalInt[12-i] = base-remainder;
			cout << "Remainder in << " << i << ": " << remainder << endl;
		} else {
			finalInt[12-i] = base;
		}
	}
cout << "Result is: " << result << endl;
return result;
}
"Why doesn't this work?" "What's wrong with this?" "How can I fix?"
Did you read what I said?

So my advice: Run your program in a debugger, statement by statement, and compare what it is doing with what you think it should be doing.

Not familiar with a debugger yet? Then start to get used to it right away. It is the most important tool you will ever use as a programmer.
When you know where is the problem, post.
Quote Originally Posted by robater View Post
Okay, well I tried to do the whole correct location thing. What am I doing wrong now?

Code:
#include <iostream>
#include <vector>
#include <cctype>
#include <stdexcept>
#include <algorithm>
#include <numeric>
#include <string>
#include <functional>
#include <assert.h>
#include <sstream>
using namespace std;

int addition(string input1, string input2, int &result, vector<int> &largeInt1, vector<int> &largeInt2, vector<int> &finalInt);


int main() {
	vector<int> largeInt1 (12, 0); // initialize to zero
	vector<int> largeInt2 (12, 0); // initialize to zero
	vector<int> finalInt (13, 0); // initialize to zeros

	int result, flength;

	string input1, input2, results;

	cin >> input1;
	cin >> input2;
	for(int i = 0; i < input1.size(); i++) {
		cout << input1.at(i);
	}

	for(int i = 0; i < input2.size(); i++) {
		cout << input2.at(i);
	}
	largeInt1.resize(input1.size());
	largeInt2.resize(input2.size());

	flength = largeInt1.size()+largeInt2.size();

	finalInt.resize(flength);
	result = addition(input1, input2, result, largeInt1, largeInt2, finalInt);

	stringstream convert;
	convert << result;
	results = convert.str();

	cout << "String Conversion is: " << results << endl;
	for (unsigned i=0; i<input1.length(); ++i)
		{
			finalInt[i] = input1.at(i);
		}

	for(int i = finalInt.size(); i <= 0; i--) {
		cout << finalInt[i];
	}

	cin >> flength;
	return 0;
}

int addition(string input1, string input2, int &result, vector<int> &largeInt1, vector<int> &largeInt2, vector<int> &finalInt){

	int length, base, last, remainder;
	bool isOdd;

	for (int i=0; i<input1.length(); ++i)
	{
		for(int n = 0; n < 12; n++) {
		if(input1.at(i)%(n+1)*10 == input1.at(i)) {
			largeInt1[12-n] = input1.at(i);
		}
	}
	}

	for (int i = 0; i<input2.length(); i++) {
		for(int n = 0; n < 12; n++) {
			if(input2.at(i)%(n+1)*10 == input2.at(i)) {
				largeInt2[12-n] = input2.at(i);
			}
	}
	}

	cout << "Input to LargeInt1: ";
	for(int i = 0; i < largeInt1.size(); i++) {
		cout << largeInt1[i] << " ";
		cout << endl;
	}
	cout << endl;
	cout << "Input to LargeInt2: ";
	for(int i = 0; i < largeInt2.size(); i++) {
		cout << largeInt2[i];
		cout << endl;
	}
	cout << endl;
	length = input1.size()+input2.size();
	if((length & 1) == 0) {
	} else {
		isOdd = true;
	}

	last = largeInt1.size();
	for(int i = 0; i <= length; i++) {
		base = largeInt1[12-i]+largeInt2[12-i];
		cout << "Base in << " << i << ": " << base << endl;
		if(base >= 10) {
			remainder = base-9;
			finalInt[12-i] = base-remainder;
			cout << "Remainder in << " << i << ": " << remainder << endl;
		} else {
			finalInt[12-i] = base;
		}
	}
cout << "Result is: " << result << endl;
return result;
}
Code:
	largeInt1.resize(input1.size());
	largeInt2.resize(input2.size());
Why are you resizing the vectors?

Code:
	flength = largeInt1.size()+largeInt2.size();

	finalInt.resize(flength);
Why is the length of the final int the length of both of the vectors?
Why are you resizing the finalInt?

Code:
int addition(string input1, string input2, int &result, vector<int> &largeInt1, vector<int> &largeInt2, vector<int> &finalInt){
Why is addition returning an int? If the number is larger than int then this function will not return the correct answer

I don't even know what your addition function is actually trying to do, but it's completely wrong....

Code:
	stringstream convert;
	convert << result;
	results = convert.str();
I don't even know what you're doing here.... You're trying to store a cstring into an int, makes no sense (results was declared as an int)

Code:
	for(int i = finalInt.size(); i <= 0; i--) {
		cout << finalInt[i];
	}
?????????????????????????????

I want you to do this, here is a very large math problem. Write it down on a piece of paper, nice and neat, line up the columns of both and I want you to add it together.

_951753456
+753159456

Think about this logically, what are you doing when you add the numbers together? Step by step by step by step. Think about where the numbers need to be in in each vector. Think about what happens when the answer of one column is greater than 9 (meaning it's two digits)
If you wanna add some really big ass nums, look at the GNU Multi-Precision library. It's used for SSL and all that lovely Cryptographic stuff.
Basically supports adding numbers as large as the amount of RAm you have.

For example, it is able to calculate mathematical operations on 4096 bit RSA keys ( Aka a key greater than a 64 bit value like __int64 )
Posts 16–18 of 18 · Page 2 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us