Table of contents
1.
Introduction 
2.
One-Dimensional Array (1D)
2.1.
Example of a One-Dimensional Array
2.1.1.
Implementation in C++
2.2.
C++
3.
Two-Dimensional Array (2D)
3.1.
Example of a Two-Dimensional Array
3.1.1.
Implementation in C++
3.2.
C++
4.
Difference Between One-Dimensional and Two-Dimensional Array
5.
 
6.
 
7.
 
8.
 
9.
Properties of 1D and 2D Arrays
10.
Applications of Arrays:
11.
Frequently Asked Questions
11.1.
What is the difference between 1-dimensional and 2 dimensional drawing?
11.2.
Is 1D array better than 2D array?
11.3.
How many loops are required to traverse a two-dimensional array?
11.4.
What is the syntax of a 2D two-dimensional array?
11.5.
What is the difference between one-dimensional arrays and two-dimensional arrays?
12.
Conclusion
Last Updated: Dec 23, 2024
Easy

Difference Between One-Dimensional And Two-Dimensional Array in C++

Author Shivam Verma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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. 

Difference Between One-Dimensional And Two-Dimensional Array

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

Implementation in C++

  • C++

C++

// 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;
}
You can also try this code with Online C++ Compiler
Run Code

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.

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

Implementation in C++

  • C++

C++

// 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;
}
You can also try this code with Online C++ Compiler
Run Code

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

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++

Properties of 1D and 2D Arrays

The properties of 1D Arrays are:

  • Linear Structure: 1D arrays are arranged in a single line, meaning they have a single dimension.
  • Elements: 1D arrays consist of a collection of elements of the same data type arranged sequentially.
  • Indexing: Each element in a 1D array is accessed using a single index. The index starts from 0 and goes up to the size of the array minus one.
  • Memory Allocation: In memory, elements of a 1D array are stored sequentially, occupying contiguous memory locations.
  • Traversal: Traversal through a 1D array involves iterating through the elements in a linear fashion, usually using loops.


The properties of 2D Arrays are:

  • Matrix Structure: 2D arrays are structured in rows and columns, forming a grid-like arrangement.
  • Elements: Each element in a 2D array is identified by two indices, one for the row and one for the column.
  • Memory Allocation: In memory, elements of a 2D array are stored row by row or column by column, depending on the programming language and memory layout.
  • Indexing: Accessing elements in a 2D array requires specifying both row and column indices.
  • Traversal: Traversal through a 2D array typically involves nested loops, where one loop iterates over the rows and another loop iterates over the columns.

Applications of Arrays:

Arrays are used extensively across various applications due to their efficient access and manipulation of elements. Here are some key applications:

  • Data Storage: Arrays are used to store data in a structured manner, allowing quick access via indices. This is useful in scenarios where rapid access to elements is crucial, such as in database indexing and management systems.
  • Implementation of Other Data Structures: Many other data structures like stacks, queues, heaps, and hash tables are implemented using arrays due to their efficient random access capabilities.
    Algorithmic Support: Arrays support a wide range of algorithms for tasks such as sorting (e.g., quicksort, mergesort) and searching (e.g., binary search). This makes them indispensable in software development.
  • Memory Management: The simplicity of arrays makes them ideal for memory allocation and management within operating systems and applications. They help manage resources predictably and efficiently.
  • Lookup Tables and Reverse Lookup Tables: Arrays are used to create lookup tables which allow for fast data retrieval. Similarly, reverse lookup tables can be implemented using arrays to find data associated with indices.
  • Matrix Operations: Arrays are extensively used in mathematical and scientific computation, particularly for representing matrices in linear algebra, which are crucial in various fields like physics and engineering.
  • Handling Multiple Variables of Similar Type: Arrays provide a compact way of handling multiple variables of a similar type. For instance, storing the marks of students, pixel data in images, or configurations settings in software.
  • Game Development: Arrays find their use in game development for storing game states, grid-based games like chess, managing sprites, and tracking player movements.
  • IoT Devices: In IoT applications, arrays are used to manage states and configurations of multiple devices efficiently, as well as to handle sensor data over time.
  • Buffer for I/O Operations: In software development, arrays are used as buffers to temporarily hold data during input/output operations, enhancing performance and reliability of applications.

Frequently Asked Questions

What is the difference between 1-dimensional and 2 dimensional drawing?

1D drawing represents a single line, while 2D drawing depicts a plane with length and width, forming shapes.

Is 1D array better than 2D array?

It depends on the specific use case; 1D arrays are simpler for linear data, while 2D arrays are suitable for grid-like data structures.

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 2D 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.

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 : 

 

Live masterclass