C++ 动态数组创建

C++ Dynamic Arrays Creation

我的问题如下

  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*);"

我创建了一个不会产生任何输出的代码。我猜问题出在数组的初始化中,但我无法弄清楚。如何让它显示随机生成的数字?

#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];
}

您永远不会为一维 int 数组或 float 二维数组分配内存。您的声明也是错误的,例如 float** arr_two[3][3] 是一个大小为 3 的数组,其中包含指向 float 的指针的大小为 3 的数组,显然不是您想要的。

您的代码应该更像这样:

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);

您还应该在打印函数中打印一些空格和换行符,否则这看起来像一个非常大的单个值。

此外,当您不再需要数据时,不要忘记稍后释放内存:

delete[] arr;

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

delete[] arr_two;

我还应该提到,在现代 C++ 中,我们很少看到原始指针的使用(除了可能在 SO 问题中)。备选方案是:

  • 智能指针,当你理解了指针和数组后,看看这里是一个很好的起点:What is a smart pointer and when should I use one?.
  • 或者最好使用 STL containers, in this case, std::vector 是正确的工具。

另请注意,您的随机值都是 int,因此您不会在 float 数组中看到 float 值。如果你想随机生成分数值,你需要一个新的方法,看看这里:Random float number generation

我想 post 我的代码,因为这对我来说是一场噩梦,需要大量测试

这将有助于动态数组分配的一维数组

我找不到所有帮助我得出此代码结论的堆栈溢出注释,所以这就是我回馈的方式。希望这有助于理解它。

我读了很多堆栈问题,说这是不可能的,并且使用向量,如果我找到它们,我也打算 post 这个。

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

//参考 // 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