You are on page 1of 17

Pointer Programming Exercise & Solutions

Pointer Basic
 WAP to add 2 numbers using Pointer (Static Memory Allocation)

C++ Program
#include<iostream>
using namespace std;
int main()
{
int first, second, *p, *q, sum;
cout<<"Enter two integers to add"<<endl;
cin>>first>>second;
p = &first;
q = &second;
sum = *p + *q;
cout<<"Sum of entered numbers="<<sum;
return 0;
}

 WAP to add two number using pointer and use dynamic memory
allocation.
In this program we have not declared 2 variables at compile time. We are going to accept values
at run time by allocating memory at run time.

C++ Program

#include<iostream>
using namespace std;
int main()
{
int *ptr,sum=0,i;
ptr = new int;
for(i=0;i<2;i++)
{
cout<<"Enter a number : "<<endl;
cin>>*ptr;
sum = sum + (*ptr);
}
cout<<"Sum = "<<sum;
return 0;
}

In the above program memory will be available at any moment of time.


Suppose memory is unavailable at run time then it will return NULL. We are checking the value
of pointer with NULL

C++ Program

#include<iostream>
Using namespace std;
#include<stdlib.h>
int main()
{
int *ptr,sum=0,i;
clrscr();
// Allocate memory Equivalent to 1 integer
ptr = new int;
if(ptr==NULL)
{
cout<<"Error in Allocating Memory"<<endl;
exit(0);
}
for(i=0;i<2;i++)
{
cout<<"Enter a number : "<<endl;
cin>>*ptr;
sum = sum + (*ptr);
}
cout<<"Sum = "<<sum;
return 0;
}

Array & Pointer


 Distinguish between Arrays and Pointer
 Array- Array allocates memory space automatically
 Pointer-Explicitly assigned to point to an allocated space.

 Arays cannot be resized


 Pointers can be resized using new operator.

 WAP to access array elements using Pointer

C++ Program

#include <iostream>
using namespace std;
int main(){
int data[5], i;
cout<<"Enter elements:"<<endl;
for(i=0;i<5;++i)
cin>>*(data+i);
cout<<"You entered:"<<endl;
for(i=0;i<5;++i)
cout<<*(data+i)<<endl;
return 0;
}
 WAP to access array elements using Pointer (Dynamic Memory Allocation)

C++ Program

#include <iostream>
using namespace std;
int main(){
int *data,n,i;
cout<<"Enter no of elements:"<<endl;
cin>>n;
data=new int[n];
cout<<"Enter the numbers"<<endl;
for(i=0;i<n;++i)
cin>>*(data+i);
cout<<"You entered:"<<endl;
for(i=0;i<n;++i)
cout<<*(data+i)<<endl;
return 0;
}

 WAP using pointers to read in an array of integers and print its elements
in reverse order.

C++ Program

#include<iostream>
using namespace std;
int main()
{
int arr[10],size,i;
int *ptr;
cout<<"Enter the size of array : "<<endl;
cin>>size;
ptr=&arr[0];
cout<<"Enter integers into array: "<<endl;
for (i = 0; i < size; i++) {
cin>>*ptr;
ptr++;
}
ptr = &arr[size - 1];
cout<<"Elements of array in reverse order are :"<<endl;

for (i = size - 1; i >= 0; i--) {


cout<<*ptr<<endl;
ptr--;
}
return 0;
}

 WAP using pointers to read in an array of integers and print its elements
in reverse order. (Dynamic Memory Allocation)

C++ Program

#include<iostream>
using namespace std;
int main()
{
int size,i;
int *ptr;
cout<<"Enter the size of array : "<<endl;
cin>>size;
ptr=new int[size];
cout<<"Enter integers into array: "<<endl;
for (i = 0; i < size; i++) {
cin>>*(ptr+i);
}
cout<<"Elements of array in reverse order are :"<<endl;

for (i = size - 1; i >= 0; i--) {


cout<<*(ptr+i)<<endl;
}
return 0;
}

 WAP to find sum of n elements entered by user. To perform this program,


allocate memory dynamically using new operator.

C++ Program

#include <iostream>
using namespace std;
#include <stdlib.h>
int main()
{
int n,i,*ptr,sum=0;
cout<<"Enter number of elements: "<<endl;
cin>>n;
ptr=new int[n]; //memory allocated using new
if(ptr==NULL)
{
cout<<"Error! memory not allocated."<<endl;
exit(0);
}
cout<<"Enter elements of array: "<<endl;
for(i=0;i<n;++i)
{
cin>>*(ptr+i);
sum+=*(ptr+i);
}
cout<<"Sum="<<sum;
delete []ptr;
return 0;
}

 WAP to find maximum element of an array using pointer.


C++ Program

#include<iostream>
using namespace std;
int main()
{
int a[10],n,i,max;
int *p;
cout<<"Enter the size of array: "<<endl;
cin>>n;
cout<<"Enter the elements in the array"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Elements in the array are:"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<endl;
p=&a[0];
max=a[0];
for(i=1;i<n;i++)
{
if(max<*p)
max=*p;
p++;
}
cout<<"Maximum element in the array is: "<<max;
return 0;
}

Function & Pointer


 Write a C++ function using pointers to exchange the values stored in two
locations in the memory?

C++ Program

#include<iostream>

using namespace std;

void swap(int*,int*);

int main()

int a,b;

cout<<"Enter two numbers "<<endl;

cin>>a>>b;

cout<<"The values of a & b entering into function"<<endl;

cout<<a<<" "<<b<<endl;

swap(&a,&b);

cout<<"The values after executing the function"<<endl;

cout<<a<<" "<<b<<endl;

return 0;

void swap(int *x,int *y)

int t;
t=*x;

*x=*y;

*y=t;

cout<<"The values of a & b inside the function"<<endl;

cout<<*x<<" "<<*y<<endl;

 WAP to find the maximum of 2 numbers using Function returning Pointer.

C++ Program

#include<iostream>
using namespace std;
int * max(int*,int*);
int main()
{
int a,b;
cout<<"Enter 2 numbers"<<endl;
cin>>a>>b;
int *res=max(&a,&b);
cout<<"Maximum of 2 numbers is "<<*res<<endl;
return 0;
}
int *max(int *x,int *y)
{
if(*x>*y)
return x;
else
return y;
}
 WAP to find the factorial of given number using function and pointer.

C++ Program

#include<iostream>
using namespace std;
void factCompute(int,int*);
int main()
{
int n, factorial;
cout<<"Enter a number"<<endl;
cin>>n;
factCompute(n,&factorial);
cout<<"The factorial of of given no is "<<factorial;
return 0;
}
void factCompute(int a,int *b)
{
int m;
*b = 1;
for(m = 1; m <= a; m++)
*b = *b * m;
}

Function, Array & Pointer


 WAP for calculating the sum of all elements stored in an array using
pointers.

C++ Program
#include<iostream>
using namespace std;
void sum(int*,int);
int main()
{
int a[10],n;
cout<<"Enter the size of array"<<endl;
cin>>n;
cout<<"Enter elements into array"<<endl;
for (int i=0;i<n;i++)
cin>>a[i];
sum(a,n);
return 0;
}
void sum(int *p,int n)
{
int sum=0;
for (int i=0;i<n;i++)
{
sum=sum+*(p);
p++;
}
cout<<"The sum of array elements is"<<sum<<endl;
}

 WAP to implement linear search using function, array and pointer

C++ Program
#include<iostream>
using namespace std;
int linearsearch(int *arr,int ele,int size)
{
int i;
int flag=0;
for(i=0;i<size;i++)
{
if(*(arr+i)==ele)
{
flag=1;
break;
}
}

if(flag==1)
return i;
else
return -1;

int main()
{
int *a,i,n,m;
cout<<"Enter the size of an array: "<<endl;
cin>>n;
a=new int[n];
cout<<"Enter the elements of the array: "<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}

cout<<"Enter the number to be search: "<<endl;


cin>>m;
i=linearsearch(a,m,n);
if(i==-1)
cout<<"The number is not in the list"<<endl;
else
cout<<"The number is found at position"<<i+1<<endl;

return 0;
}

Class & Pointer

 WAP to input and display the employee information using Class & Pointer.

C++ Program

#include<iostream>
using namespace std;
class Employee
{
private:
int eid;
char name[30];
double salary;
public:
void readInfo()
{
cout<<"Enter employee Id"<<endl;
cin>>eid;
cout<<"Enter employee name"<<endl;
cin>>name;
cout<<"Enter employee salary"<<endl;
cin>>salary;
}
void showInfo()
{
cout<<"Employee Id is"<<eid<<endl;
cout<<"Employee name is"<<name<<endl;
cout<<"Employee salary is"<<salary<<endl;
}
};
int main()
{
Employee e1;
Employee *e;
e=&e1;
e1.readInfo();
e1.showInfo();
e->readInfo();
e->showInfo();
(*e).readInfo();
(*e).showInfo();
return 0;
}

 WAP to input and display the employee information using Class & Pointer.
(Using Dynamic Memory Allocation)

C++ Program

#include<iostream>
using namespace std;
class Employee
{
private:
int eid;
char name[30];
double salary;
public:
void readInfo()
{
cout<<"Enter employee Id"<<endl;
cin>>eid;
cout<<"Enter employee name"<<endl;
cin>>name;
cout<<"Enter employee salary"<<endl;
cin>>salary;
}
void showInfo()
{
cout<<"Employee Id is"<<eid<<endl;
cout<<"Employee name is"<<name<<endl;
cout<<"Employee salary is"<<salary<<endl;
}
};
int main()
{
Employee *e=new Employee;
/*
OR
it can be represented as
Employee *e;
e=new Employee;
*/
e->readInfo();
e->showInfo();
(*e).readInfo();
(*e).showInfo();
return 0;
}

Character Pointer
 WAP using pointer to determine the length of a character string.

C++ Program

#include<iostream>
using namespace std;
int main()
{
char str[20], *ch;
int i=0;
cout<<"Enter the string \n";
cin>>str;
ch=str;
while (*ch!='\0')
{
i++;
ch++;
}
cout<<"The length of the given string is "<<i;
return 0;
}

Advantages of using Pointers:


 Pointers enable to access a variable that is defined outside the function.
 Pointers are more efficient in handling the data tables.
 Pointers reduce the length and complexity of a program.
 Pointers increase the execution speed.
 The use of pointer array to character string results in saving of data storage space in
memory.
 The real power of C lies in the proper use of Pointers.
Exercises

1. WAP to find the second largest element in an array using function and pointer.
2. WAP to remove the duplicate element of an array using pointer.
3. WAP to insert an element at desired position of an array using pointer.
4. WAP to delete an element at desired position of an array using pointer.
5. WAP to implement binary search using function and pointer.
6. WAP to sort an array using bubble sort using pointer.
7. WAP to merge two array using pointer.

You might also like