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

Whats the right way to create 9x9, 3 digit random array with pointer in c++? Exa

ID: 3557125 • Letter: W

Question

Whats the right way to create 9x9, 3 digit random array with pointer in c++?

Example in line 65: int*arr[9], should this be something like int *(*(arr+9) +9)

Whats another way to create a 9x9, 3 digit array with this program, using the right declaration for pointers?



In line 65 to 91:

{
int *arr[9];


for(int i = 0; i < 9; i++)
arr[i] = new int[9];

srand(time(0));

for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
arr[i][j] = 100 + rand() % 900;
}
}

cout << " Array of integers: " << endl;
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
}

Full Program: http://ideone.com/2Qlamm

Explanation / Answer

int arr[9][9];
//declaring one pointer
int (*p)[9][9]=&arr;
//initializing to base of the array char (*p)[5][7][6] = &arr;
//p=arr;
//logic to create a 9x9 3 digit array using the right declaration for pointers
for(i=0;i<9;i++)
{

   for(j=0;j<9;j++)
    {
       (p+(i*2)+j)= 100 + rand() % 900;
   }
}
cout << " Array of integers: " << endl;
for(int i = 0; i < 9; i++)
{
   for(int j = 0; j < 9; j++)
   {
       cout << arr[i][j] << " ";
   }
   cout << endl;
   }
   cout << endl << endl;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote