Create a 2D dynamic array to store the sum of different possibilities while roll
ID: 3566131 • Letter: C
Question
Create a 2D dynamic array to store the sum of different possibilities while rolling 2 dices. There are 36 possibilities of (a, b) that can show up while rolling two dices. Each cell in your 2D array should contain the sum (a + b). Different possibilities are shown to you in the figure below:
(1,1) (1,2) (1,3) (1,4) (1,5) (1,6)
(2,1) (2,2) (2,3) (2,4) (2,5) (2,6)
(3,1) (3,2) (3,3) (3,4) (3,5) (3,6)
4,1) (4,2) (4,3) (4,4) (4,5) (4,6)
(5,1) (5,2) (5,3) (5,4) (5,5) (5,6)
(6,1) (6,2) (6,3) (6,4) (6,5) (6,6)
Explanation / Answer
#include<iostream>
#define n 6
using namespace std;
int main()
{
int** a = new int*[n];
for(int i =0; i<n; i++)
a[i] = new int[n];
for(int i = 0; i<n; i++)
{
for(int j = 0; j<n; j++)
a[i][j] = i+j+2;
}
for(int i = 0; i<n;i++)
{
for(int j = 0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
-----------------------------------------------------------------
OUTPUT: