Pointer Exercises
Pointer Exercises
PART I
1.Using a pointer, write a C++ program that will display an area and volume of a
4
sphere (area=4𝜋r2 , volume= 3 𝜋 r3 ).
2.Suggest an output of the following program
#include<iostream>
using namespace std;
int main (){
int num, *ptr1, *ptr2;
ptr1=#
ptr2=ptr1+8;
cout<<”the difference is:”<<ptr2-ptr1<<endl;
return 0;
}
3.Which of the following gives the value stored at the address pointed to by the pointer
ptr?
A.Value(ptr)
B.ptr
C.&ptr
D. *ptr
4.Using a function pointer, write a C++ program which will compute the area of a
rectangle.
5. Design a C++ Program to print the members of a structure using arrow operator.
6. Explain the memory allocation and compare dynamic memory over static memory
allocation.
7. Explain nesting structure.
8. Differentiate array with structure.
9. a. What is the pointer in C++
b. Explain the use of Address Operator (&) and operator (*).
10. a. Define structure in C++
b. Given a structure definition,
struct Employee {
string emName;
string emSex;
string emAddress;
} Emp;
A member of the structure can be accessed by which one of the following statements?:
a. Employee.emName;
b. Emp.emName;
c. Emp->emName;
11. What are the operators used to access members of a structure using its object?
12. Write a program that calculates the roots of a quadratic Equation 2x2+6x +4 in
C++ using pointers.
PART II
Q1. Write a C++ program that uses a pointer to read a radius of a sphere from the
user and display its volume. Declare Pi as a constant variable and assign to it 22/7
value.
Q2. Write a C++ program that requests the user to enter 2 numbers and displays their
sum, difference and product by using pointers.
Q3. Develop a C++ application which computes the sum and average of the three given
integers. To achieve this, follow the instructions below:
i. Define function addition () with Three pointer parameters
ii. Declare sum as local variable to hold the sum calculated by addition ()
iii. Define moyenne () function calculates the average of three given integers and
display it.
Q4. Write a C++ program which asks user to enter a list of 15 numbers and stores
them in an array called Numbers. Your program must use a pointer to sort those
numbers to print Odd ones from the list and their sum, then again displays Even
numbers and their sum separately on the screen.
Q5. Write a C++ program which asks user to enter a number from the keyboard and
then use a pointer to print its multiplication table up to 12.
Q6. Write a C++ program which calculate the sum, the mean and the percentage of
the marks of students in 5 courses namely Visual Basic (vb), C++ programming (cpp),
web design (wd), mathematics (math) and physics (phy). The User shall be asked the
number of students s/he will record. The program will prompt user to enter the name
and surname of the students then the marks obtained by each student in 5 courses,
the program will then calculate the sum, mean and the percentage then displays on
the screen the name, surname, mean and percentage of that student. To implement
the above scenario, use a structure called student.
Q7. Using switch case, develop a C++ application to read marks (per cent) of university
students and put these students into classes according to their marks.
Classes with their corresponding marks:
• The Employee structure is nested inside the structure Organisation and it has
the data members like employee_id, name, salary.
Write C++ program to implement the above Scenario by getting and displaying the
information about organization and Employee.
Q9. Write a structure to store the roll no., name, age (between 11 to 14) and address
of students (10). Store the information of the students.
1 - Write a function to print the names of all the students having age 14.
2 - Write another function to print the names of all the students having even roll no.
3 - Write another function to display the details of the student whose roll no is given
(i.e. roll no. entered by the user).
Q10. Write a structure to store the name, account number and balance of customers
(more than 10) and store their information.
1 - Write a function to print the names of all the customers having balance less than
$200.
2 - Write a function to add $100 in the balance of all the customers having more than
$1000 in their balance and then print the incremented value of their balance.
Additional Information