1 Replies - 35870 Views - Last Post: 14 March 2008 - 01:32 AM Rate Topic: -----

#1 wisheshewasleet   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 17-September 07

Array index out of bounds

Post icon  Posted 13 March 2008 - 01:35 PM

hey everyone, I recieved this problem for homework:
"Recall that in C++ there is no check on an array index out of bounds. However, during program execution, an array index out of bounds can cause serious problems. Also, in C++ the array index starts at 0.

Design and implement the class myArray that solves the array index out of bound problem, and also allow the user to begin the array index starting at any integer, positive or negative. Every object of the type myArray is an array of the type int. During execution, when accessing an array component, if the index is out of bounds, the program must terminate with an appropriate error message. Consider the following statements:



myArray<int> list (5); // Line 1

myArray<int> myList (2,13); // Line 2

myArray<int> yourList (-5,9); // Line 3



The statement in Line 1 declares list to be an array of 5 components, the component type is int, and the components are: list[0], list[1], , list[4]; the statement in Line 2 declares mylist to be an array of 11 components, the component type is int, and the components are myList[2], myList[3], ., myList[12]; the statement in Line 3 declares yourList to be an array of 14 components, the component type is int, and the components are: yourList[-5], yourList[-4], ., yourList[0], ., yourList[8]. Write a program to test the class myArray.
"

so far, here is my code:
class myArray
{
public:
	int *list;
	int size;
	int startIndex;
	myArray(int _size);
	myArray(int _size, int startIndex);
	
};

myArray::myArray(int _size)
{
	if(_size<0)
	{
		while(_size<0)
		{
			cout<< "The array must be at least 1 component long." << endl;
			cin >> size;	
		}
	}
	else
		size=_size;
	
	list=new int[size];
	
};

myArray::myArray(int _size, int startIndex)
{
	if(_size<0)
	{
		while(_size<0)
		{
			cout<< "The array must be at least 1 component long." << endl;
			cin >> size;	
		}
	}
	else
		size=_size;
	
	list=new int[size];

	for(int i=0; i<size; i++)
	{
		list[i]=list[i+startIndex]
	};
	
};



my problem is in my for loop. I'm not sure how to change the index from 0 to another number. Should I be using a template?
I'm a bit lost, heh, any help would be appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Array index out of bounds

#2 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: Array index out of bounds

Posted 14 March 2008 - 01:32 AM

well, to have that king of indexing you need to do the operator overloading of [].

in your second constructor you will create an array of size-startindex components [e.g. 13-2=11 and 9-(-5)=14] and store start index in your member variable.

Now write non static member functions with single parameter, to overload [] operator. It will look something like this

int& MyArray::operator[](const int location)



inside this function just subtract the start index from location and return that location back.
[e.g. if location = -4 and start index = -5 then actual index = -4-(-5)=1 so return list[1] ].

That's it, I hope this will solve your problem. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1