Homework help

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
using namespace::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 = new int;
	//  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

}





closed account (D80DSL3A)
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?
closed account (D80DSL3A)
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.
Last edited on
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 
closed account (D80DSL3A)
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;
Last edited on
Topic archived. No new replies allowed.