Design and implement the class myArray that solves the array index out of bounds
ID: 3770539 • Letter: D
Question
Design and implement the class myArray that solves the array index out of bounds 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 type of 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 list 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 and then alter it in the following manner:
Write a main program that tests these functions using arrays of 3 different types of your choice. Fill your 3 arrays with random values for testing.
For each array, you will print the array, find the average, and then print out the average.
Make sure you use meaningful variables names, and label your tests in your output.
PLEASE DO NOT ANSWER THIS QUESTION UNLESS YOU ARE GOING TO COMPLETE ALL OF IT.
Explanation / Answer
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define MAX 1<<31
template <class T>
class myArray
{
//member items
private:
T* head;
int offset,size;
public:
//constructor
myArray(int first,int last = MAX)
{
if(last == MAX)
{
offset = 0;
size = first;
}
else
{
offset = first;
size = last-first;
}
head = (T*)malloc(size*sizeof(T));
}
//destructor
~myArray()
{
free(head);
}
T& operator[](int i)
{
if(i<offset||i>=(offset+size))
{
cerr<<"Index out of bound: Accessed "<<i<<" in Array["<<offset<<".."<<offset+size-1<<"] ";
exit(0);
}
return head[i+offset];
}
int mySize()
{
return size;
}
};
int main()
{
srand(time(NULL));
int i;
myArray<int> list(5);
myArray<float> mylist(2,13);
myArray<int> yourlist(-5,9);
cout<<" Randomly assigning values to list array ";
for(i=0;i<5;i++)
{
list[i] = rand()%10;
cout<<"Position "<<i<<" has random element "<<list[i]<<endl;
}
cout<<"Contents of list array:: ";
for(i=0;i<5;i++)
cout<<list[i]<<" ";
cout<<endl;
cout<<" Randomly assigning values to mylist array ";
for(i=2;i<13;i++)
{
mylist[i] = rand()%10;
cout<<"Position "<<i<<" has random element "<<mylist[i]<<endl;
}
cout<<"Contents of mylist array:: ";
for(i=2;i<13;i++)
cout<<mylist[i]<<" ";
cout<<endl;
cout<<" Randomly assigning values to yourlist array ";
for(i=-5;i<9;i++)
{
yourlist[i] = rand()%10;
cout<<"Position "<<i<<" has random element "<<yourlist[i]<<endl;
}
cout<<"Contents of yourlist array size:: ";
for(i=-5;i<9;i++)
cout<<yourlist[i]<<" ";
cout<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.