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.
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
write a program on c++ that outputs a calendar for a given month in a given...
write a program on c++ that outputs a calendar for a given month in a given year, given the day of the week on which the 1st of the month was. The information in numeric format (months are: 1=Januay, 2=February 3=March, 4= April, 5= May, 6= June, 7= July... etc days are 1=Sunday, 2=Monday, 3= Tuesday, 4= Wednesday, 5= Thursday, 6= Friday and 7= Saturday ). The program output that month’s calendar, followed by a sentence indicating on which day...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT