Pointers and Array

FOCUSING ON ARRAY AND POINTERS 

Abstract overview of this blog:Pointers and Array:

Here we will be discuss  Arrays first rather than starting up with pointers to array .I will give you a brief introduction of Array its declaration ,definition and few programs of Array.Once you are familiar with Array,we will proceed further to Array using pointers .

 

ARRAY AND DECLARATION OF ARRAY

ARRAY: Array is a collection of similar data or identical datatypes.It means the data elements you are using should be of similar data types.These objects or elements which you used  are  stored in consecutive memory locations.

for ex:List of students present in a class.

List of students of a class.

These all are of same type.

DECLARATION OF ARRAY

Declaration of array needs 3 things.

Data type : Data type means specifying which type of data you wants to store.for ex: int,char,float.

Name : Name of the array,to identify that array.

Size: Size represents the maximum values that array can hold.

Syntax of Array Declaration

type name[size];

ex: int marks[9];

char name[15];

Memory representation of array of 10 elements


int marks[9];   //array of marks name with 9 size.

Here we took array of 9 elements which will be stored at contiguous( consecutive) memory locations.

marks[9] will start from marks[0] to marks[8].as shown in this diagram.


marks[0] marks[1]    marks[2]   marks[3]  marks[4]  marks[5]  marks[6]  marks[7]   marks[8]  

marks[0] marks[1]    marks[2]   marks[3]  marks[4]  marks[5]  marks[6]  marks[7]   marks[8]  


STORING  VALUES IN ARRAY

Storing of value in array can be done in three ways.

 

int marks[3]={10,20,30};

 

int marks[3];

for(i=0;i<3;i++);

cin>>marks[i];

int i, arr1[3],arr2[3];

for(i=0;i<3;i++);

arr2[i]=arr1[i]; //copying each element of 1st arr to 2nd .

 

ACCESSING ELEMENT OF ARRAY

If  i take array of 10 elements arr[8], now to access 4th element of array i must write arr[3],in case you need to access all elements of Array,you need to use a loop ,every  time  you use array.

for ex: to initialize each element of array  i am using short code:


int i, marks[10];

for (i=0;i<10;1++)

marks[i]=5;


here all the elements of array are initialized to 5;

 

 

[0]            [1]            [2]              [3]              [4]               [5]                [6]               [7]                [8]            [9]

If you are just using  array name it means you are just pointing to first element of array .In order to access all the elements of array you need to use loop.

 

A simple program of array to insert and display  elements of array.

#include<iostream>
using namespace std;

main()
{
int arr[10],i;
cout<<"enter elements of array"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
for(i=0;i<10;i++)
{
cout<<"the Elements of array are "<<arr[i]<<endl;
}
return 0;
}

Same program of array of  inserting and displaying elements of array is done using pointers.

To know the basics of pointers first, please click on this link of pointers.

POINTER TO ARRAY

Look at this short code:

  • Here we declared an array of size 30 and a pointer which  points to the address of 1st element of array.
  • Statement ptr=arr means we are assigning a pointer which will points to the address of first element of array.
  • By displaying (*ptr) we can get the value of array of the element to which pointer is assigned.
  • By incrementing pointer (ptr++),pointer will carry the address of next elements of array.(to know how increment & decrement operations works on pointer with example ) view (INCREMENT AND DECREMENT OPERATIONS ON POINTER)
//to read n numbers ,stores them in 1-D array and display them
#include<iostream>
using namespace std;
main()
{
    int arr[30],*ptr,n;
    cout<<"Enter the number of elements to store in array "<<endl;
    cin>>n;
    ptr=arr;    // assigning pointer to array
    for(int i=0;i<n;i++)
    {cout<<"Please enter "<<i <<" element  ";
    cin>>arr[i];
}
    for(int i=0;i<n;i++)
    {cout<<i<<" element of array is  "<<*ptr<<endl;    //printing elements of array using pointer
    ptr++;
}
return 0;
}

LINEAR SEARCH USING ARRAY.

Linear search means searching an element  from a linear set of array .

#include<iostream>
using namespace std;
main()
{
    int n,arr[50],i,count=0,num;
    cout<<"Enter the number of elements to store in array "<<endl;
    cin>>n;
    cout<<"enter elements of array"<<endl;
for(i=0;i<n;i++)
{
        cin>>arr[i];
}
cout<<"enter element to found  ";
cin>>num;
    
for(i=0;i<n;i++)

{ if (num==arr[i])
{cout<<"Element found at position :  "<<count<<endl;
}
else 
{
count++;
}
}
if(count>=n)
{
cout<<"element not found";
}
    return 0;
}

 

LINEAR SEARCH USING ARRAY AND POINTERS.(POINTER TO  ARRAY)

#include<iostream>
using namespace std;

main()
{
int arr[50],i,count=0,num,*ptr,n;
cout<<"Enter the number of elements to store in array "<<endl;
cin>>n;
cout<<"enter elements of array"<<endl;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"enter element to found  ";
cin>>num;
ptr=arr;// pointer pointing to first element of array(consisting address of 1st arr[0]
for(i=0;i<n;i++)
{
if (num==*ptr)// *ptr means value at address pointer is pointing
{cout<<"Element found at position :  "<<count;
}
else 
{
ptr++;// incrementing ptr to next address
count++;
}
}
if(count>=n)
{
cout<<"element not found";
}    
return 0;
}

BINARY SEARCH USING ARRAY.

Binary search is  an algorithm to search an element from an  array in minimum possible comparisons.

The logic used here is:

  1. To find the middle element of array: by adding 1st position  and last  position of an element of array.for ex: array is of size 10,arr[10] here 1st element is at position arr[0] and last element at position arr[9],  finding middle element :mid=(0+9)/2.
  2. Then comparing middle element with the item .
  • if mid == item(search sucsessful)
  • if mid<item (then searching 1st half of  array by again finding  mid element ,by making mid as the last element of array.
  • if mid>item (then searching 2nd  half of  array by again finding  mid element  by making mid as the first element of array

While using binary search the elements of array should be in ascending (sorted) order.

//binary search
#include<iostream>
using namespace std;

main()
{
    int n,a[n],i,count=0,num,beg,end,mid;
    cout<<"enter no.of elements you need to enter :";
    cin>>n;
    cout<<"enter elements of array in ascending order"<<endl;
for(i=0;i<n;i++)
{
        cin>>a[i];
}
cout<<"enter element to be searched ";
cin>>num;
    
beg=0;
end=n-1;
mid=(beg+end)/2;
while(beg<=end && num!=a[mid] )
{if(num>a[mid])
beg=mid+1;

else
if(num<a[mid])

end=mid-1;
mid=(beg+end)/2;
}

if (num==a[mid])
{cout<<"Element found at position :  "<<mid;
}
else 
{
cout<<"element not found";
}
    
return 0;
}

BINARY SEARCH USING ARRAY AND POINTERS.(POINTER TO  ARRAY)

//binary search
#include<iostream>
using namespace std;

main()
{
int n,a[50],i,count=0,num,beg,end,mid,*ptr;
cout<<"enter no.of elements you need to enter :";
cin>>n;
cout<<"enter elements of array in ascending order"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter element to be searched ";
cin>>num;
ptr=a;    
beg=0;
end=n-1;
mid=(beg+end)/2;
while(beg<=end && num!=*(ptr+mid))
{if(num>*(ptr+mid))
beg=mid+1;
else
if(num<*(ptr+mid))
end=mid-1;
mid=(beg+end)/2;
}
if (num==*(ptr+mid))
cout<<"Element found at position :  "<<mid;
else 
cout<<"element not found";
return 0;
}

 

Concept of bubble sort

As here in the diagram explained first step

1.Each  element will be compared to  its consecutive element if the consecutive element is smaller than the previous one, the element is swapped otherwise no swapping is done.

  • As first 5  is compared to 3 , 3 is smaller than 5 (swapping done).
  • Again now 5 is compared to next element i.e 15, 15 is greater than 5 (no swapping).
  • Then 15 is compared to 2, since 2 is smaller than 15 ( swapping done)
  • Again 15 is compared to 30, 30 is greater than 15 (no swapping)
  • First step completed
  • Same steps are repeated again until all the elements are sorted.
#include<iostream>
using namespace std;
main()
{
int n,arr[100],i,j,temp;
cout<<"Enter the number of elements to store in array "<<endl;
cin>>n;
cout<<"enter elements of array"<<endl;
for(i=0;i<n;i++)
{
        cin>>arr[i];
}
for(i=0;i<n;i++)
{
   for(j=0;j<n-1-i;j++)
 {
   if (arr[j] > arr[j+1]) 
   {
    temp = arr[j];
    arr[j] = arr[j+1];
    arr[j+1] = temp;
    }
 }
}
cout<<"sorted array is : ";
for(i=0;i<n;i++)
{
    cout<<arr[i]<<endl;
}
return 0;
}

BUBBLE SORT USING ARRAY AND POINTERS.(POINTER TO  ARRAY)

#include<iostream>
using namespace std;

main()
{
    int a[100],i,j,temp,n;
    int *p;
    cout<<"enter the siz eof the array";
    cin>>n;
    p=a;
    for(i=0;i<n;i++)
    {
    cin>>*p;
    p++;
    }
    p=a;
    cout<<"unorted array=";
    for(i=0;i<n;i++)
    {
    cout<<*p<<" ";
    p++;
    }
    p=a;
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {if(*(p+i)>*(p+j))
        {
        temp=*(p+i);
        *(p+i)=*(p+j);
        *(p+j)=temp;
        }
        }
    }
    cout<<endl<<"sorted array="<<endl;
    for(i=0;i<n;i++)
    cout<<a[i]<<" ";
    return 0;    
}

 

6 thoughts on “Pointers and Array

    • int num[5]; is a declaration of array variable named num of size 5,
      where int[5] is a wrong way of declaration, as you are not giving name of array variable, you can declare and initialize array as:
      int num[5]={1,2,3,4,5};

  1. I did a program about swapping

    #include
    #include
    int main()
    {
    int x,y,z;
    printf(“Enter any two numbers for x and y\n”);
    scanf(“%d %d\n”,&x,&y);
    printf(“Before swapping the number is x=%d,y=%d\n”,x,y);
    x=y;
    y=z;
    z=x;
    printf(“After swapping the number is x=%d,y=%d\n”,x,y);
    getch();
    }

    i have not given any type of value,My out is

    before the interchange value is 10 20
    after the interchange value is 20 0

    why i am getting this value can u explain it

Leave a Comment