im making a class called BigInt it will store integers in an integer array, 1 digit per array location(just ask if you need me to clearfy any more).....i have to make an addBigInt operation, subtract,setBigInt that will store an integer value into an existing BigInt, e.g. x.setBigInt(-397) replaces the value stored in x.

and a multiplication operation now i just need a little help setting up the addBigInt???

i have a const int SIZE = 10 for now

class is BigInt

public:
i have the constructor BigInt (int = 0);

void addBigInt (const &);
void printBigInt();
void setBigInt();

private:
int x ;

BigInt::BigInt(int y) // stores integer in integer array one digit at a time
{
for(int i = 0; i < SIZE; i++)
{
x = y % 10;
y /= 10;
}
}

this is mainly what i got so far i just need help with the adding method then maybe i can use that example to carry me through the other methods..

void addBigInt(const &)
{
kinda lost what i should do here

this is my first thread want to say love this site and it has helped me in the past thank you guys and gals *cheers*

Recommended Answers

All 17 Replies

First, be clear what each element of the x[] array stores. A number from 0 to 9? A number from 0 to 2^31 - 1? A number from -2^31 to 2^31 - 1 (i.e. the entire integer range). If it's 0 through 9, everything is going to be done differently than if it's the entire integer range. Where do you keep track of positive versus negative? Make all of this explicit to the code reader. There are many ways to do this.

From the code, I am assuming every element stores an integer from 0 to 9.

I always start out by writing a toString() function. You'll need it eventually anyway, so write it now so you can test everything as you go for debugging purposes. Adding, subtracting, multiplying all come after writing your toString() function and constructors IMO.

[Edit]
Looks like you are naming toString() printBigInt(). That's fine.
[/Edit]

1 digit per array location(just ask if you need me to clearfy any more)

I missed this part in the original post. So I was correct? Each element stores a 0 through 9? Where does the negative sign get stored, or is that handled?

thank you and no i am not to deal with negative numbers and i am just starting small with 0-9 i should be able to change that with to 0-199 once code is working im trying to start small....i'm declaring the size of the array with CONST SIZE = 10 OR 20 ECT....

The prototype for addBigInt is illegal, the parameter has to have a type. Also, shouldn't you be passing in two BigInt classes if you want to add them, and return one two.

Something like:

BigInt addBigInt(const BigInt& bi1, const BigInt& bi2);

>> no i am not to deal with negative numbers

Your example shows a negative number:

e.g. x.setBigInt(-397)

As far as adding, dust off your old arithmetic books and pretend you're a third grade teacher teaching the kids how to add/when to carry, etc. Break it down step by step. The "ones place", will have an index, as will the "tens-place", etc.

And make sure you're clear on how things are stored(is 0 the most significant bit or the least significant bit?)

i.e. Is 385 this?

x[0] = 0;
x[1] = 0;
x[2] = 0;
x[3] = 0;
x[4] = 0;
x[5] = 0;
x[6] = 0;
x[7] = 3;
x[8] = 8;
x[9] = 5;
x[9] = 0;
x[8] = 0;
x[7] = 0;
x[6] = 0;
x[5] = 0;
x[4] = 0;
x[3] = 0;
x[2] = 3;
x[1] = 8;
x[0] = 5;

Either way's fine as long as you're consistent. Anyway, write the function that displays first since you'll be using it all over the place.

thank you guys so much you have made some very valid points expecially with the addBigInt needing two.....this is aweasome site...*cheers*

The function addBigInt() is a member function. As such, it only has one (1) argument, as you have written. That argument receives the right-hand value from the statement. The other argument is the implied "this" pointer and need not be explicitly declared as part of the function.

Oh, your right Fbody. It looked to me like it was prototyped outside the class because there wasn't any indentation before it, sorry.

thank you again i am still just a beginner here....do i need to call a function addBigInt on my main cpp page if so does that were i put my math code to addBigInt to the second array .....i would also like to get this printBigInt member class right will i use a for loop in my printBigInt or my addBigInt function....thanks guys i'm not trying to just get the answer i want to learn but i think i learn better that way....i will master this DANG IT lol

>> do i need to call a function addBigInt on my main cpp page


That completely depends on how you set things up. Here is one possible main:

int main()
{
    BigInt a(5);
    a.printBigInt(); // prints 5
    BigInt b(4);
    b.printBigInt(); // prints 4
    a.addBigInt(b);
    a.printBigInt(); // should display 9.
    return 0;
}

>> if so does that were i put my math code to addBigInt to the second array .....

???
The implementation of the addBigInt function would presumably go in BigInt.c:

void BigInt::addBigInt(const BigInt& bi)
{
   // any array adding goes here
}

>> i would also like to get this printBigInt member class right will i use a for loop in my printBigInt

Most likely you'll use a loop in printBigInt.


>> or my addBigInt function

Most likely you'll use a loop in the addBigInt function too.

thanks *cheers*

i'm having trouble with that though mainly with my print BigInt now because i thought i would set it up like you did with the int main section to test if i could but my problem is with the printBigInt......lets say i put two integer arrays on the main cpp like you did calling my protoytpe printBigInt() afterwards that function printBigInt is going to have to stay constant with what is in the body of printBigInt for i will use it after i add, subtract, multiply and use the setBigInt method.....ARHHH this is so easy i just make it over complicated.

should it look like this?

void BigInt::printBigInt()
{
//for loop
}
thanks again

>> ARHHH this is so easy

It's easy once you're used to it. Not so easy before that. Don't beat yourself too bad. In time it will seem obvious and easy, but think about all the things that seem easy now. They weren't easy when you first started.

>> i just make it over complicated.

See above (and below).

>> should it look like this?

void BigInt::printBigInt()
{
  //for loop
}

Yes it should.


Here's a freebie.

void BigInt::printBigInt()
{
  for(int i = 0; i < 10; i++)
  {
    cout << x[i];
  }
}

That's it!

Depending on how you are storing everything, that could possibly display things backwards. If so, change the loop so it displays in the right order.

And change my main program so that you add some cout << endl; lines. Otherwise the displays will run into each other and be less readable.

lol your aweasome bro thanks and it is all about reapation and practice right.....i love C++ always learning

well im still running into a little trouble with my addBigInt member function i get a compilier error when i run it saying int assumed but its a void not returning a value right.....

public:
void AddBigInt(const &)// compilier says there is something wrong

there are others but once i get this i will figure the others will be simaliar...

so i move on to the class function like this

void BigInt::addBigInt(const BigInt& bi)
{
//the addition code
}
add BigInt example x.addBigInt(y) will add BigInt y to x. i have the header file right i know the basic setup public then private then i write the function code at this level shouldn't be hard but its aggrivating

thanks for your time

as I said before, the parameter has to have a type. Thats why the compiler was saying "int assumed", because it didn't know what type the parameter was supposed to be.

void AddBigInt(const BigInt&);

that was it thanks man

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.