'C++ Dynamic Arrays Creation

I am prompted with the question as follows

  1. Write a program that prints dynamic arrays.
  2. The program creates an int 1D dynamic array of 3 elements and a float 2D dynamic array of 3 ROWS and 3 COLS.
  3. Initialize both arrays with random values.
  4. Both arrays will be printed separately in two separate functions
  5. void print_2d_array(float**);
  6. void print_1d_array(int*);"

I have created a code that will not produce any output. I am guessing the issue is in the initialization of the arrays, but I cannot figure it out. How do I get it to display the randomly generated numbers?

#include <iostream>
#include <iomanip>

using namespace std;

void print_2d_array(float**);
void print_1d_array(int*);

int main() {
    srand(time(NULL));

    int* arr[3];
    float** arr_two[3][3];

    for (int i = 0; i < 3; i++)
        *arr[i] = rand() % 100;
    
    for (int j = 0; j < 3; j++)
        for (int k = 0; k < 3; k++)
            **arr_two[j][k] = rand() % 100;

    print_1d_array(*arr);
    print_2d_array(**arr_two);
}

void print_2d_array(float** arr_two) {
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++)
            cout << arr_two[i][j];
    }
    cout << endl;
}

void print_1d_array(int* arr) {
    for (int i = 0; i < 3; i++)
        cout << arr[i];
}


Solution 1:[1]

I want to post my code because this was a nightmare for me with a lot of testing

This would help with a 1Dimensional array for dynamic array assignment

I can't find all the stack overflow comments that helped me reach this conclusion of code so, this is how I give back. Hopefully this helps make sense of it.

I read a lot of stack questions saying this isn't possible and use a vector, if I find them I intend to post this there as well.

#include <iostream>
#include <array>
using namespace std;

// Reference // https://www.learncpp.com/cpp-tutorial/dynamically-allocating-arrays/ // https://www.youtube.com/watch?v=FHhcSncuHEI

int main()
{

 int size;
 int number = 0;
 int count  = 0;
 cout << "enter size for array: ";
 cin  >> size;
 int *array;
 int *tempArray;
 tempArray = new int[size];
 array     = new int[size];

 // can also be declared
 // int *array = new int[size];

 while(number != -1)
 {

  cout << " enter values for array: "; 
  cin  >> number;

  if(number == -1)
  {
      break;
  }

  if(count == size)
  {
      delete [] tempArray;
      size = size + 1;
      tempArray = new int[size];
    
     for(int i=0; i < count; i++)
     {
         tempArray[i] = array[i];
     }
         delete [] array;
         array = new int[size];                 // re-use array pointer
         
     for(int i=0; i < count; i++)
     {
         array[i] = tempArray[i];
     }     
  }
 
 array[count] = number;
 count++;
  
}

//   if(number == -1)
//     {
//     for(int i=0; i < count; i++)
//       {
//     cout << &array[i];
//       }
//     }

cout << endl;

// proves the array is tracking in memory to the same places.
// if it goes out of bounds of memory, the cout will just
// print the memory address of the last memory address block
// of the dynamic array, the assignment out of bounds does not
// cause significant change.

// array[0] = 1;
// array[1] = 2;
// array[2] = 3;
// array[3] = 4;

// cout << &array[0];
// cout << &array[1];
// cout << &array[2];
// cout << &array[3];

// cout << sizeof(array)/sizeof(array[0]);

// cout << endl;

// cout << array[0];
// cout << array[1];
// cout << array[2];
// cout << array[3];
// cout << array[4];
// cout << array[5];

// cout << endl;

for(int i=0; i < size; i++)
{
cout <<  "array element " << i << ": " << array[i] << endl;
}

return 0;

}

https://onlinegdb.com/YKF9skF2Ql

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Robert Petersen