Int50 Overloaded Functions

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1366 Views - Last Post: 29 January 2011 - 07:38 PM Rate Topic: -----

#1 AverageAsian   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-January 11

Int50 Overloaded Functions

Posted 27 January 2011 - 09:52 AM

I think this is the last one I'll make for this program. I titled this topic this way so I could ask for help on other overloaded functions if I need to. Currently I'm stuck on the operator+ function. The function works correctly but I just need some guidance getting the right array size for the sum value for one situation in adding such as this: 999 + 999 which the sum its spitting out is 998. I'm thinking of adding something in the if statement in the operator+ to fix this but I'm having a hard time trying of something.

Int50.h
#include <iostream>
using namespace std;

class Int50
{
	private:
		static const int SZ = 50;
		short Ints[SZ];
		unsigned char DataSize;

	public:
		Int50();
		Int50(const string &);

		Int50 operator+(const Int50 &I2) const; 
		Int50 operator-(const Int50 &I2) const;
		Int50 operator*(const Int50 &I2) const;
		Int50 operator/(const Int50 &I2) const;

		friend ostream& operator<<(ostream& out, Int50& I);
		friend istream& operator>>(istream& in, Int50& I);
};



Int50.cpp
#include <iostream>
#include <string>
#include "Int50.h"
using namespace std;

Int50 Int50::operator+(const Int50 &I2) const
{
	Int50 Temp;
	int Carry = 0;

	for(int i = SZ-1; i >= 0; i--)
	{
		Temp.Ints[i] = Ints[i] + I2.Ints[i] + Carry;
			

		if (Temp.Ints[i] > 9)
		{
			Carry = 1;
			Temp.Ints[i] -= 10;
		}

		else
			Carry = 0;
	}

	if(Ints > I2.Ints)
		Temp.DataSize = DataSize;

	else
		Temp.DataSize = I2.DataSize;

	return Temp;
}

Int50 Int50::operator-(const Int50 &I2) const
{
	Int50 Temp;
	int Borrow = 0;
	int Added = 0;

	for(int i = SZ-1; i > 0; i--)
	{
		Temp.Ints[i] = (Ints[i] + Added) - I2.Ints[i] - Borrow;

		if(Ints[i] < I2.Ints[i])
		{
			Added = 10;
			Borrow = 1;
		}

		else
		{
			Added = 0;
			Borrow = 0;
		}
	}

	if(Ints > I2.Ints)
		Temp.DataSize = DataSize;

	else
		Temp.DataSize = I2.DataSize;

	return Temp;
}

istream& operator>>(istream& in, Int50& I)
{
	string s; 

	cout << "Enter a value: ";
		in >> s;

	I = Int50(s);

	return in;
}

ostream& operator<<(ostream& out, Int50& I)
{
	if (I.DataSize == 0)
		out << '0';

	else 
	{
		for(int i = 0; i < I.DataSize; i++) 
			out << I.Ints[i];
	}

	return out;
}

Int50::Int50()
{
	for(int i = 0; i < SZ; i++)
		Ints[i] = 0;
}

Int50::Int50(const string &s) 
{
	int sz = s.size();

	if (sz == 0 || sz > 50) 
		DataSize = 0;

	else 
	{
		DataSize = sz;
		int i;
		short n;

		for(i = 0; i < sz; i++) 
		{
			n = s[i] - '0';

			if (n < 0||n > 9) 
			{ 
				DataSize = 0;
				break;
			}

			Ints[i] = n;
		}

		for(; i < SZ; i++)
		{
			Ints[i] = 0;
		}
	}
}



Int50 Driver.cpp
#include <iostream>
#include "Int50.h"
using namespace std;

int main()
{
	Int50 I1;
		cin >> I1;
		cout << "I1: " << I1 << endl;

	Int50 I2;
		cin >> I2;
		cout << "I2: " << I2 << endl;

	Int50 I3 = I1 + I2;
		cout << I1 << "+" << I2 << "=" << I3 << endl;

	return 0;
}


This post has been edited by AverageAsian: 27 January 2011 - 10:12 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Int50 Overloaded Functions

#2 akhena   User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 94
  • Joined: 20-January 11

Re: Int50 Overloaded Functions

Posted 27 January 2011 - 10:20 AM

Is line 26 in Int50.cpp doing what you want ? ... You are comparing two pointers.

For the addition, you are looping on the 50 elements of your Int50. Why not just go from SIZE-1 until the largest DataSize (from this or I2) ? Then when you finish there, if you still have a carry, add it to the next element unless you have already reached your 50th element.

In that case, it's up to you to decide how you handle it. Either you throw an exception stating that you have an overflow, or maybe you just start again from 0. So for instance if you had Int3, doing 999 + 2 would give 1.
Was This Post Helpful? 0
  • +
  • -

#3 AverageAsian   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-January 11

Re: Int50 Overloaded Functions

Posted 27 January 2011 - 01:35 PM

View Postakhena, on 27 January 2011 - 05:20 PM, said:

Is line 26 in Int50.cpp doing what you want ? ... You are comparing two pointers.

For the addition, you are looping on the 50 elements of your Int50. Why not just go from SIZE-1 until the largest DataSize (from this or I2) ? Then when you finish there, if you still have a carry, add it to the next element unless you have already reached your 50th element.

In that case, it's up to you to decide how you handle it. Either you throw an exception stating that you have an overflow, or maybe you just start again from 0. So for instance if you had Int3, doing 999 + 2 would give 1.


I'm a little lost on your explanation there. Could you go into further detail? Also my carry must not be working properly
Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Int50 Overloaded Functions

Posted 27 January 2011 - 09:23 PM

View PostAverageAsian, on 27 January 2011 - 10:35 PM, said:

... Also my carry must not be working properly


Your carry works fine. When the for loop is finished there is still 1 in the carry. (Try printing it out).

You could also investigate adding two numbers of different lengths, eg. 2 and 14. See if it works ok. This is not to do with the carry.
Was This Post Helpful? 0
  • +
  • -

#5 AverageAsian   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-January 11

Re: Int50 Overloaded Functions

Posted 28 January 2011 - 01:21 PM

View Post#define, on 28 January 2011 - 04:23 AM, said:

View PostAverageAsian, on 27 January 2011 - 10:35 PM, said:

... Also my carry must not be working properly


Your carry works fine. When the for loop is finished there is still 1 in the carry. (Try printing it out).

You could also investigate adding two numbers of different lengths, eg. 2 and 14. See if it works ok. This is not to do with the carry.


Is that printing out the carry gonna work with my code as it is now? or will I have to add some modifications of some kind? Also, my operator+ will add correctly with numbers of different lengths like 14 and 2 such that all the numbers have 0's preceding so for it to work you would have to have 14 + 02. It'll work for most adding except the ones that involve the carrying over.
Was This Post Helpful? 0
  • +
  • -

#6 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Int50 Overloaded Functions

Posted 28 January 2011 - 01:42 PM

View PostAverageAsian, on 28 January 2011 - 10:21 PM, said:

Is that printing out the carry gonna work with my code as it is now? or will I have to add some modifications of some kind?


You are adding 999 and 999 which is 1998. The length of the input numbers are 3 and the length of the resulting number is 4. So to do it this way you need to insert a one at the beginning.


View PostAverageAsian, on 28 January 2011 - 10:21 PM, said:

Also, my operator+ will add correctly with numbers of different lengths like 14 and 2 such that all the numbers have 0's preceding so for it to work you would have to have 14 + 02. It'll work for most adding except the ones that involve the carrying over.


I was just thinking it is also possible to have the number at the right end of the array.

<-- Ints[0] ......... Ints[49] --->
[0][0][0][0][0][0][0][0][0][9][9][9]
[0][0][0][0][0][0][0][0][0][0][0][2]
+
[0][0][0][0][0][0][0][0][1][0][0][1]
Was This Post Helpful? 0
  • +
  • -

#7 akhena   User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 94
  • Joined: 20-January 11

Re: Int50 Overloaded Functions

Posted 28 January 2011 - 03:11 PM

Int50 Int50::operator+(const Int50 &I2) const
{
	Int50 Temp;
	int Carry = 0;

	for(int i = SZ-1; i >= 0; i--) /* Why are you going backward ??? */
	{
		Temp.Ints[i] = Ints[i] + I2.Ints[i] + Carry;
			

		if (Temp.Ints[i] > 9)
		{
			Carry = 1;
			Temp.Ints[i] -= 10;
		}

		else
			Carry = 0;
	}
        /* At the end of this loop, if you still have a carry, it means that the sum should be an
        // Int with 51 digits. And you probably don't want that. So you could throw an exception. */
	if(Ints > I2.Ints)
		Temp.DataSize = DataSize;

	else
		Temp.DataSize = I2.DataSize;

	return Temp;
}



Why looping backward ? When you sum 38 + 75, you add first 8 to 5, and write a 3 with a carry, then you add 3 to 7 plus the carry, so that's 1 plus a carry. So you end up with 113. In Int[0] you have the units, and in Int[1] you have the tens and in Int[2] the hundreds, etc.

In my previous post, I was trying to optimize a little bit your code. When you sum 0000000111 + 0000000022 you should find out that the result is 0000000133. So why bothering to loop on all these 0 ? What you should really be looping on, is the largest Int you have, in my case it's the first one. So I would only sum 111 + 022. But again, it's just a bit of optimization. Try first to get your code working as it should. :)
Was This Post Helpful? 0
  • +
  • -

#8 AverageAsian   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 24-January 11

Re: Int50 Overloaded Functions

Posted 29 January 2011 - 10:43 AM

Ok so I figured out how to print out the carry, but the problem is it only works for 1 digit numbers with a preceding 0. so this will work:

09 + 09 = 18

However, this will not work:

099 + 099 = 199

Problem with this is it'll add the carry to the 9 + 9 which is 18 - 10 = 8,then it adds the carry value of 1 to it. I thought placing it outside of the if statement might fix it but that only gives me an error on the 'else' saying "Error: expected a statement" I just need to figure out how to get the carry to only add the carry to the index once its on the last addition part

Int50.cpp - operator+
Int50 Int50::operator+(const Int50 &I2) const
{
	Int50 Temp;
	int Carry = 0;
	int i;

	for(i = 0; i < SZ; i++)
	{
		Temp.Ints[i] = Ints[i] + I2.Ints[i] + Carry;

		if (Temp.Ints[i] > 9)
		{
			Temp.Ints[i] -= 10;
			Carry = 1;
			Temp.Ints[i-1] += Carry;
		}
		
		else
			Carry = 0;

	}

	if(Ints > I2.Ints)
		Temp.DataSize = DataSize;

	else
		Temp.DataSize = I2.DataSize;

	//Temp.DataSize = FindDataSize(Temp);

	return Temp;
}


This post has been edited by AverageAsian: 29 January 2011 - 10:44 AM

Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Int50 Overloaded Functions

Posted 29 January 2011 - 11:48 AM

I think you shouldn't accept whatever anyone says without questioning. I haven't studied your code for a long time but I don't see anyplace where you are reversing the string (nor should you), so as far as I can tell Int[0] is the HIGHEST order digit, and Int[SZ-1] is the units, Int[SZ-2] is the 10's, and so on, and therefore I don't see what akhena was talking about.

So I believe you should change your loop back to
for(int i = SZ-1; i >= 0; i--)
as you had it before.

You are adding the carry in on line 9, which is fine. So what are you doing on line 15? As far as I can see that line is completely wrong & should just be eliminated (but if you think I'm wrong, tell me why).

Now, after the completion of the loop (after you have stored the highest-order digit in Temp.Ints[0]), you should be checking for overflow, right? That is VERY easy. Do you see how?

As far as Temp.DataSize is concerned: why are you using EITHER DataSize or I2.DataSize to determine this? Neither one is necessarily correct. If you add 9 + 9, you have two operands, both with DataSize==1, but the DataSize of the result is 2, right? Why not just look through Temp.Ints to find the first non-zero element and figure out Temp.DataSize from that?

This post has been edited by r.stiltskin: 29 January 2011 - 11:54 AM

Was This Post Helpful? 0
  • +
  • -

#10 akhena   User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 94
  • Joined: 20-January 11

Re: Int50 Overloaded Functions

Posted 29 January 2011 - 02:49 PM

I was indeed confused by the way the Int50 is built. Sorry if I mislead you. But that made me read more carefully your constructor, and I think that the way numbers are stored is not very helpful. Unless I'm again mistaken, if you give a string "12345" to your Int50 constructor, your Ints[] will look like this :
1234500000000000000000000000000000000000000000000000000

And to me, that is not very useful. I think you should opt for either of the following format :
5432100000000000000000000000000000000000000000000000000
or
0000000000000000000000000000000000000000000000000012345

The second one is the one I believe r.stiltskin is refering to. Either are good. But the one you have right now is not very useful.

In order to achieve that, you should go back to your constructor and read the string backward.

To achieve that, line 114 could be
n = s[sz-1-i] - '0';


sz is the size of your string. So sz-1 is the last digit in your string (so the unit). And you decrement it while you loop on it to finish with the 0th element.

This post has been edited by akhena: 29 January 2011 - 02:57 PM

Was This Post Helpful? 0
  • +
  • -

#11 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Int50 Overloaded Functions

Posted 29 January 2011 - 03:12 PM

Good point, but, given that sz is the length of the string and SZ is the length of the Int array, it might be less confusing to simply fill the array with 0 from Int[0] to Int[SZ - sz - 1] and then continue filling it with the sz values from the input string in normal (left to right) sequence.
Was This Post Helpful? 0
  • +
  • -

#12 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Int50 Overloaded Functions

Posted 29 January 2011 - 03:23 PM

With your current design, there is no place to enter the carry.

You could create a helper function which moves the digits of the number one or more place(s) to the right, freeing up the front. The data size value would be incremented since length is changed.

You could use the function to change the length of unequal sized numbers.
Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Int50 Overloaded Functions

Posted 29 January 2011 - 03:34 PM

View Post#define, on 29 January 2011 - 05:23 PM, said:

With your current design, there is no place to enter the carry.

???

The size of the integer is predetermined (50) and is never resized. Under what circumstances would it ever be necessary to enter a carry?
Was This Post Helpful? 0
  • +
  • -

#14 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Int50 Overloaded Functions

Posted 29 January 2011 - 04:30 PM

View Postr.stiltskin, on 30 January 2011 - 12:34 AM, said:

View Post#define, on 29 January 2011 - 05:23 PM, said:

With your current design, there is no place to enter the carry.

???

The size of the integer is predetermined (50) and is never resized. Under what circumstances would it ever be necessary to enter a carry?


Lol, I was referring to AverageAsian's design, perhaps I should have specified that.
Was This Post Helpful? 0
  • +
  • -

#15 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Int50 Overloaded Functions

Posted 29 January 2011 - 05:19 PM

View Post#define, on 29 January 2011 - 06:30 PM, said:

View Postr.stiltskin, on 30 January 2011 - 12:34 AM, said:

View Post#define, on 29 January 2011 - 05:23 PM, said:

With your current design, there is no place to enter the carry.

???

The size of the integer is predetermined (50) and is never resized. Under what circumstances would it ever be necessary to enter a carry?


Lol, I was referring to AverageAsian's design, perhaps I should have specified that.

I'm referring to AverageAsian's design too. Carry is defined and used locally in the operator+ implementation. I don't see what you have in mind when you suggest "a place to enter a carry".
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2