C/C++ 中的多維陣列的初始化


在多維陣列中,陣列應具有超過 1 的維度。下圖顯示了具有維度 3 x 3 x 3 的多維陣列的記憶體分配策略。

這是一個 C++ 程式,用於初始化多維陣列。

演算法

Begin
   Initialize the elements of a multidimensional array.
   Print the size of the array.
   Display the content of the array.
End

示例

#include<iostream>
using namespace std;
int main()
{
   int r, c;
   int a[][2] = {{3,1},{7,6}};
   cout<< "Size of the Array:"<<sizeof(a)<<"\n";
   cout<< "Content of the Array:"<<sizeof(a)<<"\n";
   for(r=0; r<2; r++) {
      for(c=0; c<2; c++) {
         cout << " " << a[r][c];
      }
      cout << "\n";
   }
   return 0;
}

輸出

Size of the Array:16
Content of the Array:16
3 1
7 6

更新於: 30-Jul-2019

153 次瀏覽

開啟你的 事業

完成課程以獲得認證

開始學習
廣告