### Author Topic: More Code driving me NUTS!  (Read 2223 times)

0 Members and 1 Guest are viewing this topic.

#### Dreyth

• Hero Member
•     • Posts: 2950
• Respect: +994 ##### More Code driving me NUTS!
« on: February 16, 2013, 11:13:27 am »
0
NVM, got it!

So from my other thread... I thought of a VERY EASY addition algorithm (that addes two numbers each stored in their own array, with each digit in one element of the array) that is concise and fast! Here it is:

Code: [Select]
`HugeInteger HugeInteger::add(const HugeInteger &a){    HugeInteger result;    int carry = 0;    for (int i = maxSize - 1; i >= 0; i--){ result.array[i] = (array[i] + a.array[i] + carry) % 10; carry           = (array[i] + a.array[i] + carry) / 10; }    return(result);        }`

Now my friend needs help with his own algorithm. The following is his WORKING code:

Code: [Select]
`hugeInteger hugeInteger::add(const hugeInteger & a){    for (int i = maxSize - 1; i >= 0; i--){ array[i] = array[i] + a.array[i]; if (array[i] >= 10){     int carry = array[i]/10;     int remain = array[i]%10;     array[i] = remain;     array[i-1] = array[i-1] + carry; }    }       return(*this);}`

The program, if run thru his driver, outputs the following:

37137
15934
53071

While his code is correct, the professor wants him to use the HugeInteger class and return an object 'result'. So we did this:

Code: [Select]
`hugeInteger hugeInteger::add(const hugeInteger & a){    hugeInteger result;    for (int i = maxSize - 1; i >= 0; i--){ result.array[i] = array[i] + a.array[i]; if (array[i] >= 10){     int carry = result.array[i]/10;     int remain = result.array[i]%10;    result.array[i] = remain;    result.array[i-1] = result.array[i-1] + carry; }    }       return(result);}`
This code, however, gives this output:

37137
15934
41210611

I don't understand why it would have a different output! It's the exact same algorithm, except that instead of storing all the values in array, we are storing them in result.array instead !!!

the following are his source and driver files:

hugeInteger.cpp
Code: [Select]
`#include "hugeInteger.h"#include <iostream>#include <cstdlib> #include <iomanip> using namespace std;hugeInteger::hugeInteger() {    for (int i = 0; i < maxSize; i++) array[i]= 0;  // defualt constuctor which initializes the       // array to 0}hugeInteger:: hugeInteger(int arr[], int b): size(b) // copy constructor{    if (size > maxSize) size = maxSize;  // if the size is greater than 40, it sets // size to equal maxSize.    for (int i = 0; i < size; i++) array[i] = arr[i];     for (int i = size; i < maxSize; i++) array[i] = 0;      for (int j = maxSize - 1; j >= maxSize - size; j--) array[j] = array[j - maxSize + size];    for (int j = 0; j< maxSize - size; j++) array[j] = 0;}hugeInteger hugeInteger::add(const hugeInteger & a){    hugeInteger result;    for (int i = maxSize - 1; i >= 0; i--){ result.array[i] = array[i] + a.array[i]; if (array[i] >= 10){     int carry = result.array[i]/10;     int remain = result.array[i]%10;    result.array[i] = remain;    result.array[i-1] = result.array[i-1] + carry; }    }       return(result);}const hugeInteger hugeInteger:: operator+(const hugeInteger &a)// overload +{    return (add (a)); } hugeInteger hugeInteger::print() {     bool term = false;    int count;    //  check for 0's and omit using a for loop    for ( int i = 0; i < maxSize; i++ ){ if (array[i] != 0){     term = true;     count = i;     break; }     }    for (int i = count; i < maxSize; i++) cout << array[i];}`
driver.cpp
Code: [Select]
`#include "hugeInteger.h"#include <iostream>#include <string>#include <sstream>#include <cstdlib>#include <iomanip>using namespace std;int main(){    int aop1 = {3,7,1,3,7};    int aop2 = {1,5,9,3,4};    hugeInteger sum; // makes an object using the default contructor      // from .h file, which will eventually hold the      // objects op1, and op2    hugeInteger op1(aop1, 5) , op2(aop2, 5); // two objects using copy contruct    op1.print();    cout << endl;    op2.print();    cout << endl;    sum = op1 + op2;    sum.print();     cout << endl;}`
« Last Edit: February 17, 2013, 11:09:38 pm by Dreyth »
I'm LAKERS from The Vertical Summit