In C++, how can I update a value in a dynamic two dimensional array? See below f
ID: 3684929 • Letter: I
Question
In C++, how can I update a value in a dynamic two dimensional array? See below for sample array initialization, initial fill, and print. I'm really just stuck on how to do an updateValue function.
#include
using namespace std;
/*********************************************************************
** Function: init
** Description: Initializes the memory of the 2-d array
** Pre-Conditions: x & y > 0
*********************************************************************/
char **init(int x, int y){
char **temp;
temp = new char*[y]; // creating first chunk of memory
for(int i = 0; i < y; i++)
temp[i] = new char[x]; // creating the rest
return temp; //returning the new array
}
/*********************************************************************
** Function: Fill
** Description: Fills the empty array with 0's
** Parameters: This is using call by reference to modify contents of array
*********************************************************************/
void fill(char **&arr, int x, int y){
char c = '.'; // just a counter variable
for (int i = 0; i < y; i++){
for (int j = 0; j < x; j++){
arr[i][j] = c; // initializing array to be all 0's
//c++; // This would be to count up from 0
}
}
}
/*********************************************************************
** Function: print
** Description: Prints out the contents of the array
*********************************************************************/
void print(char **array, int x, int y){
for(int i = 0; i < y; i++){
for (int j = 0; j < x; j++){
cout << array[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
/*********************************************************************
** Function: remove
** Description: Removes the allocated memory
*********************************************************************/
void remove(char **&arr, int y){
for(int i = 0; i < y; i++)
delete(arr[i]); // deleting the little fingers of the array
delete(arr); // deleting the array
}
/*********************************************************************
** Function: main
** Description: No main, no game. (required for program to run)
*********************************************************************/
int main(){
char **array;
int x;
int y;
/* Taking in size of array */
cout << "Please enter an x value: ";
cin >> x;
cout << "Pleaes enter a y value: ";
cin >> y;
cout << endl;
array = init(x, y); // array is returned here, we now have allocated memory
fill(array, x, y); // putting all 0's in the array
print(array, x, y); // printing out the array
remove(array, y); //frees the memory that was allocated
return 0;
}
Explanation / Answer
#include<iostream>
#include<cstdlib>
using namespace std;
/*********************************************************************
** Function: init
** Description: Initializes the memory of the 2-d array
** Pre-Conditions: x & y > 0
*********************************************************************/
char **init(int x, int y){
char **temp;
temp = new char*[y]; // creating first chunk of memory
for(int i = 0; i < y; i++)
temp[i] = new char[x]; // creating the rest
return temp; //returning the new array
}
/*********************************************************************
** Function: Fill
** Description: Fills the empty array with 0's
** Parameters: This is using call by reference to modify contents of array
*********************************************************************/
void fill(char **&arr, int x, int y){
char c = '.'; // just a counter variable
for (int i = 0; i < y; i++){
for (int j = 0; j < x; j++){
arr[i][j] = c; // initializing array to be all 0's
//c++; // This would be to count up from 0
}
}
}
/*********************************************************************
** Function: print
** Description: Prints out the contents of the array
*********************************************************************/
void print(char **array, int x, int y){
for(int i = 0; i < y; i++){
for (int j = 0; j < x; j++){
cout << array[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
/*********************************************************************
** Function: remove
** Description: Removes the allocated memory
*********************************************************************/
void remove(char **&arr, int y){
for(int i = 0; i < y; i++)
delete(arr[i]); // deleting the little fingers of the array
delete(arr); // deleting the array
}
/*********************************************************************
** Function: UpdateValue
** Description: update with new value at give x and y
** Parameters: This is using call by reference to modify contents of array
*********************************************************************/
void updateValue(char **&arr, int x, int y, char key){
arr[x][y] = key; // updatind with new value
}
/*********************************************************************
** Function: main
** Description: No main, no game. (required for program to run)
*********************************************************************/
int main(){
char **array;
int x;
int y;
/* Taking in size of array */
cout << "Please enter an x value: ";
cin >> x;
cout << "Pleaes enter a y value: ";
cin >> y;
cout << endl;
array = init(x, y); // array is returned here, we now have allocated memory
fill(array, x, y); // putting all 0's in the array
print(array, x, y); // printing out the array
int i, j;
char key;
cout<<"Enter i and j value, at which you want to update : ";
cin>>i>>j;
cout<<"Enter new value: ";
cin>>key;
if(i >= x || j >=y)
cout<<"Given position is out of range ";
else
updateValue(array, i, j, key);
remove(array, y); //frees the memory that was allocated
return 0;
}