Question

How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using...

How to initialize a dynamic two-dimensional array with values? (C++)

Here's my problem...

# include
using namespace std;

int main()
{


   // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic

   // tic tac toe board is an array of int pointers
   // each int pointer in the board points to a row

   // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int)

   const int SIZE = 3;

   int** p_p_tictactoe = new int* [SIZE];


   // ... // [5.1] row1: dynamically allocate int[SIZE], use initializer list to initialize to {1,0,0}


   // ... // [5.2] row2: dynamically allocate int[SIZE], use initializer list to initialize to {0,1,0}


   // ... // [5.3] row3: dynamically allocate int[SIZE], use initializer list to initialize to {0,0,1}

   cout << endl << endl;
   system("pause");
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# include<iostream>

using namespace std;

int main()

{

   // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic

   // tic tac toe board is an array of int pointers

   // each int pointer in the board points to a row

   // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int)

   const int SIZE = 3;

   int** p_p_tictactoe = new int* [SIZE];//here rows are initialized

  

   // ... // [5.1] row1: dynamically allocate int[SIZE], use initializer list to initialize to {1,0,0}

p_p_tictactoe[0]=new int[SIZE]{1,0,0};//here first row created and initialized with values

   // ... // [5.2] row2: dynamically allocate int[SIZE], use initializer list to initialize to {0,1,0}

p_p_tictactoe[1]=new int[SIZE]{0,1,0};//here second row created and initialized with values

   // ... // [5.3] row3: dynamically allocate int[SIZE], use initializer list to initialize to {0,0,1}

p_p_tictactoe[2]=new int[SIZE]{0,0,1};//here third row created and initialized with values

   cout << endl << endl;

   system("pause");

   return 0;

}

what i lines bolded was entered by me

i completed the remaining rows and initialized the rows.

if you have any doubt please do comment

Add a comment
Know the answer?
Add Answer to:
How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using the code above my instructor just want me to delete name from a list of...

    using the code above my instructor just want me to delete name from a list of students name. the function needs to be called delete_name. It's in c++. please no changing the code above just adding. this is pointer and dynamic array. // This is chapter 9 Pointers and Dynamic array #include< iostream> #include<string> using namespace std; //Gv //function declaration string* add_name(string*,int&); void display_names (string*,int); //main int main() //local var int size-3; string *students_names-new string[size]; //code cout<<"Enter "<<size< students names:"<<endl;...

  • C++ problem with dynamic arrays is that once the array is created using the new operator...

    C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement...

    C++ problem to use dynamic memory allocation (of arrays) and pointer manipulation in order to implement the Inner and Outer classes (Circular Buffer of circular buffers using Queues). No need of any classes from the Standard Template Library (STL), not even vector. Add the member functions of those classes by following the codes of InnerCB.h and CBofCB.h below: // file: InnerCB.h // Header file for Inner Circular Buffer. // See project description for details. // #ifndef _INNERCB_H_ #define _INNERCB_H_ class...

  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

  • How to solve this Problem in C++ . The Problem Write program that uses a class...

    How to solve this Problem in C++ . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the set...

  • Compute for Miles Per Gallon in C++

    Make a program that will calculate and compute for the quotient of miles and gallons (mpg: miles per gallons). Your program should must ask the user to specify the size of the array (using dynamic array) for the following variable: miles ,gallons and mpg. Prompt the user to Initialize  the value of miles (value for miles should be 100-250) and gallons (values should be from 5-25). Use pointer galPtr for gallons, milPtr for miles and mpgPtr for mpg.  Use function...

  • Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas...

    Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas marked with TODO in the comments: Homework 8 Template.c Note: If you turn the template back into me without adding any original work you will receive a 0. By itself the template does nothing. You need to fill in the code to dynamically allocate an array of strings that are returned to the user. Remember: A string is an array. A tokenizer goes through...

  • 1. Your project will include the following three files: A header file: dynamicArray.h that includes a...

    1. Your project will include the following three files: A header file: dynamicArray.h that includes a list of function prototypes as enumerated in the next section. An implementation file: dynamicArray.cpp that implements the functions declared in the header file. A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've implemented above. 2. The header file dynamicArray.h will include the following list of functions: constructing a dynamic array of the specified size...

  • what would be the solution code to this problem in c++? The Problem Write program that...

    what would be the solution code to this problem in c++? The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of items 2. Get the number of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT