Tuesday, 3 April 2018

Pointer in Arrays

Consider the declaration
int b[5];
int *ptr;
The pointer to array is given as below
ptr=&b[0];
which is same as
ptr=b;
If the pointer is incremented to the next data elements , then address of the incremented value of the pointer will be same as the value of the next element.

ptr=&b[0]            *ptr=b[0]
ptr+1=&b[1]        *(ptr+1)=b[1]
ptr+2=&b[2]        *(ptr+2)=b[2]
ptr+3=&b[3]        *(ptr+3)=b[3]

The expression-
  value[i] is same as (value+i)
that is
b[i] ==*(b+i)
b[0] ==*(b+0)
b[1] ==*(b+1)
b[2] ==*(b+2)
b[3] ==*(b+3)

WAP to initialize and display five elements from array by using pointer to array.

#include<conio.h>
#include<iostream.h>
void main( )
{
int a[5]={10,20,30,40,50};
int *ptr, i;
clrscr( );
ptr=&a[0];
cout<<"\n\n\tElements in array are--->";
for(i=0;i<5;i++)
{
cout<<" "<<*ptr;
ptr++;
}
getch( );
}


Output :-
Element in array are---> 10 20 30 40 50

WAP to search  an element from array using pointers to array

#include<conio.h>
#include<iostream.h>
void main( )
{
int a[5];
int *ptr, i, f=0, item;
clrscr( );
ptr=&a[0];
cout<<"\n\n\tEnter Five elements";
for(i=0;i<5;i++)
{
cin>>*ptr;
ptr++;
}
cout<<"\n\n\tEnter Element to search";
cin>>item;
ptr=&a[0];
for(i=0;i<5;i++)
{
if(*ptr==item)
{
cout<<"\n\n\tElement is searched in array at"<<" "<<i+1<<"th location";
f=1;
break;
}
ptr++;
}
if(f==0)
cout<<"\n\n\tElement is not searched in array";
getch( );
}

Output:-
Enter Five element 12 23 34 45 56

Enter Element to search 56

Element is searched in array at 5 th location

WAP to insert an element at location of array by using pointer to array.

#include<conio.h>
#include<iostream.h>
void main( )
{
int a[10], i, *ptr1,n,loc,ele;
clrscr( );
cout<<"\n\n\tEnter Size of the Array";
cin>>n;
ptr1=&a[0];
cout<<"\n\n\tEnter Element in the Array";
for(i=0;i<n;i++)
{
cin>>*ptr1;
ptr1++;
}
cout<<"\n\n\tEnter Location to insert in to array";
cin>>loc;
cout<<"\n\n\tEnter Element to insert into Array";
cin>>ele;
ptr1--;
for(i=n-1;i>=0;i--)
{
if(i==loc-1)
{
*(ptr1+1)=*ptr1;
*ptr1=ele;
}
*(ptr1+1)=*ptr1;
ptr1--;
}
ptr1=&a[0];
cout<<"\n\n\tElement in the Array after Insertion--->";
for(i=0;i<n+1;i++)
{
cout<<" "<<*ptr1;
ptr1++;
}
getch( );
}

Output:-
Enter Size of the Array 5
Enter Element in the Array 12 23 34 45 56
Enter Location to insert in to array 3
Enter Element to insert into Array 90
Element in the Array after Insertion is---> 12 12 23 90 45 56

WAP to delete an element from an array from the specified location using pointers to array.

#include<conio.h>
#include<iostream.h>
void main( )
{
int a[10],i,*ptr1,n,loc;
clrscr( );
cout<<"\n\n\tEnter Size of the Array";
cin>>n;
ptr1=&a[0];
cout<<"\n\n\t Enter the Elements in the Array";
for(i=0;i<n;i++)
{
cin>>*ptr1;
ptr1++;
}
cout<<"\n\n\t Enter the Location of element to be deleted";
cin>>loc;
ptr1=&a[loc];
for(i=loc-1;i<n;i++)
{
*ptr1=*(ptr1+1);
ptr1++;
}
ptr1=&a[0];
cout<<"\n\n\tElements in Array after deletion is--->";
for(i=0;i<n-1;i++)
{
cout<<" "<<*ptr1;
ptr1++;
}
getch( );
}

Output:-
Enter Size of the Array 5
Enter the Elements in the Array 12 23 34 45 56
Enter the location of element to be deleted 3
Elements in Array after deletion is---> 12 23 34 56

0 Comments:

Post a Comment

Popular Posts

Categories

AI (32) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (888) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses