Question

In: Computer Science

Trace through the program and what the outputs will be. If there is an error explain...

Trace through the program and what the outputs will be. If there is an error explain what must be fixed and continue to trace through.

#include <iostream>
using namespace std;
void beans(int y, int& n, int size);
void spam(int& n, int& y);
int main(){
    int m = 7;
    int n = 4;
    cout<<"m is "<<m<<" n is "<<n<<endl;
    beans(n, m, 3);
    cout<<"m is "<<m<<" n is "<<n<<endl;
    spam(m, n);
    cout<<"m is "<<m<<" n is "<<n<<endl;
    beans(m, 2, n);
    cout<<"m is "<<m<<" n is "<<n<<endl;
    return 0;
}

void beans(int y, int& n, int size){
    y = n+size;
    n = 7+n;
}
void spam(int& n, int& y){
    int m = n+y;
    y=n;
    n=y+n;
}

Solutions

Expert Solution

When the function accepts a value using '&' , it is pointing to the address of variable whose value is passed. So changes will be directly reflected.

Therefore different value of m and n printed are

O/P value of m value of n
1 7 4
2 2i 14
3 28 14

If you have any questions comment down. Please don't simply downvote and leave. If you are satisfied with answer, please? upvote thanks


Related Solutions

trace through the program and what the output would be. If there is an error explain...
trace through the program and what the output would be. If there is an error explain what to change. #include <iostream> using namespace std; int fun(int c, int b); int main(){ int a = 0, b= 5, c = 10; cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; b=fun(a, c); cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; while(b==21){ int a = 3; b = a; cout<<"a is: "<<a<<" b is: "<<b<<" c is: "<<c<<endl; } cout<<"a is:...
Can someone please walk through this program for me and explain out the outputs are generated....
Can someone please walk through this program for me and explain out the outputs are generated. #include <iostream> #include <iomanip> using namespace std; const int size = 5; int fun(int arz[size], int jump) { int i, j = 0; for(i = 0; i < size; i += jump) j += arz[i]; return j; } main() { int arx[size] = {1, 2, 4, 8, 16}; int ary[size] = {10, 20, 40, 80, 160}; cout << fun(arx, 1) << endl; cout <<...
Trace the pathway of light through the eye to the retina, and explain how light is...
Trace the pathway of light through the eye to the retina, and explain how light is focused for distant and close vision.
What are the outputs of the accounting cycle? Why are they important? Identify and explain the...
What are the outputs of the accounting cycle? Why are they important? Identify and explain the purpose of each financial statement, including how they are interrelated.
a program that takes an integer and outputs the resulting factorial number.
assembly x86 language program a program that takes an integer and outputs the resulting factorial number. 
Trace the path of a drop of blood through the body of a shark by making...
Trace the path of a drop of blood through the body of a shark by making a sequential list of named vessels, heart chambers, and organs. Begin in the anterior intestinal vein on the outside of the small intestine and end with the internal carotid artery leading to the brain.
In words, trace blood flow through the systemic and pulmonary circuits.
In words, trace blood flow through the systemic and pulmonary circuits.
trace a molecule of urea from the aorta to the kidney through the structures of the...
trace a molecule of urea from the aorta to the kidney through the structures of the urinary system, explaining their roles.
For each of the following problems provide: a. inputs b. outputs c. error conditions d. an...
For each of the following problems provide: a. inputs b. outputs c. error conditions d. an algorithm using the pseudocode reference used in class e. a set of test cases. Be careful about corner cases f. the minimum number of operations executed in your algorithm g. the maximum number of operations executed in your algorithm Q. Train Ticket for one person. Read a person’s age, then compute and display the price the person needs to pay for the train ride...
1.       What is Logic Error ? (a)    Explain the following (i)                  Post HOC Error (ii)   &nbsp
1.       What is Logic Error ? (a)    Explain the following (i)                  Post HOC Error (ii)                Fallacy of composition (iii)               Fallacy of Division 2.       Explain the meaning of (i)                  Savings (ii)                Investment 3.       What the meaning of Production Possibilities and Production Frontier 4.       Write on the following (i)                  Opportunity Cost (ii)                Explicit cost (iii)               Implicit cost (iv)               Resource Available (v)                Technology Available (vi)               100% Utilization available 5.       What are the qualities of Economic Society and explain them in detail. (a)    What...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT