'Coding has over 700 languages', '67% of programming jobs aren’t in the technology industry', 'Coding is behind almost everything that is powered by electricity'
Last Updated: Jan 1, 2024
Easy

Difference Between One-Dimensional And Two-Dimensional Array

Author Shivam Verma
0 upvote
gp-icon
Basics of C++
Free guided path
9 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction 

An array is a data structure that sequentially stores the same data type elements. The array elements are always stored in consecutive (specific) memory locations. The lowest address represents the first element of the array and the highest address to the last element of the array. There are three types of array:

  1. One-dimensional array (1D)
  2. Two-dimensional array (2D)
  3. Multi-dimensional array

One-Dimensional Array (1D)

  • The one-dimensional array consists of a list of variables that have the very same data type.
  • The size of the one-dimensional array is fixed.
  • In C++, One-dimensional Array is declared as:
    syntax:
    type variable_name[size];
  • For a dynamically sized array, we can use a vector in C++.

Example of a One-Dimensional Array

Code

// Program of One Dimensional Array in C++
#include <iostream>
using namespace std;
int main()
{
   // declaring and Initialising array in C++
   int arr[5]={15,12,23,212,55};
   for (int i=0;i<5;i++)
   {
      // Accessing each variable using for loop
      cout<<"Index: "<<i<<" and Value: "<<arr[i]<<endl;
   }
    return 0;
}

Output

Index: 0 and Value: 15
Index: 1 and Value: 12
Index: 2 and Value: 23
Index: 3 and Value: 212
Index: 4 and Value: 55

 

You practice by yourself with the help of online c++ compiler.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Two-Dimensional Array (2D)

  • A two-dimensional array consists of a list of arrays with similar data types.
  • Two-dimensional arrays are also referred to as Multi-dimensional Arrays.
  • In C++, a two-dimensional Array is declared as:
    syntax:
    type variable_name[size1][size2];
  • All the elements of a 2D array can be accessed randomly as well as with the help of their index.
  • A two-dimensional array is also known as a matrix.

Example of a Two-Dimensional Array

Code

// Program of Two Dimensional Array in C++
#include <iostream>
using namespace std;
int main()
{
   // an array with three row and three column
   int arr[3][3]={{15,12,23},{45,21,55},{63,98,56}};
   for (int i=0;i<3;i++)
   {
       for(int j=0;j<3;j++)
       {
          cout<<"Value at arr["<<i<<"]["<<j<<"]: ";
          cout<<arr[i][j]<<endl;
       }
   }
    return 0;
}

Output

Value at arr[0][0]: 15
Value at arr[0][1]: 12
Value at arr[0][2]: 23
Value at arr[1][0]: 45
Value at arr[1][1]: 21
Value at arr[1][2]: 55
Value at arr[2][0]: 63
Value at arr[2][1]: 98
Value at arr[2][2]: 56


Also see, Literals in C.Fibonacci Series in C++

Difference Between One-Dimensional and Two-Dimensional Array

Parameters 

One-dimensional array(1D)

Two-dimensional array(2D)

Definition  A simple data structure that sequentially stores elements of the same data type.  A two-dimensional array stores a list of arrays with similar data types.
Declaration 

Syntax: 

type variable_name[size];

Here type refers to the data type, and size refers to the number of elements that the array can hold.

Syntax: 

type variable_name[size1][size2];

Here type refers to datatype size1 refers to the number of rows, and size2 to the number of columns of the array.

Dimensions  A one-dimensional array has only one dimension. A two-dimensional array has a total of two dimensions.
Size(bytes) 

The size of the 1D array is:

Total number of Bytes =sizeof(datatype of array variable)* size of the array.

The size of the 2D array is:

Total number of Bytes = size of(datatype of the variable of the array)* the size of the first index * the size of the second index.

Representation  The 1D array represents multiple data items in the form of a list. The 2D array represents multiple data items in the form of a table consisting of rows and columns.
Address calculation 

Address of element arr[i] = b+w*i

Here b is the base address, w is the size of each element, and i is the index of the array.

Address of arr[i[[j] can be calculated in two ways:

Row Major:

Address of element arr[i][j]=b+(n(i-l1)+(j-l2))

Column Major:

Address of element arr[i][j]=b+(m(j-l2)+(i-l1)) 

Here b is the base address, w is the size of each element, n is the number of rows, and m is the number of the column. l1 specifies the lower bound of the row, and l2 identifies the lower bound of the column.

Row column matrix   A one-dimensional array has no row-column matrix. A two-dimensional array has a row-column matrix.

 

 

 

 

 

Must Read Lower Bound in C++

Frequently Asked Questions

How many loops are required to traverse a two-dimensional array?

Two loops are required to traverse a two-dimensional array.

What is the syntax of a two-dimensional array?

The syntax of a two-dimensional array is
type variable_name[size1][size2];
Here type refers to datatype size1 refers to the number of rows, and size2 to the number of columns of the array.

What is the difference between one-dimensional arrays and two-dimensional arrays?

In a one-dimensional array, you can save only a list of values. But in a two-dimensional array, you can store a matrix of values.

Can we change the size of an array at run time?

No, we cannot change the size of an array at run time.

Conclusion

In this blog, we got introduced to both one-dimensional and two-dimensional arrays. We also discussed the difference between one-dimensional and two-dimensional arrays.

Recommended Articles


Recommended Problems : 


Do check out some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Happy Learning!

Previous article
Multi-Dimensional Arrays
Next article
String Functions
Guided path
Free
gridgp-icon
Basics of C++
9 chapters
104+ Problems
gp-badge
Earn badges and level up
Live masterclass