So I am not asking for you to code for me, I understanding C++ very well. I am asking if anyone can decipher what exactly my instructor is asking for? Because I can't.
#include <iostream>
usingnamespace::std;
// In C++ there are 4 ways a function can return an answer to the caller
// All of our functions will calculate the answer as 1 added to the first parameter
// 1. return the answer on the stack
// NOTE: the caller can save the returned answer by using the = operator
int f1(int parm);
// 2. place the answer in the 2nd parameter
void f2(int parm, int & answer); // Why the & ?
// 3. place the answer in the memory location pointed to by the 2nd parameter
// NOTE: the 2nd parameter is for the address of the answer,
// not the answer itself
// NOTE: the caller acquires the memory for the answer
void f3(int parm, int * pointerToAnswer);
// 4. return the address of the answer on the stack
// NOTE: the function must acquire the memory for the answer
int * f4(int parm);
void main()
{
// investigate f1
cout << f1(15) << endl; // printed value is 16
// NOTE: value parameters can have constants
int ans;
// call f1 and save the answer in ans and print ans
// STUDENT WRITES THE CODE
// function chaining - a parameter will be a function call
// sqrt(sqrt(16.0)) returns an answer of 2.0
// using f1 multiple times and the constant 22 have the value of 25 printed
// STUDENT WRITES THE CODE
// investigate f2
// declare an integer, call f2 using a first parameter of 35 and print the answer
// STUDENT WRITES THE CODE
// investigate f3
// PART A - work with integers
int theAns;
// call f3 using a first parameter of 42 and have the answer placed in theAns
// then print theAns
// STUDENT WRITES THE CODE
// PART B - work with pointers
int * pointer;
// acquire at run time the memory for an integer
pointer = newint;
// call f3 using a first parameter of 117 and have the answer placed in the
// memory pointed to by pointer
// the print the answer
// STUDENT WRITES THE CODE
// return the memory
delete pointer;
// investigate f4
// PART A
// without saving the return value call f4 with the first parameter 234
// the value of 235 should be printed
// STUDENT WRITES THE CODE
// PART B
// declare a pointer variable, call f4 with the first parameter 319
// and save the return value in the pointer variable
// then print the answer -- the value 320 should be printed
// STUDENT WRITES THE CODE
// release the memory that the function acquired
// STUDENT WRITES THE CODE
}
// write the functions
// STUDENT WRITE THE CODE
int f1(int parm)
{
return 1; // DUMMY CODE TO GET TO COMPILE
}
void f2(int parm, int & answer)
{
// DUMMY CODE TO GET TO COMPILE
}
void f3(int parm, int * pointerToAnswer)
{
// DUMMY CODE TO GET TO COMPILE
}
int * f4(int parm)
{
return &parm; // DUMMY CODE - DO NOT USE!!!! only to get a compile
}
I think I understand all parts of the assignment. Which parts confuse you? Please be specific.
Pick one part so we can discuss it. Each task is fairly separate from the rest, so you should be able to tackle each part as a single task.
I'm at a loss as to how f3 function is supposed to work. The main function, the instructor has provided a regular int, and we are supposed to pass it into f3, but it needs a pointer?
It seems you're referring to part A. Use &theAns as 2nd argument for f3. In this way you're providing the memory address for pointerToAnswer to write the answer to.
You must write the definition of f3, but this is one line of code.
I don't know how this is supposed to work. It's asking me to pass in an int of 42 through the parameter parm, and a regular int as the second parameter? Aren't I only allowed to pass in a pointer int?
1 2 3 4
void f3(int parm, int * pointerToAnswer);
// 4. return the address of the answer on the stack
// NOTE: the function must acquire the memory for the answer
Sorry. I thought I explained that. From my last post: Use &theAns as 2nd argument for f3
ie. call: f3( 42, &theAns );// passing the address of the variable you want the pointer to write to
Since the pointer is being passed by value, you need only supply an address value, such as &theAns.
How are you with the code in the function body? Dereference pointerToAnswer and assign the value parm+1 to it.
ie: * pointerToAnswer = parm + 1;