Trusted by millions of Kenyans
Study resources on Kenyaplex

Get ready-made curriculum aligned revision materials

Exam papers, notes, holiday assignments and topical questions – all aligned to the Kenyan curriculum.

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

Answer Attachments

Exams With Marking Schemes

Related Questions