PROGRAM SPECIFICATION For this program, we are going to pretend that C++ has no
ID: 3827603 • Letter: P
Question
PROGRAM SPECIFICATION
For this program, we are going to pretend that C++ has no built-in facility for two-dimensional arrays. It is possible to emulate them yourself with wrapper functions around a one dimensional array.
Consider the following two-dimensional array:
int matrix[2][3];
matrix[0][0]
matrix[0][1]
matrix[0][2]
matrix[1][0]
matrix[1][1]
matrix[1][2]
The two-dimensional array can be mapped to storage in a one dimensional array where each row is stored in consecutive memory locations
int matrix1D[6];
matrix1D
[0][0]
matrix1D
[0][1]
matrix1D
[0][2]
matrix1D
[1][0]
matrix1D
[1][1]
matrix1D
[1][2]
Here, the mapping is as follows:
matrix[0][0] would be stored in matrix1D[0]
matrix[0][1] would be stored in matrix1D[1]
matrix[0][2] would be stored in matrix1D[2]
matrix[1][0] would be stored in matrix1D[3]
matrix[1][1] would be stored in matrix1D[4]
matrix[1][2] would be stored in matrix1D[5]
Write a program that includes the following functions:
int* create2DArray(int rows, int columns);
This function creates a one-dimensional dynamic array to emulate a two dimensional array and returns a pointer to the one-dimensional dynamic array.
rows is the number of rows desired in the two-dimensional array.
columns is the number of columns desired in the two-dimensional array.
Return value: a pointer to a one dimensional dynamic array large enough to hold a two-dimensional array of size rows * columns.
Note that int ptr = create2DArray(2,3); would create an array analogous to that created by int ptr[2][3];
void set(int *arr, int rows, int columns, int desiredRow, int desiredColumn, int val);
This function stores val into the emulated two dimensional array at position desiredRow, desiredColumn. The function should print an error message and exit if the desired indices are invalid.
arr is the one-dimensional array used to emulate a two-dimensional array.
rows is the total number of rows in the two-dimensional array.
columns is the total number of columns in the two-dimensional array.
desiredRow is the zero-based index of the row the caller would like to access.
desiredColumn is the zero-based index of the column the caller would like to access.
val is the value to store at desiredRow and desiredColumn.
int get(int *arr, int rows, int columns, int desiredRow, int desiredColumn);
This function returns the value in the emulated two dimensional array at position desiredRow, desiredColumn. The function should print an error message and exit if the desired indices are invalid.
arr is the one-dimensional array used to emulate a two-dimensional array.
rows is the total number of rows in the two-dimensional array.
columns is the total number of columns in the two-dimensional array.
desiredRow is the zero-based index of the row the caller would like to access.
desiredColumn is the zero-based index of the column the caller would like to access.
Complete the program that invokes all three functions in a streamlined main() function, as usual.
matrix[0][0]
matrix[0][1]
matrix[0][2]
matrix[1][0]
matrix[1][1]
matrix[1][2]
Explanation / Answer
#include<iomanip>
#include <iostream>
using namespace std;
int* create2DArray(int rows, int columns);
void set(int *arr, int rows, int columns,int desired_row, int desired_column, int val);
int get(int *arr, int rows, int columns, int desired_row, int desired_column);
int main()
{int i,j;
int *ptr=create2DArray(10,10);
for(i=0;i<10;i++)
for(j=0;j<10;j++)
set(ptr,10,10,i,j,i*10+j);
cout<<"array: ";
for(i=0;i<10;i++)
{for(j=0;j<10;j++)
cout<<setw(5)<<get(ptr,10,10,i,j)<<" ";
cout<<endl;
}
set(ptr,10,10,10,10,10);
get(ptr,10,10,10,10);
system("pause");
return 0;
}
int* create2DArray(int rows, int columns)
{return new int[rows*columns];
}
void set(int *arr, int rows, int columns, int desired_row, int desired_column, int val)
{if(desired_row<rows&&desired_column<columns&&desired_row>=0&&desired_column>=0)
arr[desired_row*columns+desired_column]=val;
else
{cout<<"invalid index ";
return; //should be system("pause");exit(1) but then can't check both set and get for errors
}
}
int get(int *arr, int rows, int columns, int desired_row, int desired_column)
{if(desired_row<rows&&desired_column<columns&&desired_row>=0&&desired_column>=0)
return arr[desired_row*columns+desired_column];
else
{cout<<"invalid index ";
system("pause");
exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.