Variable Diagrams

Draw variable diagrams for the following program with input values 2 (for variable first)and 3 (for variable second).
For instance:
•A ? shows an uninitialized value for a variable.
•The notation 25 →5 means that execution jumps from line 25 to line 5.
•We use square brackets [ ] around the name of a variable to show that it is inaccessible while the current function is being executed.

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
#include <iostream>
 using namespace std;

 void multiplyBy2(int firstP, int secondP)
 {
 firstP = firstP * 2;
  secondP = secondP * 2;
 }

 void multiplyBy3(int & firstP, int & secondP)
 {

 firstP = firstP * 3;
 secondP = secondP * 3;
 }

 int main()
 {
 int first, second;
 cout << "Enter the first number: "<< endl;
 cin >> first;
 cout << "Enter the second number:" << endl;
 cin >> second;
 multiplyBy2(first, second);
 multiplyBy3(first, second);
 cout << "The first number is "<< first << " now." << endl;
 cout << "The second number is " << second << " now." << endl;

 return 0;
 }


Variable diagrams is what code is being executed each line right?

So something like this? :

Line 5 - firstP = 2 *2 , secondP = 3 * 2
Line 13 - firstP = 2 * 3 , secondP =3 * 3
Line 21 - first = 2
Line 23 - second = 3
Line 24 - first = 4 , second = 6
Line 25 - first = 6 , second = 9
Last edited on
Shouldn't it start with:

L19 first ? second ?
L21 first = 2 second ?
L23 first = 2 second = 3
L24 -> L4
L4 firstP = 2 secondP = 3 [first] [second]
...
Last edited on
Topic archived. No new replies allowed.