Question

Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1,...

Explain the output of the following C++ program.

#include <iostream>
using namespace std;

void Magic(int i=1, int j=2,int k=3, double product =1.0)
{
i+=2;
j*=2;
k/=2;
product=i*j*k;
}

void Magic(int& i, int& j, double& product)
{
i+=2;
j=j*2+2;
product=i*j;
}

void Magic(int* i,int* j)
{
double product;
*i+=2;
*j=*j*2+2;
product=*i * *j;
}

int main()
{
double product;
int i=0,j=0,k=0;
product=i*j*k;
  
Magic();
cout<<"i, j, k and product in main () after 1st round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;
  
Magic(2,4);
cout<<"i, j, k and product in main () after 2nd round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;
  
Magic(i,j);
cout<<"i, j, k and product in main () after 3rd round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;
  
Magic(&i,&j);
cout<<"i, j, k and product in main () after 4th round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;
  
Magic(2,4,6);
cout<<"i, j, k and product in main () after 5th round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;

Magic(i,j,product);
cout<<"i, j, k and product in main () after 6th round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;
}
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please check the comments with code for code explanation

#include <iostream>
using namespace std;

void Magic(int i=1, int j=2,int k=3, double product =1.0) // default arguments used when no arguments given, arguments are values
{
i+=2; // no value returned from this function as arguments are value types
j*=2;
k/=2;
product=i*j*k;
}

void Magic(int& i, int& j, double& product) // reference parameters are used, so values are returned from this function
{
i+=2; // i = 2+2 = 4
j=j*2+2; // j = 6
product=i*j; // product = 4*6 = 24
}

void Magic(int* i,int* j) // pointers are used as arguments, so values of i and j are returned from function
{
double product;
*i+=2; // i = 2
*j=*j*2+2; // j = 2
product=*i * *j; // product = 4, product is local variable, so its value is not returned
}

int main()
{
double product;
int i=0,j=0,k=0;
product=i*j*k;
  
Magic(); // i=j=k=p = 0, so void Magic(int i=1, int j=2,int k=3, double product =1.0) is called
cout<<"i, j, k and product in main () after 1st round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;
  
Magic(2,4); // i =2, j = 4, so void Magic(int i=1, int j=2,int k=3, double product =1.0) is called
cout<<"i, j, k and product in main () after 2nd round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;
  
Magic(i,j); //void Magic(int i=1, int j=2,int k=3, double product =1.0) is called, i =j=k=product = 0
cout<<"i, j, k and product in main () after 3rd round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;
  
Magic(&i,&j); // pointers are used so void Magic(int* i,int* j) is called , i = 2, j = 2 is returned, k and product = 0
cout<<"i, j, k and product in main () after 4th round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;
  
// i = 2, j = 2 at this line
  
Magic(2,4,6);// void Magic(int i=1, int j=2,int k=3, double product =1.0) is called , so i = 2 and j = 2 and k=product = 0
cout<<"i, j, k and product in main () after 5th round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;

Magic(i,j,product);// void Magic(int& i, int& j, double& product) is called with i = 2, j = 2, product = 0 and return value , i = 4, j = 6 and product = 24, k = 0
cout<<"i, j, k and product in main () after 6th round:"<<endl<<i<<endl<<j<<endl<<k<<endl<<product<<endl;
}
  

Do ask if any doubt.

Add a comment
Know the answer?
Add Answer to:
Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT