"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!

New Topic/Question
Reply




MultiQuote


|