Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++, how can I update a value in a dynamic two dimensional array? See below f

ID: 3685021 • 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.

/*********************************************************************

** Program Filename: DynMem.cpp

** Author: Cierra Shawe

** Date: 7/7/15

** Description: Demo of dynamic memory allocation for cs162 - Su15

** Input:

** Output:

*********************************************************************/

#include <iostream>

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

/*********************************************************************
** Program Filename: DynMem.cpp
** Description: Demo of dynamic memory allocation for
** Input:
** Output:
*********************************************************************/

#include <iostream>
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: updatevalue
** Description: Update the elements of the array
*********************************************************************/
void updatevalue(char **array, int row, int column)
{
cout << "Value at row " << row << " and column " << column << " is " << array[row][column] << endl;
char value;
cout << "Enter the updated value: ";
cin >> value;
array[row][column] = value;
cout << "Array Updated ";
}

/*********************************************************************
** 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

while(true)
{
int row, column;
cout << "Enter the row you want to update, -1 to break: ";
cin >> row;
cout << "Enter the row you want to update, -1 to break: ";
cin >> column;
if(row == -1 || column == -1) break;
updatevalue(array,row,column);
}

print(array, x, y); // printing out the array
remove(array, y); //frees the memory that was allocated
return 0;
}