Array Data structure in c++ - Algomentor

Array Data structure in C++  

Array data structures in c++

Hi, welcome to the section data structures and algorithms made easy now we discuss Array data structure when we will deal with C++ program or maybe you are using any programming language like java, python or maybe any other languages also Array is very common in all of them because in every serious program we need to store some user data or maybe some program-generated data we need to store it in some location and it is very difficult if we are using all values in a variable because in programs there are sometimes there are millions of values to be stored and storing million of value each in the variable is definitely not an easy task. so we need something that can store all the values of us and we read, write, update or delete the value many times first their are millions of values and know all these works of reading, writing, update or delete those values which are not an easy task to do. So to solve the problem array comes in the picture. and boom array can solve all this problematic stuff for us 😎. We can store a whole lot of millions of values to store in the array and perform operation like reading, update, delete and a lot more in the array first we discuss some definition in the array then we write a program to all these operations of the array we create a method of all these and use this method when need. 

What is the Array?

An array is the data structure that occupies the contiguous space in the memory and has the ability to store the same data type

What is the operation we can perform in the Array?

we can do 5 operations in the Array we can perform 
  1. we can traverse the Array
  2. we can delete a particular index in the Array
  3. we can add a particular element at a particular index in the Array
  4. we can update a particular index in the Array
  5. we can read a particular index in the Array
How to define an Array?

There are few methods to define an array
  1. int arr[5] = {1,2,3,4,5}; // store 1,2,3,4,5 in array
  2. int arr[5] = {1}; // set all the element with value 1
  3. int arr[5]; // create an blank array with no value
Write A program to take a array value from the user and print value of the Array.

// Here is the code for the problem

#include<iostream>
using namespace std;
int main()
{
int n;
//take input from user
cin>>n;
int arr[n];
//array in c++ started from 0 index
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
// output of the array
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}

//Here is the output of the code

Array data structures in c++

Write A program to find the maximum element in the array?

// hear is the code

#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Number of element in the array ";
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int max = arr[0];
for(int i=0;i<n;i++)
{
if(max<arr[i])
{
max = arr[i];
}
}
cout<<"Maximum in the array is "<<max;
}

// hear is the output

array example in c++

Write A program to perform all Array operation read, update, delete, print.

Here is the code for the problem

#include<iostream>
using namespace std;
// define all array element to 0
int arr[10]={0};
void deletepos(int pos)
{
for(int i=pos+1;i<10;i++)
{
arr[i-1]=arr[i];
}
for(int i=0;i<10;i++)
{
cout<<arr[i]<<" ";
}
}
void insert(int val,int pos)
{
for(int i=9;i>pos-1;i--)
{
arr[i+1]=arr[i];
}
arr[pos]=val;
for(int i=0;i<10;i++)
{
cout<<arr[i]<<" ";
}
cout<<"";
}
void update(int val,int pos)
{
// just override the value
arr[pos-1] = val;
for(int i=0;i<10;i++)
{
cout<<arr[i]<<" ";;
}
}
void read(int pos)
{
// ready direct from index
cout<<arr[pos-1];
}
void print()
{
for(int i=0;i<10;i++)
{
cout<<arr[i]<<" ";;
}
}
int main()
{
insert(12,0);
cout<<endl;
insert(13,1);
cout<<endl;
deletepos(1);
cout<<endl;
insert(120,5);
cout<<endl;
read(5);
cout<<endl;
update(130,5);
cout<<endl;
read(5);
cout<<endl;
print();
}

// hear is the output of the code

Array data structures in c++

In this program, I made the function to solve each subtask we will learn about function in another article for know assume a function has the ability to solve particular subtask in the programming language we create all these functions and use the individual component in the main function.

I hope you like the article and series of data structures and algorithms made easy. If you like our article and want to read more article visit our home page for more article and please do follow at our social handle as we keep you regularly update in our social media and please do share this article with you coder friends and keep supporting us 😀.

Hi if you want to write a post on our website then please check this article on How to do a guest post.

Post a Comment

1 Comments