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 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;...
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...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
Please code in C language. Server program: The server program provides a search to check for...
Please code in C language. Server program: The server program provides a search to check for a specific value in an integer array. The client writes a struct containing 3 elements: an integer value to search for, the size of an integer array which is 10 or less, and an integer array to the server through a FIFO. The server reads the struct from the FIFO and checks the array for the search value. If the search value appears in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT