954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Dynamic Char Array Questions

Im trying a practice problem from a textbook and here is my attempt at it, was wondering if I could get some advice both with structurally how to approach the problem, efficency, and any words of wisdom on dynamic memory. This is my study for my final exam which so any comments will be greatly appreciated.

Problem:
Create a class with a dynamic array of unsigned char holding 0-9 that can make an arbitary large integer. Create operator overloading functions to pair.

Code:

//LongInt.h

#ifndef _LONGINT_H
#define _LONGINT_H

class LongInt
{ 
  public:
  LongInt(int number, int size); //constructor
  ~LongInt();                    //destructor
  
  private:
  int IntSize;                   //holds size of array
  char[]* LongArr;                //dynamic array of integers 0-9 
  int getInt();                  //turns array into int
       
};

#endif
//LongInt.cpp

#include "LongInt.h"

LongInt::LongInt(number, size)
{
   IntSize = size;
   LongArr = new unsigned char* [IntSize];
   numStr = new string(number)
   for(int i=0; i<IntSize; i++){ *LongArr[i] = numStr[i]; }
}

LongInt::~LongInt()
{
   for(int i=0; i<IntSize; i++){ *LongArr = 0; }            
}

int LongInt::getInt()
{
  s = new string;
  for(int i=0; i<IntSize; i++) { s.append(LongArr[i]); }
  delete s;
  
  i = new int(s);
  return i;
}

LongInt operator+(const LongInt &L1, const LongInt &L2)
{
  new int* int1  = L1.getInt();
  new int* int2  = L2.getInt();
  new int newInt = *int1 + *int2;
  
  return LongInt(newInt);        
}
TheSassyDragon
Newbie Poster
22 posts since Dec 2010
Reputation Points: 10
Solved Threads: 2
 

Have you tried compiling it? There's quite a few syntactical liberties in there. :)

char[]* LongArr;

is not valid syntax.

Where does numStr in the constructor come from?

Where does i come from in getInt() ?

new int* int1

new doesn't belong on the left side of that expression

I think some of the ideas are starting to come together, but study some examples from your text or otherwise and take another crack at it.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

You never actually stated a question...??? What do you actually need help with? If you post code you also need a question to go along with it.

hag++
Junior Poster
197 posts since Jan 2010
Reputation Points: 34
Solved Threads: 31
 

What would be the basic syntax or code to create a dynamic array of characters where each pointed to an integer?

TheSassyDragon
Newbie Poster
22 posts since Dec 2010
Reputation Points: 10
Solved Threads: 2
 

char * longInt = new char[x];

Now longInt is a char pointer pointing to the first element of a container of x number of elements of type char. That means that longInt acts like an array, though technically it isn't an array, but that is a discussion I still don't understand completely. The number x must, in the end analysis, end up being a positive integer, but it could be any positive integer from 1 to the maximum value of type int.

Now in order for longInt to be used like a number you need to be able to convert each element in longInt from a char/digit to an int and back again. That will be another step in your program.

Then in order to do things like add two "longInts" together you need to be able to "carry" a one to another element in the array if the sum of the two "characters" with the same index in each of the two arrays is over 9.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: