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...

  • 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...

  • Using a programming language of your choice, write a complete and fully functional program that uses...

    Using a programming language of your choice, write a complete and fully functional program that uses reference and pointer types to swap two integer numbers. The two numbers are read in separately by the program’s user. Use a proper prompt for each number read in. a. Use one function that uses formal parameter reference type to swap the two numbers b. Use another function that uses formal parameter pointer type to swap the two numbers. c. In the main or...

  • 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...

  • Given the following program, trace the output by drawing the memory model as the program runs....

    Given the following program, trace the output by drawing the memory model as the program runs. Assuming each cell is one byte memory. #include <iostream> using namespace std; int main( ) { int *p; int val[3] = {1, 2, 3}; // array p = &val[0]; cout << p << " " << &val[0] << endl; for ( int i = 0 ; i <3 ; i++ ) { cout << "val[" << i << "]: value is " << *(p+i)...

  • 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

  • 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();...

  • 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...

  • 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()...

  • 4. Perform a hand trace by producing a table of values for each of the variables...

    4. Perform a hand trace by producing a table of values for each of the variables in the following program. Similar to what you did in one of your labs. Note: Any computer generated output will be awarded a grade of zero. {4 points) #include <iostream> using namespace std; int main() { int low high; low = 1; high = 20; while (low < high) | cout << low << " " << high << endl; low = low +...

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