Introduction to Data Structures and Arrays

535

What is Data Structures?

The word ‘Data Structures’ itself is a combination of two different words namely ‘Data’ and ‘Structures’.

Data – Any collection of information is called Data. Example – Student Name, Student Roll no, etc.

Structures – It is defined as a way to store and represent data. Examples – Graph, Arrays, List, etc.

Combinedly, Data Structure is a way to organize data such that it can be used effectively.

Types of Data Structures –

Data Structure is broadly classified into two different types which are Primitive and Non-Primitive.

Primitive – The basic types of data that can be used to store a single data are called primitive data types. Example – integer(used to store a integer type value), character(used to store a non-integer value) etc.

Non-Primitive – The data structure that are derived from these primitive data structures are called non-primitive data structures. Example – Array(used to store a list of data).

To implement and use data structure, we need a language. So, we will learn Data Structures through C++.

Now, we will learn about our first and most useful Data Structure which is Array.

What is Array?

An array is a Data Structure that stores multiple elements at a contiguous memory location. Here, contiguous is different from consecutive because contiguous means a common difference of addressed or one after another but consecutive refers to the immediate next memory location. Also, the elements stored should be homogenous which means, all the elements should be of similar type whether it be integer, character, or any.

Some of the properties of Arrays include –

  • Finite – An array always consists of a finite set of elements.
  • Contiguous memory locations – The elements will be stored at memory locations which will be ordered one after the another.
  • Random Access – We can access any of the elements randomly. For example, if we need to access the fourth element then it is not needed to pass through the first three elements.
  • Homogenous – All the elements should be of the same data type.

Given below is the syntax to define an array –

Data_type Name_of_Array[Size];

Now, we will define an Array and understand this concept properly with the help of an example.

#include<iostream>

using namespace std;
int main(){
int arr[10];
for(int i=0;i<10;i++){
arr[i] = i+1;
}
for(int i=0;i<10;i++){
cout<<arr[i];
}
}

Output –

1 2 3 4 5 6 7 8 9 10

Explanation –

‘int arr[10]’ will declare an array of size 10. That means, 10 contiguous memory locations are reserved inside the memory to store 10 integers.

And after that, we used a ‘for loop’ to iterate over the array and initialized each block of data with a value just greater than the index.

And then, we iterated over the array to print the values stored by it.

Introduction to Data Structures and Arrays
Data structures array

Now, we will understand two different types of arrays –

  • One Dimensional Array
  • Two Dimensional Array

One-Dimensional Array –

In this, the array stores the elements at contiguous memory locations having an index number through which we can access any element.

For example –

int arr[5];

 Two-Dimensional Array –

In this, the array stores the elements in the form of a matrix. Any element is accessed with the help of two indices.

Data structures multi dimensional array
Data structures multi dimensional array

For example –

int arr[2][3];

Similarly, we can create a multi-dimensional array. But basically, the most basic representation is of 1-d array and multi-dimensional arrays are derived from it.

For example, arr[2][3] derives 2 one-dimensional arrays and inside that 3 one-dimensional arrays. But for users, understanding we will understand this as matrices which will be easier in terms of understandability and accessibility.

So, in this article, we learned about the concept of arrays, which is one of the basic and widely used data structures. It is quite easier to use and searching is quite easier if we know the index of the element.

Previous articleHow to create a Thread in Java
Next articleOperations on Arrays – Data Structures Traversal, Insertion, Deletion, Searching and Sorting
Harshit Brijwasi
I am Harshit Brijwasi. I am an engineer and a Technical Content writer. I am here to share my knowledge about technical topics and help researchers with them.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.