0% found this document useful (0 votes)
11 views4 pages

Practical 5 POINTERS - Edited

Uploaded by

huzaifakadri95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Practical 5 POINTERS - Edited

Uploaded by

huzaifakadri95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

FYCS02 C++ Programming

Fycs76
Practical-5
Q1. Pointer Basics: Write a C++ program that declares an integer variable, initializes it, and
then uses a pointer to modify its value. Print the value of the integer before and after the
mofication using both the variable and the pointer.
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int num=10;
int *p;
p=&num;
cout<<"Initial value of number:"<<num<<endl;
cout<<"Initial value of number using pointer:"<<*p<<endl;
*p=20;
cout<<"Modified value of number:"<<num<<endl;
cout<<"Modified value of number using pointer:"<<*p<<endl;
getch();
}
Output:

Q2. Array access Using Pointers: Create an integer array in C++. Use a pointer to access and
print each element of the array. Make sure not to use the array subscript([]) notation.
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int a[5]={10,20,30,40,50};
int *p;
p=a;
cout<<"Array elements using pointer:"<<endl;
for(int i=0;i<5;i++){
cout<<*(p+i)<<" ";
}
getch();
}
Output:
Fycs76
FYCS02 C++ Programming

Q3. Dynamic Memory Allocation for a Single Variable: Write a C++ program that
dynamically allocates memory for a single integer. Prompt the user to input a value, store it
using the pointer, and print the value. Finally, free the allocated memory.(use
delete(pointername) to free up dynamically allocated memory)
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int *p;
p=new int;
cout<<"Enter an integer value:";
cin>>*p;
cout<<"The value you entered is:"<<*p<<endl;
delete p;
getch();
}
Output:

Q4. Swapping Two Variables: Implement C++ function that takes two pointers as parameters
and swaps the values of the two variables they point to. Test the function by swapping two
integers in the main() function.
#include<iostream.h>
#include<conio.h>
void swap(int *a,int *b){
int temp=*a;
*a=*b;
*b=temp;
}
void main(){
clrscr();
int x=10,y=20;
cout<<"Before swapping:"<<endl;
cout<<"x="<<x<<",y="<<y<<endl;
swap(&x,&y);
cout<<"After swapping:"<<endl;
cout<<"x="<<x<<",y="<<y<<endl;
getch();
}
Output:
Fycs76 FYCS02 C++ Programming

Q5. Basic Pointer Arithmetic: Write a C++ program that creates an array of integers using
dynamic memory allocation. Populate the array with values, and then use pointer arithmetic
to print the values in reverse order.
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int n,i,*a;
cout<<"Enter the number of elements:";
cin>>n;
a=new int[n];
cout<<"Enter "<<n<<" integer values:"<<endl;
for(i=0;i<n;i++){
cin>>*(a+i);
}
cout<<"Array values in reverse order:"<<endl;
for(i=n-1;i>=0;i--){
cout<<*(a+i)<<" ";
}
delete[] a;
getch();
}
Output:

Q6. Dynamic Memory Allocation and Deallocation: Implement a C++ function that
dynamically allocates memory for a 2D array (matrix), allows the user to input the elements,
and then prints the matrix. Ensure that the program correctly frees the memory after usage.
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int r,c,i,j,**m;
cout<<"Enter the number of rows:";
cin>>r;
cout<<"Enter the number of columns:";
cin>>c;
m=new int*[r];
for(i=0;i<r;i++){
m[i]=new int[c];
}
cout<<"Enter elements of the matrix:"<<endl;
for(i=0;i<r;i++){
Fycs76 FYCS02 C++ Programming

for(j=0;j<c;j++){
cin>>m[i][j];
}
}
cout<<"The matrix is:"<<endl;
for(i=0;i<r;i++){
for(j=0;j<c;j++){
cout<<m[i][j]<<" ";
}
cout<<endl;
}
for(i=0;i<r;i++){
delete[] m[i];
}
delete[] m;
getch();
}
Output:

You might also like