Showing Page:
1/5
The following program is an example of very poorly used
scope rules. The mixture of local variables, global
variables, pass-by-reference, pass-by-value, and repeated
variable names makes it difficult to predict what will be
printed. Enter the program, without line numbers, then
compile and run the program. Save the output to a file
using the script command. Edit the output file, by adding
text that explains for each line of the output where the
variable was declared and where it\'s value was assigned,
using the line numbers to help. For example, part of the
explanation of the first line of output would be: \"x was
declared on line 9 and it was assigned the value 0 on line
9\". Print and turn in your annotated output.
Solution
#include <iostream>
using namespace std;
int function1(int x,int &y);
int function2(int y,int &x);
int g=4;
int main()
{
int a=1;
int b=5;
Showing Page:
2/5
int x=0;
cout<<\"Main 1: a = \"<<a<< \" b = \"<<b;
cout<<\" g = \"<<g<< \" x = \"<<x;
cout<<endl<<endl<<endl<<endl<<endl;
{
int a=7;
int g=10;
cout<<\"Main 2: a = \"<<a<< \" b = \"<<b;
cout<<\" g = \"<<g<< \" x = \"<<x;
cout<<endl<<endl<<endl<<endl<<endl;
}
x=function1(a,b);
cout<<\"Main 3: a = \"<<a<< \" b = \"<<b;
cout<<\" g = \"<<g<< \" x = \"<<x;
cout<<endl<<endl<<endl<<endl<<endl;
x=function2(a,b);
cout<<\"Main 4: a = \"<<a<< \" b = \"<<b;
cout<<\" g = \"<<g<< \" x = \"<<x;
cout<<endl<<endl<<endl<<endl<<endl;
return 0;
}
int function1(int x,int &y)
{
int a;
a=x-y;
Showing Page:
3/5
g=x+y;
y=a+g;
cout<<\"Function1 a = \"<<a<< \" x = \"<<x;
cout<<\" g = \"<<g;
cout<<endl<<endl<<endl<<endl<<endl;
return a;
}
int function2(int b,int &x)
{
int temp;
temp=b;
b=x;
x=temp;
temp=function1(b,x);
g=temp*x;
return temp;
}
OUTPUT:
//a was declared on line 7 and it was assigned to 1
//b was declared on line 8 and it was assigned to 5
//g was declared on line 5 and it was assigned to 4
//x was declared on line 9 and it was assigned to 0
Main 1: a = 1 b = 5 g = 4 x = 0
//a was declared on line 14 and it was assigned to 7
//b was declared on line 8 and it was assigned to 5
Showing Page:
4/5
//g was declared on line 15 and it was assigned to 10
//x was declared on line 9 and it was assigned to 0
Main 2: a = 7 b = 5 g = 10 x = 0
//a was declared on line 31 and it was assigned to x-y -->
a-b(1-5) --> -4
//x is declared passed as formal argument and it was
assigned to a value(1)
//g was declared on line 5 and it was assigned to x+y -->
a+b(1+5) --> 6
Function1 a = -4 x = 1 g = 6
//a was declared on line 7 and it was assigned to 1
//b was declared on line 8 and it was modified by
function1 at line 34 6+(-4) --> 2
//g was declared on line 5 and it was assigned to x+y -->
a+b(1+5) --> 6
//x value was modified at line 20 and it was assigned to -4
which return value of function1 of a
Main 3: a = 1 b = 2 g = 6 x = -4
//function2 call with function2(1,2) and this calls
function1(2,1)
//a was declared on line 31 and it was assigned to x-y -->
a-b(2-1) --> 1
//x is declared passed as formal argument and it was
assigned to a value(2)
//g was declared on line 5 and it was assigned to x+y -->
Showing Page:
5/5
a+b(1+2) --> 3
Function1 a = 1 x = 2 g = 3
//a was declared on line 7 and it was assigned to 1
//b was declared on line 8 and it was modified by
function1 at line 34 as 1+3 --> 4
//g was declared on line 5 and it was assigned to temp*x -
-> 2*2 --> 4
//x value was modified at line 24 and it was assigned to 1
which return value of function2 of temp
Main 4: a = 1 b = 4 g = 4 x = 1

Unformatted Attachment Preview

The following program is an example of very poorly used scope rules. The mixture of local variables, global variables, pass-by-reference, pass-by-value, and repeated variable names makes it difficult to predict what will be printed. Enter the program, without line numbers, then compile and run the program. Save the output to a file using the script command. Edit the output file, by adding text that explains for each line of the output where the variable was declared and where it \'s value was assigned, using the line numbers to help. For example, part of the explanation of the first line of output would be: \"x was declared on line 9 and it was assigned the value 0 on line 9\". Print and turn in your annotated output. Solution #include using namespace std; int function1(int x,int &y); int function2(int y,int &x); int g=4; int main() { int a=1; int b=5; int x=0; cout<<\"Main 1: a = \"< a-b(1-5) --> -4 //x is declared passed as formal argument and it was assigned to a value(1) //g was declared on line 5 and it was assigned to x+y --> a+b(1+5) --> 6 Function1 a = -4 x = 1 g = 6 //a was declared on line 7 and it was assigned to 1 //b was declared on line 8 and it was modified by function1 at line 34 6+(-4) --> 2 //g was declared on line 5 and it was assigned to x+y --> a+b(1+5) --> 6 //x value was modified at line 20 and it was assigned to -4 which return value of function1 of a Main 3: a = 1 b = 2 g = 6 x = -4 //function2 call with function2(1,2) and this calls function1(2,1) //a was declared on line 31 and it was assigned to x -y --> a-b(2-1) --> 1 //x is declared passed as formal argument and it was assigned to a value(2) //g was declared on line 5 and it was assigned to x+y --> a+b(1+2) --> 3 Function1 a = 1 x = 2 g = 3 //a was declared on line 7 and it was assigned to 1 //b was declared on line 8 and it was modified by function1 at line 34 as 1+3 --> 4 //g was declared on line 5 and it was assigned to temp*x -> 2*2 --> 4 //x value was modified at line 24 and it was assigned to 1 which return value of function2 of temp Main 4: a = 1 b = 4 g = 4 x = 1 Name: Description: ...
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4