a) Hand-trace the following program and determine and write down what is the out
ID: 3880788 • Letter: A
Question
a) Hand-trace the following program and determine and write down what is the output of the code.
b) Run the code and compare the program output to your hand-traced result obtained from part (a).
#include <iostream>
#include <iomanip>
using namespace std;
void f();
int x = 5;
int main()
{
cout << "x = " << x << endl;
int x = 6;
cout << "x = " << x << endl;
{
int x = 7;
cout << "x = " << x << endl;
}
cout << "x = " << x << endl;
f();
return 0;
}
void f()
{
cout << "x = " << x << endl;
int x = 8;
cout << "x = " << x << endl;
}
About arrays, you need to know the following in order to complete the programs:
· How to declare 1D and 2D arrays
· How to declare and initialize 1D and 2D arrays at the same time
· How to load array elements with values entered by user
· How to perform operations on array elements such as moving elements around (swapping)
· How to perform computations with arrays such as summing up elements
· How to perform search and sort with arrays – two of the many important applications of arrays
· How to pass an array to a function
· Arrays, pointers, and pointer arithmetic
Explanation / Answer
Variable x is declared and initialised to 5 at line number 6 is a global variable.
Scope of global variable is through out the file.
Hence cout on x variable on line number 10 which is the first statement of main function refers to global variable X.
Hence output of first cout on x is 5.
Next local variable x is declared at line number 12 and initialised to 6.
The next cout on x on line number 13 gives the output as 6,
because now x refers to local variable declared at line 12.
At line 15, variable x is again declared and initialised to 7 but with a scope that is within angular braces'{' from line number 14 to 17.
Cout on variable x at line 16, whose scope is between line numbers 14 to 17. It is for the variable x that is declared at line 15.
Hence cout on x at line number 16 gives output as 7.
Next cout on variable x on line number 18 refers to the variable x that is declared at line 12.
Hence cout on x gives output as 6.
At line 19 function f is called. Inside function f, first cout on variable X, refers to global variable.
Hence gives output is 5.
In next line variable X is again declared and initialised to 8 as local variable.
Next cout inside this function on variable X gives output as 8, as it refers to this function(own) local variable.
Hand traced and output of the program are same.
A.Declaring 1D and 2D arrays:
int a[10], b[10][5];
a[10] refers to 1D, where 10 denotes the number of elements available in the array a.
b[10][5] refers to 2D, where 10 denotes the number of rows and 5 denotes number of columns in array b.
B.Declare and initialise at same time
1D
int a[]={10,20,30};
2D
int b[]2]={10,20,30,40};
C.Load elements in array with values entered by user:
Using for loop it can be loaded
for(int i =0;i <10;i ++)
cin>>a[i];
D.For summing same for loop can be used
Sum=0;
for(int i =0;i <10;i ++)
sum+=a[i];
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.