What if c++ had no built in facility for 2D array? It is possible to emulate the
ID: 3624951 • Letter: W
Question
What if c++ had no built in facility for 2D array? It is possible to emulate them yourself with wrapper functions around a 1D array. Consider the following 2D array:int matrix[2][3];
The 2D array can be mapped to storage in a 1D array where each row is stored in consecutive memory locations (your compiler actually does something very similar to map 2D arrays to memory).
int matrix1D[6];
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]
Based on this idea, complete the definitions for the following functions:
int* create2DArray(int rows, int columns);
This creates a 1D array to emulate a 2D array and returns a pointer to the 1D array. rows is the number of rows desired in the 2D array. columns is the number of columns desired in the 2D array.
Return value: a pointer to a 1D array large enough to hold a 2D 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 stores val into the emulated 2D array at position desiredRow, desiredColumn. The functions should print an error message and exit if the desired indices are invalid. arr is the 1D array used to emulate a 2D array. rows is the total number of rows int the 2D 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 returns the value in the emulated 2D array at position desiredRow, desiredColumn. The functions should print an error message and exit if the desired indices are valid. arr is the 1D array used to emulate a 2D array. rows is the total number of rows in the 2D array. columns is the total number of columns in the 2D 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.
Create a suitable test program that invokes all 3 functions in C++.
Explanation / Answer
please rate - thanks
#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.