Skip to main content

2D Array in Cpp (c++)

2D-Array:

2D array is similar to a matrix of mxn (row into column). where every element has a specific index which is usually denoted by i and j. i represent the number of rows and j represent the number of columns.


 

Declaration of 2D Array in C++

int A[5][4];                                               // 5 rows and 4 column

Initialization Of Array:

An array initializer for a 2D array contains the rows of A, separated by commas and enclosed in braces. Each row, in turn, is a list of values separated by commas and enclosed in braces.
int A[5][4] = { 
                     {  1,  2, 3, 4 },
                     { -44, 65, 4, 23},
                     { -45, -1, 3, 34}
                     { -45, -1, 3, 34}
                     { -45, -1, 3, 34}
                  };

There are three-dimensional, four-dimensional, and even higher-dimensional arrays, but they are not used very often in used.

2D array as a pointer:

int **ptr = new int * [row];
 for(x=0;x<row;x++)
 *(ptr+x) = new int [col];

Implementation of 2D Array in C++:

#include<iostream>

using namespace std;

int main()
{

     int row,col,x,y;

    cout<<"Enter number of rows: ";
 cin>>row;

    cout<<"Enter number of columns: ";
 cin>>col;

     int **ptr = new int * [row];


      for(x=0;x<row;x++)

      *(ptr+x) = new int [col]; //Storing elements

       for (x=0;x<row;x++)
       for(y=0;y<col;y++)
       {
             cout<<"Enter number: ";
             cin>>ptr[x][y];
        }

        //Displaying pointer to pointer to ptr

        cout<<"You have entered:"<<endl;

 for (x=0;x<row;x++)
 {
  for(y=0;y<col;y++)
  {
   cout<<ptr[x][y]<<" ";
  }
  cout<<endl;
 }

 return 0;
}

Comments

Popular posts from this blog

Data Types

DATA TYPES in C/C++: Since C and C++ are strongly type languages, therefore every thing must be defined. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Data Types Description C-Keyword Format Specifier integer Integers are whole numbers that can have both positive and negative values but no decimal values.   int   %d or %i Float Floating type variables can hold real numbers precision of 6 digits. float %f Double can hold real numbers with the precision of 14 digits. double %f Character data type allows a variable to store only one character. char %c Implementation in C: #include<stdio.h> int main() { printf(“%d n”,18); //this will print integer printf(“%fn”,32.56); //this will print decimal number printf(“%d %d %dn”,32,56,89); // %d is a place holder for integer and it provide place for one integer printf(“%ld...

Tic-Tac-Toe Implementation in Cpp (C++) Step by Step

Conditional Statement

Conditional Statement: A conditional statement is an if-then statement which means that if some statement is true then it follows the if-block of code and if the condition is false then it follows the else-block of code. In the programming world, there are two types of conditional statement namely if-else and switch. if-else Statement: Syntax: if (condition 1) { } else if (condition 2) { } else { }   Implementation of If-else statement in C: Q: Write a program to find the greatest of three numbers. #include<stdio.h> int main() { int a,b,c; printf(“Please Enter First Number “); scanf(“%d”,&a); printf(“Please Enter Second Number “); scanf(“%d”,&b); printf(“Please Enter Third Number “); scanf(“%d”,&c); if (a>b && a>c) { printf(“The Greatest Number is %d”,a); } else if (b>c) { printf(“The Greatest Number is %d”,b); } else { pr...