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.
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: May 6, 2024
Easy

Difference Between One-Dimensional And Two-Dimensional Array

Shivam Verma
0 upvote

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:

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

• 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;}``

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.

• 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;}``

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

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.

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 :

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!

Live masterclass