lilmaniac2
5,000+ posts
Ask Me about SI Mag V4!
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 list (5); // Line 1
myArray myList (2,13); // Line 2
myArray 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.
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 list (5); // Line 1
myArray myList (2,13); // Line 2
myArray 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.
Code:
/******************************************************************************/
/*********************************Chad Hicks***********************************/
/*************************** Final Exam Prob ****************************/
/**************************Written Using Dev-C++ Ver4**************************/
/************************* Written on November 8th 2007************************/
/**************************For Math 240 Alan Smothers**************************/
/*****************************At Concord University****************************/
/******************************************************************************/
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
//template<class Type>
class myArray
{
friend ostream& operator<<(ostream&, const myArray);
friend istream& operator>>(istream&, const myArray &);
public:
int *stuff;
int diff;
int holder;
myArray (int index)
{
stuff = new int[index];
}
myArray (int index1,int index2)
{
diff= index2-index1;
stuff = new int[diff];
}
myArray operator[](int index)
{
return stuff[index];
}
myArray operator+(myArray);
myArray operator= (myArray);
};
int main()
{
myArray list1(5);
myArray list2(5);
int i;
cout<<"list1 : ";
for (i = 0; i<5; i++)
cout<<list1[i]<<" ";
cout<<endl;
cout<<"Enter 5 integers: ";
for (i=0; i<5; i++)
cin>>list1[i];
cout<<endl;
cout<<"After filling list1: ";
for (i=0; i<5; i++)
cout<<list1[i]<<" ";
cout<<endl;
list2 = list1;
cout<<"list2: ";
for (i = 0; i<5; i++)
cout<<list2[i]<<" ";
cout<<endl;
cout<<"Enter 3 elements: ";
for (i=0; i<3; i++)
cin>>list1[i];
cout<<endl;
cout<<"First three elements of list1: ";
for (i=0; i<3; i++)
cin>>list1[i];
cout<<endl;
myArray list3(-2, 6);
cout<<"list3: ";
for (i=-2; i<6; i++)
cout<<list3[i]<<" ";
cout<<endl;
list3[-2]=7;
list3[4]=8;
list3[0]=54;
list3[2]=list3[4]+list3[-2];
cout<<"list3: ";
for (i=-2; i<6; i++)
cout<<list3[i]<<" ";
cout<<endl;
return 0;
}
ostream& operator<<(ostream& osObject, myArray& cObject)
{
return osObject;
}
istream& operator>>(istream& isObject, myArray& cObject)
{
return isObject;
}
myArray myArray::operator= (myArray otherList)
{
return otherList;
}
myArray myArray::operator+ (myArray param)
{
myArray temp(5);
temp.holder= holder+param.holder;
return temp;
}
