You are on page 1of 2

Object Oriented Programming (W4)

Lab 3
Tuesday; April 11, 2023
Instructor: Arifah Azhar
Email: arifah.azhar@umt.edu.pk

OBJECTIVES THINGS TO REMEMBER


 Understand what pointers are and how  Indent your code
they work in C++.  Comment your code
 Declare and initialize pointers.  Use meaningful variable names
 Use pointers to manipulate data in  Plan your code carefully before coding
memory.  Do not take input or output inside a
 Pass pointers to functions and use them function unless it is an input or output
in function arguments. function
 Understand the difference between  Create a menu based switch based lab
pointers and references. that enables you to repeated select
between pre, in or post lab modules.
Once in a respective module, create a
sub menu to execute the respective
question out of the module.

Pre-lab module [CLO 2]

This lab is designed such that you are provided with each and every step to solve the problem.

Exersices. Check your understanding by solving these problems


Task 1
1. Declare an integer variable named num and assign it the value 10.
2. Declare a pointer variable named ptr that points to an integer.
3. Initialize ptr to point to the memory address of num.
4. Print the value of num and the value of ptr.
5. Print the value that ptr points to using the dereference operator (*ptr).

Task 2
1. Declare an integer variable named num and assign it the value 20.
2. Declare a pointer variable named ptr that points to an integer.
3. Initialize ptr to point to the memory address of num.
4. Use the dereference operator (*ptr) to change the value of num to 30.
5. Print the value of num and the value that ptr points to.

Task 3

1. Declare a function named swap that takes two integer pointers as arguments.
2. Use the dereference operator to swap the values that the pointers point to.
3. In the main function, declare two integer variables named x and y and assign them values.
4. Declare two integer pointer variables named ptr_x and ptr_y and initialize them to point to the
memory addresses of x and y, respectively.
page 1 of 2
5. Call the swap function with the ptr_x and ptr_y pointers as arguments.
6. Print the values of x, y, ptr_x, ptr_y, &x, &y

Task 4 (problem solved in class)

You have been given an array of integers, and you need to manipulate the array using pointers
in the following ways:
1. Declare an integer array of size 5 and initialize it with values of your choice.
2. Declare a pointer variable of type integer and initialize it to point to the first element of the ar-
ray.
3. Create a loop that uses the pointer to traverse the array and print out each element.
4. Create a function named "findMax" that takes a pointer to the first element of the array and the
size of the array as arguments. The function should find the maximum value in the array and
return it.
5. Create a function named "reverseArray" that takes a pointer to the first element of the array
and the size of the array as arguments. The function should reverse the order of the elements
in the array.
6. Create a function named "sortArray" that takes a pointer to the first element of the array and
the size of the array as arguments. The function should sort the elements in the array in as-
cending order.
7. Test the program by calling the "findMax" function and displaying the maximum value in the
array, then calling the "reverseArray" function and displaying the reversed array, and finally
calling the "sortArray" function and displaying the sorted array.
8. Modify the program to prompt the user to enter the values for the array and then display the
array before and after each manipulation.
9. Add error checking to ensure that the user enters valid input for the array size and values.

page 2 of 2

You might also like