1

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];
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • `int* arr[3];` -- This is incorrect. Did you learn about `new[]`? Nowhere in your code have you used it. Right now, your code uses uninitialized pointers everywhere. – PaulMcKenzie Mar 30 '21 at 18:05

2 Answers2

2

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

1

You never allocate memory for the 1D int array, nor for the float 2D array. Your declarations are also wrong, for example float** arr_two[3][3] is an array of size 3 of arrays of size 3 of pointers to pointer to float, clearly not what you want.

Your code should look more like this:

int *arr = new int[3]; // pointer to int will hold 3 ints

float **arr_two = new float *[3]; // array of 3 pointers to float

for (int i = 0; i < 3; i++)
{
    arr_two[i] = new float[3]; // each pointer will hold 3 floats
}

for (int i = 0; i < 3; i++)
    arr[i] = rand() % 100; // indexing is the same as if it was an array[size]

for (int j = 0; j < 3; j++) // indexing is the same as if it was an array[size][size]
    for (int k = 0; k < 3; k++)
        arr_two[j][k] = rand() % 100;

print_1d_array(arr); // you pass the name of the pointers, no dereference needed
print_2d_array(arr_two);

You should also print some spaces and newlines in your print functions, otherwise this will look like a very large single value.

Also, don't forget to free the memory later, when you no longer need the data:

delete[] arr;

for (int i = 0; i < 3; i++)
{
    delete[] arr_two[i];
}

delete[] arr_two;

I should also mention that in modern C++ very rarely we see the usage of raw pointers (except maybe in SO questions). Alternatives are:

Also note that your random values are all int, so you won't see float values in the float arrays. If you want randomly generate fractional values, you need a new approach, take a look here: Random float number generation

anastaciu
  • 23,467
  • 7
  • 28
  • 53