Friday, July 31, 2009

How do I make an array with no determined size in C++?

This is the prompt: "Write a program that asks the user to enter a series of one-digit numbers (you will need to do error checking). When they have finished (you will have to determine how the user indicates they are finished), print out how many of each number the user entered."





We are supposed to use an *array* to store the user entered numbers, but I can't figure out how to make an array of an indetermined size. (IE numarray[?] )

How do I make an array with no determined size in C++?
You don't need an undetermined-size array for this task :)


Just create an array of COUNTERS. It will be 10 counters (as there are 10 possible one-digit numbers) and each time user enters the number increase the corresponding counter. GL!





Otherwise, if you want to store all the user's input anyway, you can use the following simple class I wrote for you:





class UndeterminedArray


{


private:


int m_Count;


int * m_Data;


public:


UndeterminedArray()


{ m_Count = 0; m_Data=0; }


~UndeterminedArray()


{ delete[]m_Data; m_Count=0; m_Data=0; }


void Add ( int inNewValue )


{


int * temp = new int [ m_Count+1];


if(m_Data) memcpy(temp,m_Data,m_Count*sizeof(int));


delete [] m_Data;


m_Data = temp;


m_Data[m_Count++]=inNewValue;


}


int GetCount ( void ) const


{ return m_Count; }


int operator[] ( int inIndex )


{


if(inIndex%26lt;0 || inIndex%26gt;=m_Count) return 0;


return m_Data[inIndex];


}


};
Reply:main(int argc,char argv[])


Use the parameters, if you do not understand may be you can contact a freelance programmer to help your out. Check websites like http://getafreelnacer.com/
Reply:First you have to create a limked list class using pointer where each elememt will store a one digit number and a pointer to the next element in the list. You have to create a method to add element at the end of the list. You can create an instance of that class within the main function and use pointers for storing %26amp; retrieving digits.
Reply:If they are one digit numbers there can only be 10 elements in the array.





int array [10] ;





If you read as a characters, you need to check that the value is in the range '0' ... '9'.





If you read as integer, you need to ceck the value is in the range 0 ... 9 .
Reply:u want an array of undetermined size.


I suggest you to use dynamic array with the help of pointers.


If u really want its solution using that let me know I will post that
Reply:im not sure if you are talking about this http://www.chips.navy.mil/archives/99_ju... or not ive always use a high determing # to make it easy like 50 or 60 even but you can try list array or smart array also. sorry i cant hellp more
Reply:You cannot make an array of undetermined size. That’s another way of saying you want memory, but you won’t tell the computer how much. The computer can’t handle that.





Ideally, you should use the stl vector for this. That is one of the ideas behind using C++ anyway. If you really want to use arrays, a linked list of arrays is probably the smartest idea.





Your other option is to allocate a really really really large array. This isn’t as failsafe as the linked list of arrays (or the vector) but your homework probably isn’t testing for absolute robustness here.
Reply:Make it easy on yourself, make its size 50 or 60





then a loop and get the loop to exit once the user type end or -1 or whatever.





if you wanna go the long way, then take a look at ArrayList but I think the example is way too simple to use arraylist
Reply:Either use pointers,or store the digits individually and then add them later when the user finishes...it sounds lame,but I think it works....


No comments:

Post a Comment