Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Programming Assignment For this lab, there are two parts. The first part deal

ID: 3796540 • Letter: 1

Question

1. Programming Assignment

For this lab, there are two parts. The first part deals with pass-by-reference or call-by-reference. The second part deals with pointers.

1.1.    Pass-by-reference Assignment

For this part of the lab (lab3a), we are going to pretend that we are on a text-based adventure game. Our life will be measured via hit points and we start with 10 hit points. If we want to increase our hit points, we can quaff a potion using the command quaff potion and it will increase our hit points by 2.

·        Ask the user what they would like to do. Have the user enter quaff potion using two words. As a reminder, you will have to use getline because it is two words. You can use a string or a c-string.

·       Using pass-by-reference, create a void function called quaff that adds two hit points.

·       In main, display the original hit points (10) and new hit points after quaffing the potion (12)

Here is an example execution of the program:

--bash-4.1$ ./lab3a

You have 10 hit points.

What would you like to do?:

quaff potion

You have quaffed the potion!

You have 12 hit points.

-bash-4.1$

1.2.    Pointer Assignment

For this part of the lab (lab3b), these are the requirements:

·       Declare a normal integer variable called numIcedTea and initialize it to 10

·       Declare an integer pointer called ptrIcedTea

·       Set the integer pointer to point at the numIcedTea memory address

·       Print the variable value of ptrIcedTea

·       Print the dereferenced value of ptrIcedTea

·       Using ptrIcedTea update the value to 20

·       Print the updated value of numIcedTea

·       Print the dereferenced value of ptrIcedTea

Here is an example of the code running.

-bash-4.1$ ./lab3b

ptrIcedTea = 0x7ffec030d894

*ptrIcedTea = 10

numIcedTea = 20

*ptrIcedTea = 20

-bash-4.1$

--bash-4.1$ ./lab3a

You have 10 hit points.

What would you like to do?:

quaff potion

You have quaffed the potion!

You have 12 hit points.

-bash-4.1$

Explanation / Answer

1) Answer

#include<iostream>
#include<string>
using namespace std;
//Function quaff accepts a parameter of type reference
void quff(int &point)
{
//Adds 2 point to the existing point value
point += 2;
}//End of function

//Main function
int main()
{
//String to store user option
string option;
//Initial point value is 10
int point = 10;
//Loops till user choice
do
{
cout<<" You have "<<point<<" hit points ";
cout<<" What would you like to do?: ";
//Accepts user choice
std::getline (std::cin, option);
//Compares user choice if it is quaff potion then call the quaff function
if(option.compare("quaff potion") == 0)
quff(point);
//Otherwise stop
else
break;
}while(1);
}//End of main method

Output:

You have 10 hit points
What would you like to do?: quaff potion

You have 12 hit points
What would you like to do?: quaff potion

You have 14 hit points
What would you like to do?: not

2) Answer

#include<iostream>
using namespace std;
int main()
{
//Declares an integer variable and assigns 10 to it
int numIcedTea = 10;
//Declares an integer pointer
int *ptrIcedTea;
//Pointer is pointing to integer variable
ptrIcedTea = & numIcedTea;
//Displays the contents of pointer variable
cout<<" ptrIcedTea = "<<ptrIcedTea;
//Displays the value at the address location stored in the pointer (dereferenced)
cout<<" *ptrIcedTea = "<<*ptrIcedTea;
//Assigns 20 using pointer variable
*ptrIcedTea = 20;
//Displays the variable value
cout<<" numIcedTea = "<<numIcedTea;
//Displays the pointer value (Dereferenced)
cout<<" *ptrIcedTea = "<<*ptrIcedTea;
}

Output:

ptrIcedTea = 0x29fef8
*ptrIcedTea = 10
numIcedTea = 20
*ptrIcedTea = 20