Advertisement

C++ AP question (for HS class) re: bigInt

Started by October 26, 2000 03:09 PM
6 comments, last by Mastiff 24 years, 2 months ago
The assignment: Write a program that adds two integers, both consisting of up to 60 digits each. Use functions where necessary. It sounds simple enough, and I''m sure I should know how to be able to program this by now. My teacher isn''t the most talented, however, and the book is of little help (C++ for You++, AP Ed.). He also refuses to teach us. When he lectures, he simply reiterates what the book tells us. So we''re all rather adrift in this class, especially with this program. Any help would be greatly appreciated. Mastiff
quote: Original post by Mastiff

The assignment: Write a program that adds two integers, both consisting of up to 60 digits each. Use functions where necessary.

Mastiff


Think about another way to store the digits. For example, you could create an new type like:

    struct bigNumber{	int digits[60];	bool positiveOrNegative;];    


Then do operations on a digit by digit basis.


This should be enough to give you some ideas.


-------
Andrew

Edited by - acraig on October 26, 2000 4:26:30 PM
Advertisement
Good Sir:

You = helpful.

Mastiff

(Edited...)

            // Paul Sheckarski// addbig.cpp//// Add two integers each consisting of up to 60 digits.#include <iostream.h>int temp,x;const int MAXDIGITS=60;class bigNum{public:	//accessor	void buildNum(temp);	//public member functions	int addNum(int digits[MAXDIGITS]);	void showNum();private:	int digits[60];	bool isThisPos;};void bigNum::buildNum(int temp){	digits[temp]=x;}void main(){	bigNum bigNumber;		cout << "ENTER NUMBER > " <<;		for (temp=0;temp<MAXDIGITS;temp++)		cin >> bigNumber.buildNum(x);		bigNumber.addNum(digits[MAXDIGITS]);	bigNumber.showNum();}    


I see a few problems with this, though. The maine one is cin will read the entire 60-digit user-input integer as bigNum.digits[0], won't it? Maybe I misunderstood the original suggestion?

Another problem I now see is that this program REQUIRES a 60-digit number instead of allowing the user to input UP TO 60 digits. Damnit, programming hurts my brain.

Edited by - Mastiff on October 26, 2000 5:04:50 PM

Edited by - Mastiff on October 26, 2000 6:49:54 PM
Try reading it in as a 60-character string and then changing each digit to the number it should be. Also, this could be really cool if you used operator overloading Just add 2 numbers with the + sign
- Rick
it would better be:
void addnum(bignum &b);


Since you want each element of the array to hold a value from 0-9, it would be better to use char instead of int for space reasons. And reading a char array off stdin would be best, subtracting 48 (30 hex) from each element would convert the ASCII value to numerical value. This will give you the most significant digit in the first element however...

I don''t want to solve the whole thing for you, but there are a few suggestions.


Mike
"Unintentional death of one civilian by the US is a tragedy; intentional slaughter of a million by Saddam - a statistic." - Unknown
I can see several problems with the code Mastiff, you might wanna try something more like this:

[source]
typedef big_num int[DIGITS];

big_num add_big_num(big_num number1, big_num number2)
{
int i = 0;
big_num temp;

// Gotta make sure you add the elements individually
for (; i < DIGITS; i++)
{
temp = number1 + number2<br><br> // This wont work correctly in some cases, I wont do your<br> // homework for you, but it should show you, always make<br> // sure to account for all possible errors, this has many<br> // such possibilities.<br> if (temp > 9)<br> number1[i+1] = temp - 9;<br> }<br><br> return temp;<br>} </i> <br><br>—————————–<br><br>A wise man once said "A person with half a clue is more dangerous than a person with or without one."<br><br><a href="http://theotherside.com/t-shirts/msbsod.pl">The Micro$haft BSOD T-Shirt</a>
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
Advertisement
Allow the user to enter the number as a string, then parse it into digits (from the right).

------------------------------
BCB DX Library - RAD C++ Game development for BCB
Wow, I''m glad we don''t have any gay things like that in my AP CS class

Actually, we have had some stupid ones, but at least our book doesn''t have a name that is screaming "flaming" so loudly

Excuse me whilst I conquer Earth...

Commander M
(a.k.a. Crazy Yank)
http://commanderm.8m.com
CmndrM@gdnmail.net

This topic is closed to new replies.

Advertisement