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;
}
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.
Explain the output of the following C++ program. #include <iostream> using namespace std; void Magic(int i=1,...
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
Consider the following C++ program: #include <iostream> using namespace std; void f1(int); void f2(int); void f3(int); int main() { f1(10); return 0; } void f1(int n) { f2(n + 5); } void f2(int n) { f3(n - 2); } void f3(int n) { cout << n << endl; // LINE 1 } Just before the program statement marked with the comment "LINE 1" is executed, how many stack frames will be on the program call stack?
C++ question #include <iostream> using namespace std; void printReverse(int); int main() { int number = 12345; cout << "Original : " << number << endl; cout << "Reversed : "; printReverse(number); } void printReverse(int x) { if (x == 0) return; cout << x % 10; printReverse(x /= 10); } Modify the above recursive program to output the number in the same order. Note that the program still should break up the number and then output it in the...
#include<iostream> using namespace std; void calcSumAndDiff(int ,int, int &, int &); void calcSumAndDiff(int n1,int n2, int &sum, int &diff){ sum = n1 + n2; diff = n1 - n2; } int main() { int n1,n2,sum,diff; n1=30;n2=10; calcSumAndDiff(n1,n2,sum,diff); cout<<"Sum is :"<<sum<<endl; cout<<"Diff is:"<<diff<<endl; system("pause"); }
Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) { int Num1; cout << "Enter 2 numbers: "; cin >> Num2; if (Num1 < Num2) cout << "Smallest number is " << Num1; else cout << "Smallest number is " << Num2; return 0; }
16 Points) Question 3 Write down the outputs of the following program into the provided table include <iostream> using namespace std; void fun I(int a); int fun2(int a, int b); int x-3: int main) int x-1,y 0,z-2; x-fun2(y,z); cout sx fun 1 (z); cout (#xtytz(endl; y-fun2(x,x); cout <exty+zscendl; system("pause"); void fun 1 (int a) int fun2(int a, int b) int static c2; return atx; 16 Points) Question 3 Write down the outputs of the following program into the provided table...
#include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS = 8; vector <double> inverse(NUM_ITEMS); int j; double temp; for (int i = 0; i < NUM_ITEMS; i++) { inverse.at(i) = 1 / (i + 1.0); } cout << fixed << setprecision(2); cout << "Original vector..." << endl; for (int i = 0; i < NUM_ITEMS; i++) { cout << inverse.at(i) << " "; } cout << endl; cout << "Reversed vector..." << endl; for...
#include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void displayArrayContent(int[]); void displayLargestValue(int[]); void displaySmallestValue(int[]); int main(){ int number; int numbers[SIZE] = {9,1,90,98,53,22,76,29,37,65}; cout <<"Enter a number: "; cin >> number; cout << endl; displayGreaterThan(numbers,number); cout << endl; displaySmallerThan(numbers,number); cout << endl; displayArrayContent(numbers); cout << endl; displayLargestValue(numbers); cout << endl; displaySmallestValue(numbers); cout << endl; return 0; } void displayGreaterThan(int value[],int num){ cout << " All larger value(s)than" <<...
#include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be implemented by you } int main() { cout << "To calculate x^y ..." << endl; double x; int y; cout << "Please enter x: "; cin >> x; cout << "Please enter y: "; cin >> y; if(x == 0) { if (y > 0) cout << 0 << endl; else cout << "x^y is not defined" <<endl; } else { cout << improvedPow(x,y)...
C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int* ptri, int* ptr2) { int *temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap1(int ptri, int ptr2){ int temp; temp = ptri; ptr1 = ptr2; ptr2 = temp; portion void swap2(int *&ptri, int *&ptr2){ int* temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap3(int &ptri, int &ptr2) { int temp; temp = ptr1; ptr1 = ptr2; ptr2 =...