C++ 2d array

A One-dimensional array stores elements in the linear fashion.
In contrast to 1d array (one-dimensional array), 2d array stores elements in a table which has rows and columns.

#include <iostream>
#include <conio.h>
using namespace std;

int main(){
   //declare 2d array and initialize array
   //when initializing 2d array, you can omit its first dimension.
   int arr_2d[][5]={ //5 rows and 5 columns
     {0,0,0,0,0},{0,0,0,0,0},
      {0,0,0,0,0},{0,0,0,0,0},
      {0,0,0,0,0}
     };
   //2d array assignment using for loops
   int size=sizeof(arr_2d)/sizeof(int)/5;
   cout<<size;
   for(int i=0;i<size;i++){
        for(int j=0;j<size;j++)
             arr_2d[i][j]=1; //assign 1 to all array elements
             }
  //output the 2d array elements as a table
   for(int i=0;i<size;i++){
        for(int j=0;j<size;j++)
             cout<<arr_2d[i][j]<<"\t";
        cout<<"\n";
        }
  
  
   getch();
   return 0;
}

Posted by: Dara | post date: 01-26-2013 | Subject: C++ Programming Language




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.