Hello, can someone help me write this C++ program:

An array can be used to store large integers one digit at a time. For example, the integer 1234 could be stored in the array a by setting a[0] to 11,
a[1] to 2, a[2] to 3, and a[3] to 4. However, for this project, you might find it easier to store the digits backward, that is, place 4 in [0],
place 3 in a[1], place 2 in a[2], and 1 in a[3];
Design, implement, and test a class in which each object is a larger integer with each digit stored in a separate element of an array.
You'll also need a private member variable to keep track of the sign of the integer (perhaps a boolean variable).
The number of digits may grow as the program runs, so that the class must use a dynamic array.

Hmmm

This is A WAY of going about it. You don't have to follow this word-for-word. This will give you an idea of what you can do.

--

You can make a class called BigInt that accepts a const char* or string as a number (or int), converts whatever case into individual digits in an array that is initially sized to be big enough for the first number.

Obviously have a flag (bool) called positive and set it based on the user's input (if the user inputs a string with a minus sign as its first character, or the user inputs a negative int, set the flag accordingling).

Create a set method that enables you to "set" the BigInt (in fact, you can simply have the constructor call the set method and put the work of assigning the number there). If the new number to be assigned is bigger (in terms of digits, not value!) than the old number, you'll have to resize the array to make up for that. You have two variables (one called size, and one called capacity) to keep track of the current size of the BigInt and to track whether or not the array needs to be resized.

So in all it is highly recommended that you have variables that cover these requirements-

*Array of ints
*ArraySize
*Capacity
*Positive flag
*3 different versions of the public function 'set' (one that accepts const char*, one that accepts string and finally one that accepts an integer).

Edit: Remember! When accepting string input you will need to find out where the numbers start on the ASCII chart so you can subtract the character by the right number and get the appropriate number into the array. Example:

const int ASCII = 48;
//... 48 on the ASCII chart is char '0'

string num;
//... let num be the number (in string represensation)

int n;
//... let n be the determined required size for the array]

int* intArr = new int[n];

// Assuming our input was a string '12345'

for (i=0; i < num.length(); i++)
{
   intArr[i] = static_cast<int>(str[num.length() - 1 - i]) - ASCII;
}


// intArr[0] == 5
// intArr[1] == 4
// intArr[2] == 3
// intArr[3] == 2
// intArr[4] == 1

If conversion from char to int doesn't work (or isn't recommended by your professor) via static cast then you can use an old-fashioned (int) cast on the character, though I've heard it's not recommended. There should also be library functions supplied to convert ascii to integers.

http://www.codeguru.com/forum/showthread.php?t=231054

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.