Lab 3 - Pointer Lab
Objective: Understanding Pointers through Function Implementations
Overview
In this lab, you will explore the concept of pointers in C++ by implementing functions that perform operations using pointers. Using pointer arguments, you will create functions to swap two numbers and square a number. The assignment involves three files: myFunctions.hpp
(or .h) for function declarations, myFunctions.cpp
for function definitions, and main.cpp
for testing these functions.
Lab Objectives
Understand Pointers: Learn how pointers work in C++ by manipulating data through them.
Implement Pointer Functions: Write functions that perform operations using pointers.
Test Functions: Use a main program to test your pointer functions.
Tasks
Create Function Declarations (
myFunctions.hpp
):Define two functions:
swap(int* ptrOne, int* ptrTwo)
andsquare(int* ptrNum)
.Include necessary header files and use appropriate namespaces.
Implement Functions (
myFunctions.cpp
):Implement the
swap
function to swap the values pointed to byptrOne
andptrTwo
.Implement the
square
function to square the value pointed to byptrNum
.Add print statements in each function to display the values before and after the operations for verification.
Test Your Functions (
main.cpp
):Create two integer variables and initialize them.
Call the
swap
function with pointers to these variables.Call the
square
function with a pointer to one of the variables.Observe the output to verify the correctness of your functions.
Requirements
Ensure that your
swap
function correctly swaps the values of the variables pointed to by the pointers.The
square
function should modify the value pointed to by its argument.Include appropriate output statements in your functions for easy verification.
Your main program should demonstrate the functionality of both functions clearly.
Comment your code to explain the logic and pointer operations.
Example Run:
Deliverable
Upload the following:
Full source code (.cpp files or .txt files)
Screenshot of the Console with the code executing
2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark