Question

In: Computer Science

C++ program. Please explain how the code resulted in the output. The code and output is...

C++ program. Please explain how the code resulted in the output. The code and output is listed below.

Code:

#include <iostream>
#include <string>

using namespace std;

int f(int& a, int b) {
   int tmp = a;
   a = b;

   if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }

   b = tmp;
   return b;
   return a;
}

int main() {
   int a = 0, b = 1, c = 8;

   f(b, c);
   cout << a << ' ' << b << ' ' << c << endl;

   a = f(a, b);
   cout << a << ' ' << b << ' ' << c << endl;

   return 0;
}

Output:

0 8 8

0 8 8

0 8 8

Solutions

Expert Solution

int &a is the reference variable, which means its an another variable for an already existing variable.

Function Main

int f(int& a, int b)

{
6. int tmp = a;
7. a = b;

8. if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }

9. b = tmp;
10. return b;
11. return a;
}

int main()

{
1. int a = 0, b = 1, c = 8;

2. f(b, c);
3. cout << a << ' ' << b << ' ' << c << endl;

4. a = f(a, b);
5. cout << a << ' ' << b << ' ' << c << endl;

   return 0;
}

variable a = 0, b = 1, c = 8

line 2: is calling function f(b,c)

line 6: tmp = 1 (a value is 1)

line 7 : a=8 (b is having 8 value) note : a is reference variable which is referencing b in main, so a value reflect in b.

line 8 : will not execute..(if condition is false)

line 9: b=1 (tmp is having 1 value)

line 10: return

line 3: print a , b , c [a is having 0, b is having 8(because of line 7) and c is having 8]

line 4: is calling a = f(a, b)

line 6: tmp = 0 (a value is 0)

line 7 : a=8 (b is having 8 value) note : a is reference variable which is referencing 'a' in main, so 'a' value reflect in 'a' of main.

line 8 : print tmp , a , b [tmp is having 0, a is having 8 and b is having 8 ](if condition is true)

line 9: b=0 (tmp is having 0 value)

line 10: return b

line 4: a = 0 (function returned 0 )

line 5: print a , b , c [a is having 0, b is having 8(updated by previous function call) and c is having 8]


Related Solutions

Please code the program showing the output below. using C language 1. Using 'if' or 'while'...
Please code the program showing the output below. using C language 1. Using 'if' or 'while' or 'for' and 'break' statement / only using <stdio.h> A bC dEf GhIj KlMnO 2. a program that identifies the largest number that can be expressed in short. Using only loop (ex.for,if,while) and break statement only using <stdio.h>
please submit the C code( no third party library). the C code will create output to...
please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.
Write Algoritm , code and output. In C++ In Operating Systems , Simulate with a program...
Write Algoritm , code and output. In C++ In Operating Systems , Simulate with a program to schedule disk in seek optimization.
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
Explain the errors in the C code that cause the output NOT to be -10. (Hint:...
Explain the errors in the C code that cause the output NOT to be -10. (Hint: There are 2 errors): #include <stdio.h> #include <stdlib.h> #include <pthread.h>    #define SIZE 5 void *threadFunc(void *arg); int *changingVal; int a[SIZE]; // Assume a[] = { 13444, 3320, 31020, 3302, 31313 }; int main() { int i; int min; int * changeVal = malloc (sizeof(int*)); *changeVal = -10; // Thread creation pthread_t thread1; pthread_attr_t attr; pthread_attr_init(&attr);    for (i = 0; i < SIZE;...
C++ code Output Formatting. Design and implement a complete C++ program that reads a customer record...
C++ code Output Formatting. Design and implement a complete C++ program that reads a customer record from the user such as the record has 4 fields (pieces of information) as shown below: Account Number (integer) Customer full name (string) Customer email (string) Account Balance (double) The program is expected to print the record in the format shown below: Account Number :  0000000 Name                     :  full name Email                      :  customer email Balance                  :  000000.00$ For example, if the user enters the following record 1201077 Jonathan I. Maletic [email protected]...
provide a C code (only C please) that gives the output below: ************************************ *         Menu HW...
provide a C code (only C please) that gives the output below: ************************************ *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1. Creating polynomials * * 2. Adding polynomials * * 3. Multiplying polynomials. * * 4. Displaying polynomials * * 5. Clearing polynomials. * * 6. Quit. * *********************************** Select the option (1 through 6): 7 You should not be in this class! ************************************* *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1. Creating polynomials...
This question is about java program. Please show the output and the detail code and comment...
This question is about java program. Please show the output and the detail code and comment of the each question and each Class and interface. And the output (the test mthod )must be all the true! Thank you! Question1 Create a class Animal with the following UML specification: +-----------------------+ | Animal | +-----------------------+ | - name: String | +-----------------------+ | + Animal(String name) | | + getName(): String | | + getLegs(): int | | + canFly(): boolean | |...
Please code in C# - (C - Sharp) Assignment Description Write out a program that will...
Please code in C# - (C - Sharp) Assignment Description Write out a program that will ask the user for their name; the length and width of a rectangle; and the length of a square. The program will then output the input name; the area and perimeter of a rectangle with the dimensions they input; and the area and perimeter of a square with the length they input. Tasks The program needs to contain the following A comment header containing...
Please code in c# (C-Sharp) Write a program that will ask the user for their name....
Please code in c# (C-Sharp) Write a program that will ask the user for their name. If the user does not input anything, display a warning before continuing. The program will then ask the user whether they want to have an addition, subtraction, multiplication, or division problem. Once the user indicates their choice, the program will display 2 randomly generated numbers from 1 to 9 in a math problem matching the user’s choice. Example: user selects addition, the equation presented...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT