4 Replies - 961 Views - Last Post: 07 August 2010 - 08:37 AM Rate Topic: -----

#1 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 122
  • Joined: 06-September 09

Overcoming the limited max size of an "Int" variable

Posted 07 August 2010 - 02:50 AM

I have to get two integers with a maximum of 20 digits in length, add them together, and output the result. Since an integer can't store a number that big, I have to find a way to work around that. I've tried two things, but both didn't work. I also didn't quite understand the "hint" my book gave me which was: "Read numbers as strings and store the digits of the number in the reverse order"

The first of the two solutions that failed:
int main() {
	char arry[2][21];
	int arry2[21] = {0};
	int row,col;
	
	for (row = 0; row < 2; row++) {
		for (col = 0; col < 20; col++) {
			cin >> arry[row][col];
		}
	}
	for (col = 20; col >= 0; col--) {
		arry2[col] += (static_cast<int>(arry[0][col]) - 48) + (static_cast<int>(arry[1][col]) - 48);
		//carry the one
		if (arry2[col] >= 10) {
			arry2[col] -= 10;
			arry2[col-1] += 1;
		}
	}
	for (col = 0; col < 20; col++) {
		cout << arry2[col];
	}
	return 0;
}

This one works in that it will output the correct number. There's three problems, though.
1) It forces the input to be entered in individual digits
2) It forces the user to input a total of 40 digits, rather than that just being the max
3) If the user inputs numbers that will end in the last digit being a carried one (ex: 50000000000000000000 + 50000000000000000000) I'll get an error stating: "Stack around the variable 'arry2' was corrupted"

So I tried to come up with this one:
int main() {
	char arry[2][21];
	int length1,length2;
	int row, col;
	
	string str1, str2;

	cin >> str1 >> str2;
	
	length1 = str1.size();
	length2 = str2.size();
	
	for (row = 0; row < 2; row++) {
		for (col = 20; col > 0; col--) {
			if (row == 0) {
				arry[row][col] = str1.substr(length1, 1);
				length1--;
			} else {
				arry[row][col] = str2.substr(length2, 1);
				length2--;
			}
		}
	}
	return 0;
}

Before I finished, I learned that a char variable apparently can't take a character from a string. At least, the way I tried it.

I am lost.

Is This A Good Question/Topic? 0
  • +

Replies To: Overcoming the limited max size of an "Int" variable

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Overcoming the limited max size of an "Int" variable

Posted 07 August 2010 - 05:38 AM

You are close with the second attempt. :)

Strings have an array operator - [] see string::operator[] so you can reference the character you are selecting with substr() directly:

arry[row][col] = str1[0];



If you want include bounds checking while extracting characters from the strings use string::at - Get character in string

Legal array indexes range from zero to N-1, where N is the declared size of the array. You are starting your array indexes at one, so you have sized your arrays at 21 instead of 20. I have modified your code to use 20 as the array size:

const int COLUMNS = 20;
const int ROWS = 2;

char arry[ROWS][COLUMNS];

int length1,length2;
int row, col;

string str1, str2;

cin >> str1 >> str2;

for (row = 0; row < ROWS; row++) {
	// zero is a legal index value,
	// so check that col is not negative
	for (col = COLUMNS - 1; col > -1; col--) {
		if (row == 0) {
			arry[row][col] = str1[col];
		} else {
			arry[row][col] = str2[col];
		}
	}
}

cout << endl;

cout << "str1 = " << str1 << endl;
cout << "str2 = " << str2 << endl;

cout << endl;

cout << "Array row zero = ";

for (int i = 0; i < COLUMNS; i++)
	cout << arry[0][i];

cout << endl;

cout << "Array row one  = ";

for (int i = 0; i < COLUMNS; i++)
	cout << arry[1][i];

cout << endl;



Here's the output with these changes:

Quote

12345678901234567890
98765432109876543210

str1 = 12345678901234567890
str2 = 98765432109876543210

Array row zero = 12345678901234567890
Array row one = 98765432109876543210

Hit ENTER to continue...

This post has been edited by n8wxs: 07 August 2010 - 05:43 AM

Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Overcoming the limited max size of an "Int" variable

Posted 07 August 2010 - 06:20 AM

Use a function. This should work:
#include <iostream>

using namespace std;

bool convert(const string &s, char *cs, int maxSize) {
	int length = s.size();
	
	if (length>maxSize) {
		cout << "String larger than " << maxSize << endl;
		return false;
	}
	
	cs[length]=0;
	
	for (int i=0; i<length; i++) {
		char ch = s[i];
		if (ch<'0' || ch>'9') {
			cout << "String contains invalid value " << ch << endl;
			cs[0] = 0;
			return false;
		}
		cs[i] = ch;
	}
	return true;
}


int main() {
	
	char arry[2][21];
	
	//string str1, str2;
	//cin >> str1 >> str2;
	string str1("123456789"), str2("54536347554754");
	
	convert(str1, arry[0], 20);
	convert(str2, arry[1], 20);
	
	for (int row = 0; row < 2; row++) { cout << arry[row] << endl; }
	return 0;
}



Or do you truly want the thing zero padded up front?

If so:
bool convert(const string &s, char *cs, int maxSize) {
	int length = s.size();
	
	if (length>maxSize) {
		cout << "String larger than " << maxSize << endl;
		return false;
	}
	
	cs[maxSize] = 0;
	
	int padding = maxSize - length;
	for (int i=0; i<padding; i++) { cs[i] = '0'; }
	
	for (int i=padding; i<maxSize; i++) {
		char ch = s[i-padding];
		if (ch<'0' || ch>'9') {
			cout << "String contains invalid value " << ch << endl;
			cs[0] = 0;
			return false;
		}
		cs[i] = ch;
	}
	return true;
}


Was This Post Helpful? 1
  • +
  • -

#4 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 122
  • Joined: 06-September 09

Re: Overcoming the limited max size of an "Int" variable

Posted 07 August 2010 - 06:39 AM

<3 you both. Thanks very much.
Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Overcoming the limited max size of an "Int" variable

Posted 07 August 2010 - 08:37 AM

Here's another approach that might work for you. Use a structure to hold an array pointer and the size of the array. This allows for automatic sizing of the array, which means the number strings can be arbitrarily long.



struct numarray {
	int nsize;
	char * narray;
};

int _tmain(int argc, char* argv[])
{
	string str1, str2;

	cin >> str1 >> str2;

	struct numarray num1, num2;

	num1.narray = new char[str1.length()];
	num1.nsize = str1.length();

	num2.narray = new char[str2.length()];
	num2.nsize = str2.length();

	for (int i = 0; i < num1.nsize; i++)
		num1.narray[i] = str1[i];

	for (int i = 0; i < num2.nsize; i++)
		num2.narray[i] = str2[i];

	cout << endl;

	cout << "str1 = " << str1 << endl;
	cout << "str2 = " << str2 << endl;

	cout << endl;

	cout << "Array num1 = ";

	for (int i = 0; i < num1.nsize; i++)
		cout << num1.narray[i];

	cout << endl;

	cout << "Array num2 = ";

	for (int i = 0; i < num2.nsize; i++)
		cout << num2.narray[i];

	cout << endl;

	delete [] num1.narray;
	delete [] num2.narray;

	cin.clear();
	cin.sync();
	cout << endl << "Hit ENTER to continue...";
	cin.get();

	return 0;
}



Here's a run:

Quote

12345678901234567890
9876543210987654321098765432109876543210

str1 = 12345678901234567890
str2 = 9876543210987654321098765432109876543210

Array num1 = 12345678901234567890
Array num2 = 9876543210987654321098765432109876543210

Hit ENTER to continue...

This post has been edited by n8wxs: 07 August 2010 - 08:56 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1