fixing array index out of bounds problem

Hey guys, I'm stuck on this assignment and was wondering if you could give me any pointers? The directions are:

Design and implement the class myArray that solves the array index out of bound problem, and also allows the user to begin the array index starting at any integer, positive or negative. Every object of type myArray is an array of 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 componentst 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[12].

I really have no idea how to approach this problem. This is what I have so far:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef H_myArray
#define H_myArray
#include <iostream>
using namespace std;

template <class myArray>
class myArray
{
	public:

	private:

	
	return 0;
};
#endif 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#ifndef H_myArray
#define H_myArray

#define MAX_ARRAY_SIZE 100

template <class DataType>
class MyArray
{
	private:
             DataType arrayData[MAX_ARRAY_SIZE];
             int startIndex;
             int endIndex;

        public:
             //Save values of startIndex and endIndex to be used in checking
             //the parameters when calling arrayInsert() or arrayDelete
             //in case of out-of-bounds indeces passed
             MyArray(int startIndex, int endIndex);
             ~MyArray();
       
            //Insert data into index indexWhereToInsert
           arrayInsert(int indexWhereToInsert, DataType data);
        
           //Find data and delete it from the array
           arrayDelete(DataType data);

          //Display array contents
          displayArray();
        

         //Add more functions you like
         ...
};


This is somehow an example header file for you.
Last edited on
Topic archived. No new replies allowed.