this is what I have so far help me. #include #include \"myArray.h\" using namesp
ID: 3627083 • Letter: T
Question
this is what I have so far help me.
#include
#include "myArray.h"
using namespace std;
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++)
cout << 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;
}
the question is
Design and implement the class myArray that solves the array index out of bounds problem and also allows 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; priate error message. Consider the following statements:
myArray list(5);
myArray myList(2,13);
myArray yourList(-5, 9);
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 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 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], ..... youtList[8]. write a programto test the class myArray.
i also have this please help me
//Header the myArray.h
#ifndef H_myArray
#define H_myArray
#include <iostream>
using namespace std;
class myArray
{
public:
// constructors
myArray ( int );
myArray ( int, int );
~myArray();
// subscript operator for non-const objects
int &operator[] (int);
//subcript operator for const objects returns rvalue
int &operator[] (int) const;
private:
int size; // pointer-based array size
int startIndex; //pointer-based array size
//pointer to first element of pointer-based array
int *ptr;
}; //end template class array
#endif
//implementation file
#include <iostream>
#include "myArray.h"
using namespace std;
//constructor
myArray :: myArray ( int si, int s )
{
startIndex = -si;
size = s;
// create space for pointer based array
ptr = new int [ size + startIndex ];
// set pointer-based array element
for ( int i = 0; i < ( s + startIndex ); i++ )
ptr[i] = 0;
} // end function myArray
//constructor
myArray::myArray (int s)
{
startIndex = 0;
size = s;
//crate space for pointer-based array
ptr = new int [size];
//set pointer-based array element
for (int i = 0; i< s; i++ )
ptr[ i ] = 0;
} // end constructor
// destructor
myArray::~myArray()
{
delete [] ptr;
}// end destructor
//overload subscript operator for non-const Arrays;
//reference return creates a modifiable lvalue
int &myArray::operator[] (int subscript)
{
//check for subscript out-of-range error
if (subscript < -startIndex || subscript >= size )
{
cerr << " Error: subscript" <<subscript
<< "out of range" << endl;
exit (1); //terminate program; subscript out of range
} //end if
// reference return
return ptr[ subscript + startIndex ];
} //end function operator[]
//overloaded subscript operator for const Arrays
// const reference return creates an rvalue
int myArray::operator[] (int subscript) const
{
//check for subscript out-of-range error
if (subscript <- startIndex || subscript >= size )
{
cerr << " Error: Subscript " << subscript
<< "out of range" << endl;
exit (1); //terminate program; subscript out of range
}// end if
//returns copy of this element
return ptr[ subscript + startIndex ];
}// end function operator[]
#include <iostream>
using namespace std;
int main() //function main begins program execution
{
//let the user know about the program
cout << " A program to check the functionality "
<< " of the class myArray. ";
//create objects of type myArray
myArray a1 (5);
myArray a2 (2, 13);
myArray a3 (-5, 9);
//insert elements into the object
for (int i=0; i < 5; i++)
a1[1] = ( i * 3 + 65);
//insert elements into the object
for (int i=2; i< 13; i++)
a2[i] = i * 3;
//insert elements into the object
for (int i = -5; i<9; i++)
a3[i] = i * 2;
//print the elements of object
cout << " The elements in the first object are";
for (int i = 0; i < 5; i++)
cout << " myList [" << i << "] = " << a1[i];
//print the elements of the object
cout << " The elements in the second object are";
for (int i = 2; i < 13; i++)
cout << " myList2[" << i << "] = " << a2[i];
//print the elements of the object
cout << " The elements in the third object are";
for (int i = -5; i <9; i++)
cout << " myList3[" << i << "] = " << a3[i];
//you can test index out of bound problem with the help of any of the following statments.
//cin>>a1[5]
//cin>>a2[0]
//cin>>a3[13]
//cin>>a3[-6]
//cin>>a3[10]
cout<< " ";
system ("pause");
return 0; //indicates program executed succesfully
}//end of function, main
Explanation / Answer
please rate - thanks
I don't understand your sample output--sorry I don't work with separate files so you'll have to separate them again
hope this helps
//Header the myArray.h
//#ifndef H_myArray
//#define H_myArray
#include <iostream>
using namespace std;
class myArray
{
public:
// constructors
myArray ( int );
myArray ( int, int );
~myArray();
// subscript operator for non-const objects
int &operator[] (int);
//subcript operator for const objects returns rvalue
int &operator[] (int)const ;
private:
int size; // pointer-based array size
int startIndex; //pointer-based array size
//pointer to first element of pointer-based array
int *ptr;
}; //end template class array
//#endif
//implementation file
//#include <iostream>
//#include "myArray.h"
//using namespace std;
//constructor
myArray :: myArray ( int si, int s )
{
startIndex = si;
size = s-si+1;
// create space for pointer based array
ptr = new int [ size ];
// set pointer-based array element
for ( int i = 0; i <size ; i++ )
ptr[i] = 0;
} // end function myArray
//constructor
myArray::myArray (int s)
{
startIndex = 0;
size = s;
//crate space for pointer-based array
ptr = new int [size];
//set pointer-based array element
for (int i = 0; i< s; i++ )
ptr[ i ] = 0;
} // end constructor
// destructor
myArray::~myArray()
{
delete [] ptr;
}// end destructor
//overload subscript operator for non-const Arrays;
//reference return creates a modifiable lvalue
int& myArray::operator[] (int subscript)
{
//check for subscript out-of-range error
if (subscript < startIndex || subscript > size )
{
cerr << " Error: subscript " <<subscript
<< " out of range" << endl;
system("pause");
exit (1); //terminate program; subscript out of range
} //end if
// reference return
return ptr[ subscript + startIndex ];
} //end function operator[]
//overloaded subscript operator for const Arrays
// const reference return creates an rvalue
int& myArray::operator[] (int subscript) const
{
//check for subscript out-of-range error
if (subscript < startIndex || subscript >= size )
{
cerr << " Error: Subscript " << subscript
<< " out of range" << endl;
system("pause");
exit (1); //terminate program; subscript out of range
}// end if
//returns copy of this element
return ptr[ subscript + startIndex ];
}// end function operator[]
//#include <iostream>
//using namespace std;
int main() //function main begins program execution
{
//let the user know about the program
cout << " A program to check the functionality "
<< " of the class myArray. ";
//create objects of type myArray
myArray a1 (5);
myArray a2 (2, 13);
myArray a3 (-5, 9);
//insert elements into the object
for (int i=0; i < 5; i++)
a1[i] = ( i * 3 + 65);
//insert elements into the object
for (int i=2; i< 13; i++)
a2[i] = i * 3;
//insert elements into the object
for (int i = -5; i<9; i++)
a3[i] = i * 2;
//print the elements of object
cout << " The elements in the first object are";
for (int i = 0; i < 5; i++)
cout << " myList [" << i << "] = " << a1[i];
//print the elements of the object
cout << " The elements in the second object are";
for (int i = 2; i < 13; i++)
cout << " myList2[" << i << "] = " << a2[i];
//print the elements of the object
cout << " The elements in the third object are";
for (int i = -5; i <9; i++)
cout << " myList3[" << i << "] = " << a3[i];
//you can test index out of bound problem with the help of any of the following statments.
//cin>>a1[5]
//cin>>a2[0]
//cin>>a3[13]
//cin>>a3[-6]
//cin>>a3[10]
cout<< " ";
system ("pause");
return 0; //indicates program executed succesfully
}//end of function, main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.