aboutsummaryrefslogtreecommitdiff
path: root/CST116-Ch9-Debugging/CST116-Ch9-Debugging-Florea.cpp
blob: 52afd5825a268e9766832c3a46a018978f076a2e (plain) (blame)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/********************************************************************
 * Andrei Florea - CST 116 - Chapter 9 - Debugging & Functions
 *
 * Chapter 9 Questions & Answers (it didn't specify which questions, so I'm assuming all?)
 *  How does a computer know where to go back to?
 *      - It knows because of a parameter that is called the return address, which lets the computer know
 *      where to go back to after executing the function.
 *
 *  How does it maintain the state of all variables when a function is called?
 *      - The state of all variables is saved before a function call, and when creating a function,
 *      you can decide whether you want to pass the parameters as reference ( reference to the memory location )
 *      or passing the value of the variable which both can alter the original values, but they differ on how.
 *      Passing by reference, any manipulation inside the function to the given parameter variables will alter it,
 *      but if its passed by value, it has to return the variable(s), as it creates a copy of the original variables.
 *
 *
* File: CST116-Ch9-Debugging.cpp
*
* General Instructions: Complete each step before proceeding to the
* next.
*
* Debugging Exercise 1
*
* 1) Insert a breakpoint on the lines indicated in the code.
* 2) Run to Breakpoint 1.
* 3) Place a watch on age and days.
* 4) Add another watch using &age for the name. This will display
*    the address of age.
* 5) Write down the address of age.
 *
 * 0x000000016dd67838
 *
* 6) Step Into the code for the function GetAge.
* 7) The execution continues to the function header for GetAge.
* 8) Step into one more time.
* 9) Why did the address of age and value change?
 *
 * The address of age and its value changed because we entered a function, where it initialized
 * a new variable with the same name, and gave it a different value. It is not overwriting the
 * original age variable, it is instead creating a new variable.
 *
* 10) Step over the cout and cin statements.
* 11) Verify the value entered is stored properly in age.
* 12) Step into until the flow returns to main.
* 13) Step over one more time.
* 14) Why didn't the value entered get transferred back to main?
 *
 * The value entered didn't get transferred back to main because we are not
 * storing the return value of GetAge() to the variable age.
 *
* 15) Stop debugging and fix the error.
* 16) Run to Breakpoint 1.
* 17) Step over the function call to GetAge.
* 18) Verify that the value entered was returned and stored
*     correctly from GetAge.
* 19) Stop debugging.
 *
 * Done.
*
* Debugging Exercise 2
*
* 1) Run to Breakpoint 1.
* 2) Step over the call to GetAge.
* 3) Step into CalcDays.
* 4) Step into one more time so that the current line is the
*    calculation.
* 5) Why is age greyed out in your watch window?
 *
 *  I believe age is greyed out in my watch window because we are not using the variable age to
 *  calculate the amount of days. It is now using the newly initialized variable in the parameter of
 *  CalcDays() which is "int years". So instead of age, I have years in my watch window.
 *
* 6) Stop debugging.
*
* Debugging Exercise 3
*
* 1) Run to Breakpoint 2.
* 2) When asked, enter the value of 20 for your age.
* 3) Verify that the variable age is 20 and the variable days
*    is 7300.
 *
 *    Verified.
 *
* 4) Step into the PrintResults function.
* 5) Age is 7300? Not even Ralph is that old.
* 6) Why did the values for both variables change?
 *
 * The values changed because of the function call with the argument order. The function is defined
 * with the parameters in the order of (int days, int age). While the function call is calling
 * with the arguments of the variables in the order of (age, days).
 *
* 7) Stop debugging and fix the error.
*
* Debugging Exercise 4
*
* 1) Run to Breakpoint 2.
* 2) Display your Call Stack window.
* 3) View the contents of the window and notice that the top
*    function on the stack is main.
* 4) Step into the PrintResults function.
* 5) Notice that the call stack now shows PrintResults on top of
*    the stack.
 *
 *    Done.
 *
********************************************************************/
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

const int DAYS_PER_YEAR = 365;

int GetAge();
int CalcDays(int age);
void PrintResults(int age, int days);

int main()
{
    int age = 0;
    int days = 0;

    // Breakpoint 1
    // Put breakpoint on the following line 
    age = GetAge();
    days = CalcDays(age);

    // Breakpoint 2
    // Put breakpoint on the following line
    PrintResults(days, age);

    return 0;
}
int GetAge()
{
    int age;

    cout << "Please enter your age: ";
    cin >> age;

    return age;
}
int CalcDays(int years)
{
    int days;

    days = years * DAYS_PER_YEAR;

    return days;
}
void PrintResults(int days, int age)
{
    cout << age << "! Boy are you old!\n";
    cout << "Did you know that you are at least " << days << " days old?\n\n";
}