Question

a) Hand-trace the following program and determine and write down what is the output of the...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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];

Add a comment
Know the answer?
Add Answer to:
a) Hand-trace the following program and determine and write down what is the output of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that works with two arrays of the same size that are related to...

    Write a program that works with two arrays of the same size that are related to each other in some way (or parallel arrays). Your two arrays must be of different data types. For example, one array can hold values that are used in a formula that produces the contents of the second array. Some examples might be:  from a previous program, populations and the associated flowrates for those populations (an int array of populations and a double array...

  • How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using...

    How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using namespace std; int main() {    // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic    // tic tac toe board is an array of int pointers    // each int pointer in the board points to a row    // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int)    const...

  • Consider the following C++code snippet and what is the output of this program? # include<iostream> using...

    Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...

  • C++ output 6) What is the exact output of the following program? #include <iostream> using namespace...

    C++ output 6) What is the exact output of the following program? #include <iostream> using namespace stdi void zolo(int &a, int sb) int main int x = 5, y =8; zolo(x,y)i cout << "x " << x << endl ; cout << "y = "" << y << endl ; return o: void zolo(int &a, int &b) int v1 = b + a; ' int v2 = b-a; 3 a=v1;13

  • Example (4) Trace the following program and find the output >> SOURCE CODE #include <iostream.h> int...

    Example (4) Trace the following program and find the output >> SOURCE CODE #include <iostream.h> int main0 // define two integers int x-3; int y = 4; //print out a message telling which is bigger if (x >y) i cout << "x is bigger than y" << endl: else cout << "x is smaller than y" << endl; return 0; Example (5) Write a C++ program that takes from the user a number in SR (Saudi Riyal) then the program...

  • What does the following program print? I // Exercise 3.12 Solution: ex03_12.cpp // What does this...

    What does the following program print? I // Exercise 3.12 Solution: ex03_12.cpp // What does this program print? #include <iostream> using namespace std; 3 4 7 int main() { int y; // declare y int x = 1; // initialize x int total = 0; // initialize total while ( 10 ) // loop 10 times y = X* X; // perform calculation cout << y << endl; // output result total += y; // add y to total ++x;...

  • please help with the operator overloading lab (intArray) in c++ will provide what it is being...

    please help with the operator overloading lab (intArray) in c++ will provide what it is being required and the code that was given from the book. the code that was provided is below ------------------------------------------------------------------------------------------------------------------------- // iadrv.h #ifndef _IADRV_H #define _IADRV_H #include "intarray.h" int main(); void test1(); void test2(); void test3(); void test4(); void test5(); void test6(); void test7(); void test8(); void test9(); void test10(); void test11(); void test12(); void test13(); void test14(); void test15(); void test16(); void test17(); void test18();...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • can you show me the details please. thank you 13. What is the output of the...

    can you show me the details please. thank you 13. What is the output of the following program? #include <iostream.h> void main() { int x; 1/ declare variable for (x = 1; x <= 5; x++) { // loop 5 times switch (x) { case 1: cout << x < // switch value of // if "x" is equal // display "X", but endl; case 2: cout << x << endl; // if "x" is equal // display "x", but...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT