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

This is a discussion on C++//Microsoft Visual C++ .NET//Arrays and Sorting within the C++ Programming forums, part of the General Programming Boards category; I was wondering if anybody could help me find the problem in my logic. I'm creating a mathmatics program for ...

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Question 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?


    Last edited by xviddivxoggmp3; 09-18-2003 at 05:55 AM.

  2. #2
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Arrow source code .cpp

    Sorry for the double post.


    .cpp SOURCE CODE
    Attached Files Attached Files

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    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.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  4. #4
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    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.
    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?

    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?
    Last edited by xviddivxoggmp3; 09-18-2003 at 04:54 PM.

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    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).
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  2. Sorting vector<MyClass>
    By jmgk in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2006, 09:43 PM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 06:50 PM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 09:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21