# C++//Microsoft Visual C++ .NET//Arrays and Sorting

• 09-18-2003
xddxogm3
C++//Microsoft Visual C++ .NET//Arrays and Sort
I was wondering if anybody could help me find the problem in my logic. I'm creating a mathmatics program for adding/subtracting integer arrays. I believe the logic error would be on one of the sorts or in the calculation itself. below is my code for a simple self made sort to reverse the integers.

Code:

```    for (x=Count2;x>=0;x--)     {                            if (!(HugeInteger2[x]=='\r'))           {             ReOrder2[ReOrderCount2]=HugeInteger2[x]-48;             ReOrderCount2++;           }            }```
I feel that the logic on this is sound. Another spot that I'm thinking has issues is this below math equations for addition and subtraction.

Code:

```for(x=0;x<=SIZE;x++) {   if (ReOrder1[x]>='0'&&ReOrder1[x]<='9'       &&ReOrder2[x] >='0'&&ReOrder2[x]<='9')   {       if (ReOrder1[x]=='\0')         Add1=0;       else         Add1=ReOrder1[x]-48;       if (ReOrder2[x]=='\0')           Add2=0;       else           Add2=ReOrder2[x]-48;       Addition[x] = Add1+Add2+AddCarry;       Add++;       if ( Addition[x] > 9 )       {           Addition[x] %= 10;           AddCarry = 1;       }       else           AddCarry = 0;   }        }```
The basic idea of what i'm trying to do is create and input a char array that gets interprited like an integer array (ints stored in ascii value). I do my calculations like equality, greatness and other misc calculations (add, subtract, devide, etc.).

(1) Does this code show any obvious logic error?

(2) Where and how?

:confused:
• 09-18-2003
xddxogm3
source code .cpp
Sorry for the double post.

.cpp SOURCE CODE
• 09-18-2003
ripper079
Hmmm, I´m not really sure I understand what you are doing, but I have a sample code that you might find intresting.
Code:

```#include <iostream> using namespace std; class HugeInt { public:         HugeInt();         ~HugeInt();         void PrintNumbers();         void Set2();         void Set4();         HugeInt operator+(const HugeInt &ref);         HugeInt operator*(const HugeInt &ref);         HugeInt operator=(const HugeInt &ref); private:         int number[30]; }; HugeInt::HugeInt() {         for (int i = 0; i < 30; i++)                 number[i] = 0; } HugeInt::~HugeInt() { } void HugeInt::Set2() {         number[22] = 8;         number[23] = 7;         number[24] = 6;         number[25] = 5;         number[26] = 4;         number[27] = 3;         number[28] = 2;         number[29] = 1;                 //Number 87654321        } void HugeInt::Set4() {         number[20] = 8;         number[21] = 9;         number[22] = 4;         number[23] = 3;         number[24] = 7;         number[25] = 2;         number[26] = 1;         number[27] = 9;         number[28] = 0;         number[29] = 3;         //Number 8943721903 } void HugeInt::PrintNumbers() {         for (int i = 0; i < 30; i++)                 cout << number[i];         cout << endl; } HugeInt HugeInt::operator +(const HugeInt &ref) {         HugeInt HResult;         int HCarry = 0;         for (int i = 29; i >= 0; i--)         {                 HResult.number[i] = (((number[i] + ref.number[i]) % 10) + HCarry);                 HCarry = ((number[i] + ref.number[i]) / 10);         }         return HResult; } HugeInt HugeInt::operator *(const HugeInt &ref) {         HugeInt Result; //Constructor should set all elements in number to 0         for (int y = 29; y >= 0; y--)         {                 HugeInt tempResult;                 int nCarry=0;                 for (int x = 29,a = y; x >= 0; x--,a--)                 {                         tempResult.number[a] = ((number[x] * ref.number[y]) % 10) + nCarry;                         nCarry = ((number[x] * ref.number[y]) / 10);                 }                 Result = Result + tempResult;         }         return Result; } HugeInt HugeInt::operator =(const HugeInt &ref) {         for (int i = 0; i < 30; i++)                 number[i] = ref.number[i];         return *this; } int main() {         HugeInt number2;         HugeInt number4;                number2.Set2();         number4.Set4();         number2.PrintNumbers();         number4.PrintNumbers();         HugeInt multi;         multi = number2 * number4;         multi.PrintNumbers();         return 0; }```
Maybe this will give you some insights on how to solve your problem.
• 09-18-2003
xddxogm3
Thanks for the response
I have some code similar to this already for review. I haven't learned how to use operator overloading yet, so this is a little over my head so far. I will take it into concideration thanks.
This code does spark a question though, and the tutorial on classes do not address these.
Quote:

HugeInt HugeInt::operator +(const HugeInt &ref)
{
HugeInt HResult;
int HCarry = 0;

If I'm reading your code correctly, this means you can instantiate an object of class HugeInt at any point in the code and it is not required to be placed at the begining in main? If I want even I can avoid placing it in main all together. Would I be accurate on this assumption?

Quote:

int main()
{
HugeInt number2;
HugeInt number4;

number2.Set2();
number4.Set4();
Does this part of code state that you are capable of creating multiple instantiations of the single array in the class. In actuallity creating two seperate arrays?
• 09-18-2003
ripper079
Quote:

If I'm reading your code correctly, this means you can instantiate an object of class HugeInt at any point in the code and it is not required to be placed at the begining in main?
You can create an instance of HugeInt after it has been declared and in any memberfunction of it´s class (this case HugeInt).

Code:

```int main() { HugeInt number2; HugeInt number4; number2.Set2(); number4.Set4(); ...```
This means that you are creating two objects(instances) of HugeInt and nothing more. How the implementation (how the class is designed and how it works) looks like is not of intresst here. Once you have declared a class you can create how many objects you want (or until your are out of memory).