0% found this document useful (0 votes)
89 views

What Are Pointers? What Is The Purpose of Using Pointers?: ASK Olution

The document contains solutions to 9 tasks related to pointers in C++. It explains that pointers store the memory address of a variable and are used to manage dynamically allocated memory. It then provides code solutions to tasks involving declaring and using pointers to pass values between functions, store array elements, find maximum values, and calculate circle properties by passing addresses to a function. Benefits of pointers include direct memory access, passing multiple return values from functions, reduced storage and time complexity. Applications include file handling, dynamic memory allocation, and polymorphism through base class pointers.

Uploaded by

Khawar Khalil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

What Are Pointers? What Is The Purpose of Using Pointers?: ASK Olution

The document contains solutions to 9 tasks related to pointers in C++. It explains that pointers store the memory address of a variable and are used to manage dynamically allocated memory. It then provides code solutions to tasks involving declaring and using pointers to pass values between functions, store array elements, find maximum values, and calculate circle properties by passing addresses to a function. Benefits of pointers include direct memory access, passing multiple return values from functions, reduced storage and time complexity. Applications include file handling, dynamic memory allocation, and polymorphism through base class pointers.

Uploaded by

Khawar Khalil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Page 1 of 8

TASK #1:
What are pointers? What is the purpose of using pointers?
SOLUTION:
A pointer however, is a variable that stores the memory address as its value. A pointer variable points to a
data type (like int or string) of the same type and is created with the * operator.
Pointers are used to store and manage the addresses of dynamically allocated blocks of memory. Such
blocks are used to store data objects or arrays of objects. Most structured and object-oriented languages
provide an area of memory, called the heap or free store, from which objects are dynamically allocated.
Task #2:
Find the output of the following code:
main()
{
int a,b,c;
int *x,*y,*z;
a=14; b=9; c=7;
x=&a;
y=&b;
z=&c;
*x=*y+*z;
a=b+c;
*y = (*z)*(*x);
(*y)--;
(*z)++;
*z = (*y)+(*x);
A = a+b+c;
*x=(*z)-(*y);
*z=(*z)-(*y)+20;
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
cout<<*x<<endl;
cout<<*y<<endl;
cout<<*z<<endl;
return 0;
}

SOLUTION:
#include<iostream>
using namespace std;
main()
{
int a,b,c;
int *x,*y,*z;
a=14; b=9; c=7;
x=&a;
y=&b;
Page 2 of 8

z=&c;
*x=*y+*z;
a=b+c;
*y = (*z)*(*x);
(*y)--;
(*z)++;
*z = (*y)+(*x);
//A = a+b+c;
*x=(*z)-(*y);
*z=(*z)-(*y)+20;
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
cout<<*x<<endl;
cout<<*y<<endl;
cout<<*z<<endl;
return 0;
}
Page 3 of 8

TASK #3:
Write a program that asks the user to enter integers as inputs to be stored in the variables 'a'
and 'b' respectively. There are also two integer pointers named ptrA and ptrB. Assign the
values of 'a' and 'b' to ptrA and ptrB respectively, and display them.
SOLUTION:
#include <iostream>
using namespace std;
main(){
int a, b, *ptrA, *ptrB;
cout << "Enter value of A: ";
cin >> a;
cout << "Enter value of B: ";
cin >> b;
ptrA = &a;
ptrB = &b;
cout << "Value of ptrA is " << *ptrA
<< " stored in address "<< ptrA <<"\n";
cout << "Value of ptrB is " << *ptrB
<<" stored in address "<< ptrB <<"\n";
}

TASK #4:
Write a program which takes inputs from user, store in the array and print in reverse order
using pointers.
SOLUTION:
#include <iostream>
using namespace std;
int main()
{
Page 4 of 8

int input[100], output[100], count, i;


cout << "Enter number of elements in array: ";
cin >> count;
cout << "Enter " << count << " numbers \n";
for(i = 0; i < count; i++)
cin >> input[i];
cout << "Reversed Array\n";
for(i = 0; i < count; i++)
{
output[i] = input[count-i-1];
cout << output[i] << " ";
}
}

TASK #5:
Write a program which take input from user and store its address in a pointer and pass the
address to pointer in a function and double the input value and display it in main.
SOLUTION:
#include <iostream>
using namespace std;
void func(double& n) { n=10.72; }
Page 5 of 8

int main()
{
double *ptr, i;
cout << "Enter Any Number: ";
cin>>i;
ptr = &i;
cout<<"Before Passing the address to pointer in a function: "<<i<<endl;
func(i);
cout<<"After Passing the address to pointer in a function: "<<i;
}

TASK #6:
Write a program which takes inputs from user in an array of size 7. The program prints on
screen a pointer that point to the max value.
SOLUTION:
#include<iostream>
using namespace std;
int *findMax(int arr[],int n);
int main(){
int n=7,i,*p;
cout<<"*** Enter 7 numbers ***\n";
int arr[n];
for(i=0;i<n;i++) {
cout<<"Enter value "<<i+1<<": ";
cin>>arr[i];
}
p=findMax(arr,n);
cout<<"\nThe max value is: "<<*p;
return 0;
}
Page 6 of 8

int *findMax(int data[],int n){


int *max=data;
int i;
for(i=1;i<n;i++){
if(*max<*(max+i)) *max=*(max+i);
}
return max;
}
Page 7 of 8

TASK #7:
Write a program that gets the radius from user, pass radius to a function areaperi() and
function areaperi() returns “area” and “perimeter” by dereference operator.
SOLUTION:
#include<iostream>
using namespace std;
void areaperi(int r, float *a, float *p)
{
*a = 3.14*r*r;
*p = 2*3.14*r;
}
int main()
{
int radius;
float area, perimeter;
cout<<"Enter radius of circle: ";
cin>>radius;
areaperi(radius, &area, &perimeter);
cout<<"\nArea is: "<<area;
cout<<"\nPerimeter is: "<<perimeter;
}
Page 8 of 8

TASK #8:
Write a program that take two numbers an input from user. Find the maximum from both of
them using the dereference pointer comparison.
SOLUTION:
#include<iostream>
using namespace std;
int main(){
int a, b, *ptrA, *ptrB;
cout<<"Enter number 1: ";
cin>>a;
cout<<"Enter number 2: ";
cin>>b;
ptrA = &a;
ptrB = &b;
if(*ptrA > *ptrB)
cout<<"\nMaximum number is: "<<*ptrA;
else
cout<<"\nMaximum number is: "<<*ptrB;
}

TASK #9:
What are benefits of using the pointers and what are the applications?
SOLUTION:
ADVANTAGES:
Pointers provide direct access to memory.
Pointers provide a way to return more than one value to the functions.
Reduces the storage space and complexity of the program.
Reduces the execution time of the program.
Provides an alternate way to access array elements.
APPLICATIONS:
Pointers are used for file handling. Pointers are used to allocate memory dynamically. In C++, a pointer
declared to a base class could access the object of a derived class.

Prepared By: Khawar Khalil

You might also like