Tuesday, December 8, 2020

Arrays for beginners for free.

Array.


Array is basically a contiguous memory allocation of same data type.It can't be of different data type.

It is a homogeneous continuous memory allocation.


We will learn about:

  1. What is an Array
  2. Declaring and initializing
  3. Accessing array element

1.What is an array:


Array is basically a contiguous memory allocation of same data type.It can't be of different data type.

It is a homogeneous continuous memory allocation.


2.Declaring and initializing:


int A[5];

The above statement is used for declaring an array.

int A[5] = {1,2,3,4,5};

The above statement is used for initialing an array.


Program to illustrate difference between declaring and initializing:


#include<iostream>
using namespace std;
int main()
{
int A[5];
int B[5] = {1,2,3,4,5};
for(int i=0;i<5;i++)
{
cout<<"Enter element for array A";
cin>>A[i];
}
for(int i=0;i<5;i++)
{
cout<<A[i]<<" ";
}
cout<<endl;
for(int i=0;i<5;i++)
{
cout<<B[i]<<" ";
}
return 0;
}

3.Accessing array element:

We can access array element with their position index.

Program that will explain how to access array element:


#include<iostream>
using namespace std;
int main()
{
int A[5];
for(int i=0;i<5;i++)
{
cout<<"Enter element for array A";
cin>>A[i];
}
for(int i=0;i<5;i++)
{
cout<<A[i]<<" ";
}
return 0;
}


Programs on array:




Share this

0 Comment to "Arrays for beginners for free."

Post a Comment

If you have any doubts then let me know.