GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Jun-2008, 05:10
mmboko mmboko is offline
New Member
 
Join Date: Jun 2008
Posts: 1
mmboko is on a distinguished road

Define a class Biginteger that stores arbitrary large integers


Hi guys ... I have problems making one task ... pls somebody help with it cause i have to present it in 2 days and i cant make it

Here is the task :

Define a class Biginteger that stores arbitrary large integers, by keeping their digits in a dynamically allocated array of integers.
Supply the "big three" memory management functions. Use this class to demonstrate
(a) the difference between initialization
Biginteger s;
Biginteger t = s;
and assignment
Biginteger s;
Biginteger t;
s = t;
(b) the fact that all constructed objects are automatically destroyed
(c) the fact that the copy constructor is invoked if an object is passed by value to a function
(d) the fact that the copy constructor is not invoked when a parameter is passed by reference
(e) the fact that the copy constructor is used to copy a return value to the caller.
Supply a constructor Biginteger(string) that reads a sequence of digits from a string. Overload + and - operators to add and subtract the digit sequences. Overload the stream operators << and >>. Demonstrate all these functions and operators.

it is insane pls someone help me with it !
thank you very much !
  #2  
Old 03-Jun-2008, 05:26
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: I am stuck ... can someone assist me pls !


Quote:
Originally Posted by mmboko
Define a class Biginteger that stores arbitrary large integers, by keeping their digits in a dynamically allocated array of integers.
As with most problems, break it down into more manageable parts.

Fundamentally, you are wanting abstract arithmetic of arbitrary precision. So, the first thing you need to do is figure out how to detect overflow & deal with it accordingly. Once you have resolved this in your head, the rest will being falling into place.

If you are under a stringent deadline, you should begin now as there are several parts to the implementation.
  #3  
Old 03-Jun-2008, 09:25
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Define a class Biginteger that stores arbitrary large integers


It's really best for YOU to try creating the class. Then, when you get stuck, post what you have so that we can help. Maybe start with something like this:
CPP / C++ / C Code:
class BigInteger
{ private:
    int* Digits; // dynamic array of digits
    int Length; // length of the above array

  public:
    BigInteger(); // default constructor
    BigInteger( int ); // constructor that takes an int as a parameter
    BigInteger( BigInteger& ); // copy constructor
    ~BigInteger(); // destructor

};
  #4  
Old 04-Jun-2008, 03:00
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Define a class Biginteger that stores arbitrary large integers


As you all know, unsigned int has range 0 to 65536 and this use 4 bytes in most platforms.

Try to relate this with length memory you have allocated.

I also want to know how.

This overflow problem will get solved once you using character or string.

The copy constructor should return reference and not by value.

Overload + and - as below :

Code:
BigInteger& operator+(BigInteger& rhs); BigInteger& operator-(BigInteger& rhs);

If these two function overload inside a class, then take one argument, else outside of a class then take two arguments.
  #5  
Old 04-Jun-2008, 05:51
mamntc02 mamntc02 is offline
Junior Member
 
Join Date: Mar 2008
Location: Barcelona - Catalonia
Posts: 57
mamntc02 has a spectacular aura about

Re: Define a class Biginteger that stores arbitrary large integers


Quote:
Originally Posted by Peter_APIIT
As you all know, unsigned int has range 0 to 65536 and this use 4 bytes in most platforms.
Well, that's not true. If an unsigned int takes 4 bytes, its range is from 0 to 2^32 -1 = 4294967295.

Anyway, I guess you can't make that kind of assumption, if you don't know the platform.

If you only need to implements addition and subtract operation, I'd follow fakepoo tip, and operate digit by digit. For example, as Peter suggest:
CPP / C++ / C Code:
class BigInteger{
	private:
	int length;
	int* digits;

	public:
	// Other constructors
	BigInteger(int*); //creates BigInteger from int array.
	
	BigInteger operator+(BigInteger& rsh){
		int len = length > rsh.length ? length + 1: rsh.length + 1;
		int carry = 0;
	
		// Create temp arrays to align data
		int* temp1 = new int[len];
		int* temp2 = new int[len];
		int* tempInt = new int[len];
		
		// clean memory
		memset(temp1,0,sizeof(int)*len);
		memset(temp2,0,sizeof(int)*len);
		
		// Align positions
		memcpy(&temp1[len-length -1], digits, length);
		memcpy(&temp2[len-rsh.length -1], rsh.digits, rsh.length);
	
	  // Operate
		for (int i = 0; i < len; i++){
			tempInt[i] = temp1[i] + temp2[i] + carry;
			if (tempInt[i] > 10){ 
				carry = 1;
				tempInt[i] = tempInt[i]%10;
			}
			else carry = 0;
		}
		
		BigInteger bi = new BigInteger(tempInt);		
		return bi;
	}
};
This is just an example, there are missing stuff, and it may not even compile, and have some errors. It's just to give and idea about implement this operator.

Regards.
__________________
Please, correct me if I'm wrong, and sorry for my english ;)
  #6  
Old 04-Jun-2008, 09:04
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Define a class Biginteger that stores arbitrary large integers


Found a small problem in your function. Here is probably what you want:
CPP / C++ / C Code:
//BigInteger bi = new BigInteger(tempInt);
BigInteger bi(tempInt);
return bi;
  #7  
Old 04-Jun-2008, 09:29
mamntc02 mamntc02 is offline
Junior Member
 
Join Date: Mar 2008
Location: Barcelona - Catalonia
Posts: 57
mamntc02 has a spectacular aura about

Re: Define a class Biginteger that stores arbitrary large integers


Uppss!!! Thanks
__________________
Please, correct me if I'm wrong, and sorry for my english ;)
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I'm stuck in a problem.... plzzzzzz help sool87 C++ Forum 12 09-Nov-2007 06:40
Stuck on Codes joesmithf1 C++ Forum 2 10-Jun-2007 22:11
Stuck error C2275 chrisn42 C++ Forum 1 07-Apr-2007 21:20
Stuck on C Programming PEANUTS C Programming Language 2 17-Jan-2007 14:58
Stuck daking_09 C++ Forum 4 03-Apr-2006 10:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:40.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.