SolvedAdding 2 huge numbers using vectors

Posts 1–15 of 18 · Page 1 of 2
Adding 2 huge numbers using vectors
I googled and found either useless shit or too complex, Can someone help me make a program using vectors to add 2 big numbers for example "9905803458 + 2942093480"

This is what I have so far (not working):

Code:
#include <iostream>

#include <vector>
using namespace std;

int main()
{
    vector <int> int1;
    vector <int> int2;
    vector <int> final;
    
    int input1, input2;
    int length = 0, length1 = 0;
    
    
    cin >> input1 >> input2;
    cout << "after cin" << endl;
    
string strNum = to_string(input1); // 123 becomes "123"
length = strNum.length(); // length = 3
    
string strNum1 = to_string(input2); // 123 becomes "123"
length1 = strNum.length(); // length = 3

    if(length > length1){
        strNum = to_string(input1); // 123 becomes "123"
length = strNum.length(); // length = 3

    } else {
        strNum1 = to_string(input2); // 123 becomes "123"
length1 = strNum.length(); // length = 3
    }


    cout << length;
    
    string q = to_string(input2); // 123 becomes "123"

    for(int i = 0; i < length; i++){
        
        int1[i] = strNum.at(i);
        int2[i] = strNum1.at(i);
    }
    cout << "after ye" << endl;
    for(int i = 0; i < length; i++){
        cout << " " << int1[i];
    }
    
    return 0;
}
So you're just trying to add two very large numbers? Why are you using strings and ints?

Data Type Ranges
Quote Originally Posted by InunoTaishou View Post
So you're just trying to add two very large numbers? Why are you using strings and ints?

Data Type Ranges
because I have to use vectors (school thing)... help pls
That's not it, you're using int, just use a bigger data type and you can add very large numbers together.

vector<long long> instead of vector<int>

Maybe if you post the actual assignment we might be able to help you better.
Quote Originally Posted by InunoTaishou View Post
That's not it, you're using int, just use a bigger data type and you can add very large numbers together.

vector<long long> instead of vector<int>

Maybe if you post the actual assignment we might be able to help you better.
I am sure you don't know that a typical (4-byte) signed int holds values up to 2147483647.
You can take the naive approach and store each digit of the number in a separate element in the vector and then work on the numbers like you would on pencil and paper.
Let's assume the char datatype is 8 bits, then use sizeof and CHAR_BIT.
Quote Originally Posted by maestro1994 View Post
I am sure you don't know that a typical (4-byte) signed int holds values up to 2147483647.
You can take the naive approach and store each digit of the number in a separate element in the vector and then work on the numbers like you would on pencil and paper.
Let's assume the char datatype is 8 bits, then use sizeof and CHAR_BIT.
Well based on the link I posted in my very first reply

Code:
Type Name	Bytes	Other Names		Range of Values
int 		4 	signed 			–2,147,483,648 to 2,147,483,647
Oh look, int ha is 4 bytes and has a range of values of -2.1billion to 2.1billion

Anyways, based on OP's post all he stated was he needed to add two very large numbers using vectors. He could be trying to store each number in an element of a vector and work the math like a 3rd grader does on their test or he could just need to use std::accumulate or a for loop to add them all together.
Quote Originally Posted by InunoTaishou View Post
Well based on the link I posted in my very first reply

Code:
Type Name	Bytes	Other Names		Range of Values
int 		4 	signed 			–2,147,483,648 to 2,147,483,647
Oh look, int ha is 4 bytes and has a range of values of -2.1billion to 2.1billion

Anyways, based on OP's post all he stated was he needed to add two very large numbers using vectors. He could be trying to store each number in an element of a vector and work the math like a 3rd grader does on their test or he could just need to use std::accumulate or a for loop to add them all together.
My teacher wants me to do 1 part of the input at a time into sections of the vector, for example if my number was 23, vector[0] would be 2 and vector[1] would be 3.

This is my lab instructions for reference.

Lab 17
Extra Additives
Objective:
To reinforce skills in defining and manipulating arrays,with the vector class.
Computers store decimal numbers in binary form to conserve memory. Even though this allows a greater
range of values, the largest integer value in many systems is 32,767 and long integers, the largest value is
less than 2.2 billion.
Your task is to write a program, using a minimum of three functions that input pairs of numbers between 0
and 100 billion, inclusive. You should then add these numbers together and print the results.
Input:
The input will only consist of digit characters (‘0’ . . ‘9’). No commas or other punctuation will be in the
data. All values will be non-negative. Since the integers may exceed any binary integer value allowed in the
system, we shall read in the values as a string and convert each integer character to its corresponding integer
digit and store it in an integer array.
Output:
Your program should print out the results of the addition. Do not print any leading zeros before any answer.
Put a loop in your program to run it four times.
Examples:
Input: Output:
124235
7453
325235
0
23523
100000000000
326346124
76434124235
The sum is: 131688
The sum is: 325235
The sum is: 100000023523
The sum is: 76760470359
Use the declarations at right
to store your digits.
vector<int> largeInt1 (12,0); // initialize to zero
vector<int> largeInt2 (12,0); // initialize to zero
vector<int> finalInt (13,0); // initialize to zero
To convert digit characters to
digit integers you may use
this function.
int asciiToInt (char ch)
{
return (ch - ‘0’);
}
Code:
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int asciiToInt(char ch)
{
	return (ch - '0');
}

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 zero
	
	// Loop four times
	for (int i = 1; i <= 8; i++)
	{
		// Create a string variable for the input and the vectorIndex and finalIntIndex will be used for assigning each number in the proper position in the vectors
		string number;
		int vectorIndex = 11, finalIntIndex = 12;

		cin >> number;

		// Check that the number's length is not longer than the elements in the vector
		if (number.length() > largeInt1.size())
		{
			cout << "That number is too large. Please try again" << endl;
			i -= 1;
			continue;
		}

		// If i is even, meaning we're getting input from the second or fourth number
		if ((i % 2) == 0)
		{
			// Assign each character number to the element in the vector. Going from right to left for the vector and the number since you add right to left
			for (int index = number.length() - 1; index >= 0; index--)
			{
				largeInt2[vectorIndex] = asciiToInt(number[index]);
				vectorIndex -= 1;
			}

			// Since we're on the second or fourth number, start adding each vector together, starting from right to left
			for (int index = largeInt1.size() - 1; index >= 0; index--, finalIntIndex--)
			{
				// Add the elements in largeInt1 and largeInt2 together at the same index in the current index for finalInt
				finalInt[finalIntIndex] += largeInt1[index] + largeInt2[index];

				// Check to see if that finalInt[finalIntIndex] is > 9 and we're not on the 0 index. If the number is then you need to subtract 10, storing the difference in the element one left of finalInt
				if (finalInt[finalIntIndex] > 9 && index > 0)
				{
					
				}
			}

			// Print your answer, be sure to clear the vectors (set everything back to 0) so the next set of numbers are not being added on top of this set
			
		}
		// We're on the first or third input
		else
		{
			// Assign each character number to the element in the vector. Going from right to left for the vector and the number since you add right to left
			for (int index = number.length() - 1; index >= 0; index--)
			{
				largeInt1[vectorIndex] = asciiToInt(number[index]);
				vectorIndex -= 1;
			}
		}
	}
}
I didn't do the addition part for you but I gave you a hint on what to do.

Just want to note, for the main for loop you could make it i != 4 and move the declaration of all the vectors into the for loop. Get rid of the if (i % 2) and instead do

Code:
cin >> number;
// assign the elements in the number to the elements in the first vector
cin >> number;
// assign the elements in the number to the element in the second vector
and then do your math. It's just a matter of what you want to do
Quote Originally Posted by InunoTaishou View Post
Well based on the link I posted in my very first reply

Code:
Type Name	Bytes	Other Names		Range of Values
int 		4 	signed 			–2,147,483,648 to 2,147,483,647
Oh look, int ha is 4 bytes and has a range of values of -2.1billion to 2.1billion
I didn't read the link. I accidentally didn't see your first post.

Quote Originally Posted by InunoTaishou View Post
based on OP's post all he stated was he needed to add two very large numbers using vectors. He could be trying to store each number in an element of a vector and work the math like a 3rd grader does on their test or he could just need to use std::accumulate or a for loop to add them all together.
No, leave it.



Quote Originally Posted by InunoTaishou View Post
Code:
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int asciiToInt(char ch)
{
	return (ch - '0');
}

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 zero
	
	// Loop four times
	for (int i = 1; i <= 8; i++)
	{
		// Create a string variable for the input and the vectorIndex and finalIntIndex will be used for assigning each number in the proper position in the vectors
		string number;
		int vectorIndex = 11, finalIntIndex = 12;

		cin >> number;

		// Check that the number's length is not longer than the elements in the vector
		if (number.length() > largeInt1.size())
		{
			cout << "That number is too large. Please try again" << endl;
			i -= 1;
			continue;
		}

		// If i is even, meaning we're getting input from the second or fourth number
		if ((i % 2) == 0)
		{
			// Assign each character number to the element in the vector. Going from right to left for the vector and the number since you add right to left
			for (int index = number.length() - 1; index >= 0; index--)
			{
				largeInt2[vectorIndex] = asciiToInt(number[index]);
				vectorIndex -= 1;
			}

			// Since we're on the second or fourth number, start adding each vector together, starting from right to left
			for (int index = largeInt1.size() - 1; index >= 0; index--, finalIntIndex--)
			{
				// Add the elements in largeInt1 and largeInt2 together at the same index in the current index for finalInt
				finalInt[finalIntIndex] += largeInt1[index] + largeInt2[index];

				// Check to see if that finalInt[finalIntIndex] is > 9 and we're not on the 0 index. If the number is then you need to subtract 10, storing the difference in the element one left of finalInt
				if (finalInt[finalIntIndex] > 9 && index > 0)
				{
					
				}
			}

			// Print your answer, be sure to clear the vectors (set everything back to 0) so the next set of numbers are not being added on top of this set
			
		}
		// We're on the first or third input
		else
		{
			// Assign each character number to the element in the vector. Going from right to left for the vector and the number since you add right to left
			for (int index = number.length() - 1; index >= 0; index--)
			{
				largeInt1[vectorIndex] = asciiToInt(number[index]);
				vectorIndex -= 1;
			}
		}
	}
}
I didn't do the addition part for you but I gave you a hint on what to do.

Just want to note, for the main for loop you could make it i != 4 and move the declaration of all the vectors into the for loop. Get rid of the if (i % 2) and instead do

Code:
cin >> number;
// assign the elements in the number to the elements in the first vector
cin >> number;
// assign the elements in the number to the element in the second vector
and then do your math. It's just a matter of what you want to do
Your code is incomplete. Let's see: the "carry handling" ( if (finalInt[finalIntIndex] > 9 && index > 0) ... ) is empty. Are you sure it works?
Quote Originally Posted by maestro1994 View Post
I didn't read the link. I accidentally didn't see your first post.


No, leave it.





Your code is incomplete. Let's see: the "carry handling" ( if (finalInt[finalIntIndex] > 9 && index > 0) ... ) is empty. Are you sure it works?
Quote Originally Posted by InunoTaishou View Post
I didn't do the addition part for you but I gave you a hint on what to do.
Did you accidentally not read the entire post? I got all the ground work laid out for him, with comments explaining what is going on. He's in a class, this is his assignment, I could have finished it but he would learn better if he finished it himself.
Quote Originally Posted by InunoTaishou View Post
Code:
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int asciiToInt(char ch)
{
	return (ch - '0');
}

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 zero
	
	// Loop four times
	for (int i = 1; i <= 8; i++)
	{
		// Create a string variable for the input and the vectorIndex and finalIntIndex will be used for assigning each number in the proper position in the vectors
		string number;
		int vectorIndex = 11, finalIntIndex = 12;

		cin >> number;

		// Check that the number's length is not longer than the elements in the vector
		if (number.length() > largeInt1.size())
		{
			cout << "That number is too large. Please try again" << endl;
			i -= 1;
			continue;
		}

		// If i is even, meaning we're getting input from the second or fourth number
		if ((i % 2) == 0)
		{
			// Assign each character number to the element in the vector. Going from right to left for the vector and the number since you add right to left
			for (int index = number.length() - 1; index >= 0; index--)
			{
				largeInt2[vectorIndex] = asciiToInt(number[index]);
				vectorIndex -= 1;
			}

			// Since we're on the second or fourth number, start adding each vector together, starting from right to left
			for (int index = largeInt1.size() - 1; index >= 0; index--, finalIntIndex--)
			{
				// Add the elements in largeInt1 and largeInt2 together at the same index in the current index for finalInt
				finalInt[finalIntIndex] += largeInt1[index] + largeInt2[index];

				// Check to see if that finalInt[finalIntIndex] is > 9 and we're not on the 0 index. If the number is then you need to subtract 10, storing the difference in the element one left of finalInt
				if (finalInt[finalIntIndex] > 9 && index > 0)
				{
					
				}
			}

			// Print your answer, be sure to clear the vectors (set everything back to 0) so the next set of numbers are not being added on top of this set
			
		}
		// We're on the first or third input
		else
		{
			// Assign each character number to the element in the vector. Going from right to left for the vector and the number since you add right to left
			for (int index = number.length() - 1; index >= 0; index--)
			{
				largeInt1[vectorIndex] = asciiToInt(number[index]);
				vectorIndex -= 1;
			}
		}
	}
}
I didn't do the addition part for you but I gave you a hint on what to do.

Just want to note, for the main for loop you could make it i != 4 and move the declaration of all the vectors into the for loop. Get rid of the if (i % 2) and instead do

Code:
cin >> number;
// assign the elements in the number to the elements in the first vector
cin >> number;
// assign the elements in the number to the element in the second vector
and then do your math. It's just a matter of what you want to do
Do you mind just looking over my new code, if I don't code it myself I won't understand it. I basically did everything except when I input 5 it becomes 53, can you help me fix my code?

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];
	}


	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)
	{
		largeInt1[i] = input1.at(i);
	}

	for (int i = 0; i<input2.length(); i++) {
		largeInt2[i] = 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[i]+largeInt2[i];
		cout << "Base in << " << i << ": " << base << endl;
		if(base >= 10) {
			remainder = base-9;
			finalInt[i] = base-remainder;
			cout << "Remainder in << " << i << ": " << remainder << endl;
		} else {
			finalInt[i] = base;
		}
	}
cout << "Result is: " << result << endl;
return result;
}
Quote Originally Posted by InunoTaishou View Post
Did you accidentally not read the entire post? I got all the ground work laid out for him, with comments explaining what is going on. He's in a class, this is his assignment, I could have finished it but he would learn better if he finished it himself.
Oh ok, don't worry.

Quote Originally Posted by robater View Post
Do you mind just looking over my new code, if I don't code it myself I won't understand it. I basically did everything except when I input 5 it becomes 53, can you help me fix my code?

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];
	}


	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)
	{
		largeInt1[i] = input1.at(i);
	}

	for (int i = 0; i<input2.length(); i++) {
		largeInt2[i] = 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[i]+largeInt2[i];
		cout << "Base in << " << i << ": " << base << endl;
		if(base >= 10) {
			remainder = base-9;
			finalInt[i] = base-remainder;
			cout << "Remainder in << " << i << ": " << remainder << endl;
		} else {
			finalInt[i] = base;
		}
	}
cout << "Result is: " << result << endl;
return result;
}
There are several errors in your code:

(1) computation of length: Why do you think the length of the result is them sum of the length of the two operands? The result length can at max be the max (length1, lenght2) + 1.

(2) isOdd is never initialized to false; and it is not used anywhere

(3) The addition loop access vector cells that are out of range:
Code:
for(int i = 0; i <= length; i++) {
(4) You calculate a remainder, but never use it in the next loop iteration:
Code:
remainder = base-9;
    finalInt[i] = base-remainder;
And probably some errors I haven't seen by just skimming over your source code. 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.
Like maestro pointed out, there's a lot of errors.

One of the things my teacher taught the class when I took it was to figure out what your program needs to do and an algorithm to accomplish that.

Code:
al·go·rithm
ˈalɡəˌriT͟Həm/
noun
noun: algorithm; plural noun: algorithms

    a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer.
Starting from the top you've got two vectors that will hold the users inputted numbers (each character in the number stored in each elements of the vector)

So you need to first put each character (I.e., if the users number is 1992 then 1, 9, 9, 2 are all characters) need to be put in the correct spots in the vector (think about how you would add a very large number that you can't do in your head)
Ex.
_1798
+8971
------
????

Next you need to add each element in both arrays together and store the result in the answer vector

After that you need to go through each element in the answer vector and determine if it's >9 (meaning it has two digits) (again, look at the example above on what you do in addition if you have two numbers added together when they are >9)

Finally you'll print your result, and not print any leading 0's.

I might have missed some steps but this is going to be a rough idea on what to do.

1. You did .resize on your vector. Your teacher gave you the variable declarations he wants you to use in your program.
2. Your addition function returns an int, the whole point of your project is to add numbers together that are larger than what int can support. (Meaning if the user inputs 1325849561254 && 999999999999, your addition function will not return the correct answer)
Quote Originally Posted by InunoTaishou View Post
Like maestro pointed out, there's a lot of errors.

One of the things my teacher taught the class when I took it was to figure out what your program needs to do and an algorithm to accomplish that.

Code:
al·go·rithm
ˈalɡəˌriT͟Həm/
noun
noun: algorithm; plural noun: algorithms

    a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer.
Starting from the top you've got two vectors that will hold the users inputted numbers (each character in the number stored in each elements of the vector)

So you need to first put each character (I.e., if the users number is 1992 then 1, 9, 9, 2 are all characters) need to be put in the correct spots in the vector (think about how you would add a very large number that you can't do in your head)
Ex.
_1798
+8971
------
????

Next you need to add each element in both arrays together and store the result in the answer vector

After that you need to go through each element in the answer vector and determine if it's >9 (meaning it has two digits) (again, look at the example above on what you do in addition if you have two numbers added together when they are >9)

Finally you'll print your result, and not print any leading 0's.

I might have missed some steps but this is going to be a rough idea on what to do.

1. You did .resize on your vector. Your teacher gave you the variable declarations he wants you to use in your program.
2. Your addition function returns an int, the whole point of your project is to add numbers together that are larger than what int can support. (Meaning if the user inputs 1325849561254 && 999999999999, your addition function will not return the correct answer)
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;
}
Anyway, also there is mpz_set_str function.
Just set your large number like this:
Code:
mpz_set_str (MP_INT *integer, char *initial_value, int base)
Code:
std::string add_it( char const * in1, char const * in2, int const SIZE)
  {
  MP_INT integ1, integ2, result;
  mpz_set_str (&integ1, in2, 10);
  mpz_set_str (&integ2, in2, 10);
  mpz_add (&result, &integ1, &integ2);
  char output[SIZE];
  mpz_get_str (output, 10, &result);
  return output;
  }
You've received the replies with solutions. I think you can now do it.
Posts 1–15 of 18 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us