Professional Documents
Culture Documents
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;
}
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;
}
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;
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;
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;
}
#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;
}
C++ Program
#include<iostream>
void swap(int*,int*);
int main()
int a,b;
cin>>a>>b;
cout<<a<<" "<<b<<endl;
swap(&a,&b);
cout<<a<<" "<<b<<endl;
return 0;
int t;
t=*x;
*x=*y;
*y=t;
cout<<*x<<" "<<*y<<endl;
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;
}
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;
}
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];
}
return 0;
}
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;
}
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.