Site hosted by Angelfire.com: Build your free website today!

Program 42

C++

K. Dzwonkiewicz     Using apvector

 

Gigantic Integer

With the use of the apvector concept create your own number type, sort of…

 

Have 2 apvectors filled randomly with numbers (in the range 0-9 per location).

Have a third vector, max size 31, that will be the result of adding the corresponding elements of array#1 plus array#2. It won’t always use the full 30.

Each RUN will first randomize sizes for each array on that run ( size range 5-30).

The apvectors won’t necessarily be the same length.

 

Add the two apvectors as if they were single numbers

 

Sample run

FIRST # 98765432543256765436786543

Second#           145634598877126453761

              +___________________________

              98756578177855642563240204

 

You need to use the size to find out how many times to loop through. Remember the array will need to be added from location size back to 1. During each pass, add one cell to another cell (same subscript)

…num3[x]=num1[x]+num2[x]. putting the sum into the third apvector. If the sum exceeds 9, you must carry a one to the next cell(moving left) into the top apvector. This could be done by a nested loop that is branched to or in a function.

Don’t forget that the carrying to one cell may also make this cell need to carry as well. Find a smooth way to do this. (hint: initialize 3rd array to zeros, if you need to carry a one…place it in the 3rd array’s upcoming cell).