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.
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 | |...
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 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...
Please do this in C++ only. Please Show your code and your output too. Would you...
Please do this in C++ only. Please Show your code and your output too. Would you focus on number 2 in the Required functions:? Please use all the variables in the program. The assignment problem: You are asked to develop an application that prints a bank’s customers' names, IDs, and accounts balances in different ways. In this program, you need to read a file of customer’s data into different arrays, pass these arrays to functions as (array parameter), calculate the...
Please find implementation of KNN algorithm (in C++) below Please explain how each code implements the...
Please find implementation of KNN algorithm (in C++) below Please explain how each code implements the following steps of the algorithm in question: 1. Determine parameter K = number of nearest neighbors 2. Calculate the distance between the query-instance and all the training samples 3. Sort the distance and determine nearest neighbors based on the K-th minimumdistance 4. Gather the category Y of the nearest neighbors 5. Use simple majority of the category of nearest neighbors as the prediction value...
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1....
C++ CODE PLEASE MAKE SURE TO USE STRINGSTREAM AND THAT THE OUTPUT NUMBER ARE CORRECT 1. You must call all of the defined functions above in your code. You may not change function names, parameters, or return types. 2. You must use a switch statement to check the user's menu option choice. 3. You may create additional functions in addition to the required functions listed above if you would like. 4. If user provides option which is not a choice...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT