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

  1. Understand Pointers: Learn how pointers work in C++ by manipulating data through them.

  2. Implement Pointer Functions: Write functions that perform operations using pointers.

  3. Test Functions: Use a main program to test your pointer functions.

Tasks

  1. Create Function Declarations (myFunctions.hpp):

    • Define two functions: swap(int* ptrOne, int* ptrTwo) and square(int* ptrNum).

    • Include necessary header files and use appropriate namespaces.

  2. Implement Functions (myFunctions.cpp):

    • Implement the swap function to swap the values pointed to by ptrOne and ptrTwo.

    • Implement the square function to square the value pointed to by ptrNum.

    • Add print statements in each function to display the values before and after the operations for verification.

  3. 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:

  1. Full source code (.cpp files or .txt files)

  2. Screenshot of the Console with the code executing

2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark