Write a program using C++ language that creates a two-by-three two-dimensional safe array of integers.Demonstrate that it works.

      

Write a program using C++ language that creates a two-by-three two-dimensional safe array of integers.Demonstrate that it works.

  

Answers


Davis
// A simple bounded two-dimensional array example.
#include
#include
using namespace std;
class array {
int isize, jsize;
int *p;
public:
array(int i, nt j);
int &put(int i, int j);
int get(int i, int j);
};
array::array(int i, int j)
{
p= new int[i*j];
if(!p) {
cout << "Allocation error\nt(1);
}
isize = i;
jsize = j;
}
// Put something into the array.
int &array::put(int i, int j) {
if(i<0 || i>=isize || j<0 || j>=jsiz << "Bounds error !!!\n";
exit(1);
}
return p[i*jsize + j]; // return reference to p[i]
}
// Get something from the array.
int array::get int j)
{
if( i<0 || i>=isize || j<0 || j>=jsize) {
cout << "Bound error!!!\n";
exit(1);
}
return p[i*jsize +j]; // return chat main()
{
array a(2, 3);
int i, j;
for(i=0; i<2; i++)
for( j=0; j<3; j++)
a.put(i, j) = i+j;
for(i=0; i<2; i++)
for(j=0; j<3; j++)
cout << a.get(i, j << ' ';
// generate out of bounds
a.put(10, 10);
return 0;
Githiari answered the question on May 29, 2018 at 18:59


Next: Rework the strtype class below so it uses new and delete. #include #include #include using namespace std; class strtype { char *p; int len; public: strtype(char *ptr); ~strtype() }; strtype::strtype (char *ptr) { len =...
Previous: What are the disadvantages of strip grazing?

View More Computer Science Questions and Answers | Return to Questions Index


Exams With Marking Schemes

Related Questions