//Include necessary libraries //--> //Use namespace std //--> //Declaring functi
ID: 3624519 • Letter: #
Question
//Include necessary libraries//-->
//Use namespace std
//-->
//Declaring function prototypes for swapping numbers
//Declare a function prototype with
//Function name: swapByValue
//Return type: void
//Arguments: two integers passed by value
//-->
//Declare a function prototype with
//Function name: swapByReference
//Return type: void
//Arguments: two integers passed by reference
//-->
//Declaring function prototypes for calculating area of circle
//Declare a function prototype with
//Function name: areaByValue
//Return type: double
//Arguments: none
//-->
//Declare a function prototype with
//Function name: areaByReference
//Return type: void
//Arguments: one double passed by reference
//-->
int main()
{
int num1;
cout<<"Please enter first number ";
cin>>num1;
int num2;
cout<<"Please enter second number ";
cin>>num2;
//Trying to Swap two numbers by using the swapByValue method
cout<<" INSIDE MAIN: The values before swap are: num1 = "<<num1<<", num2 = "<<num2<<" ";
//Call the swapByValue method passing num1 and num2 to it.
//-->
cout<<"BACK INSIDE MAIN: The values after call to swapByValue function are: num1 = "<<num1<<", num2 = "<<num2<<" ";
//Trying to Swap two numbers by using the swapByReference method
cout<<" INSIDE main: The values before swap are: num1 = "<<num1<<", num2 = "<<num2<<" ";
//Call the swapByReference method passing num1 and num2 to it.
//-->
cout<<"BACK INSIDE main: The values after call to swapByReference function are: num1 = "<<num1<<", num2 = "<<num2<<" ";
//Lets calculate the area of circle using call by value and call by reference
double area1, area2;
cout<<" Area Using call by Value";
//Assign to area1 the value returned by areaByValue()
//-->
cout<<"Area of circle is "<<fixed<<setprecision(2)<<area1;
cout<<" Area Using call by Reference";
//Call the function areaByReference passing to it area2, areaByReference(area2)
//-->
cout<<"Area of circle is "<<fixed<<setprecision(2)<<area2;
return 0;
}
/*
* Function swapByValue tries to swap the input arguments(num1 and num2) that are passed by value
*/
//Write the function header in the following line.
//-->
{
cout<<" Inside Function swapByValue: The values BEFORE swap are: num1 = "<<num1<<", num2 = "<<num2<<" ";
//Write the code to swap num1 and num2
//HINT: declare a integer called temporary
//Assign num1 to temporary
//Assign num2 to num1
//Assign temporary to num2
//-->
cout<<" Inside Function swapByValue: The values AFTER swap are: num1 = "<<num1<<", num2 = "<<num2<<" ";
}
/*
* Function swapByReference tries to swap the input arguments(num1 and num2) that are PASSED BY REFERENCE
*/
//Write the function header in the following line.
//-->
{
cout<<" INSIDE FUNCTION swapByReference: The values BEFORE swap are: num1 = "<<num1<<", num2 = "<<num2<<" ";
//Write the code to swap num1 and num2
//Refer to the HINT in the above function to swap two numbers
//-->
cout<<" INSIDE FUNCTION swapByReference: The values AFTER swap are: num1 = "<<num1<<", num2 = "<<num2<<" ";
}
/*
* Calculates area of circle using call by value
* NOTE: the area is returned
*/
//Define the header for the function areaByValue in the following line
//-->
{
double area, radius;
cout<<" Enter radius of circle ";
cin>>radius;
double PI = 3.1459;
//Calculate area of circle as PI*radius*radius
//-->
//NOTE: We return the area becuase the area variable in the function is local to the function
//and to get its value in the main() we need the function to return it.
//-->
}
/*
* Calculates area of circle using call by reference
* NOTE: the area is NOT returned
*/
//Define the header for the function areaByReference in the following line
//-->
{
double radius;
cout<<" Enter radius of circle ";
cin>>radius;
double PI = 3.1459;
//Calculate area of circle as PI*radius*radius
//-->
//NOTE: We do NOT return any value becuase we have passed area by reference and it is automatically updated
}
/* QUESTIONS FOR THOUGHT
* ---------------------
* 1. Can a function return multiple values?
* 2. What are static variables? Can you use a static variable to count the number of times a function is called in the main?
* 3. What are default arguments for functions? How would their prototypes look like?
* 4. What are reference variables? Can you explain the difference between the functions
* swapByValue and swapByReference
* 5. In the function areaByValue, the variable area is local to the function, in the function callByReference(area) is still
* a local variable, but how are the two functions different? Why do we need to return the area in one and not in the other.
*/
Explanation / Answer
//Include necessary libraries
#include<iostream>
#include<iomanip>
using namespace std;
//Function prototypes
void swapByValue(int num1,int num2);
void swapByReference(int *num1,int *num2);
double areaByValue();
void areaByReference(double *val);
int main()
{
int num1;
cout<<"Please enter first number ";
cin>>num1;
int num2;
cout<<"Please enter second number ";
cin>>num2;
//Trying to Swap two numbers by using the swapByValue method
cout<<" INSIDE MAIN: The values before swap are: num1 = "<<num1<<", num2 = "<<num2<<" ";
//Call the swapByValue method passing num1 and num2 to it.
swapByValue(num1,num2);
cout<<"BACK INSIDE MAIN: The values after call to swapByValue function are: num1 = "<<num1<<", num2 = "<<num2<<" ";
//Trying to Swap two numbers by using the swapByReference method
cout<<" INSIDE main: The values before swap are: num1 = "<<num1<<", num2 = "<<num2<<" ";
//Call the swapByReference method passing num1 and num2 to it.
swapByReference(&num1,&num2);
cout<<"BACK INSIDE main: The values after call to swapByReference function are: num1 = "<<num1<<", num2 = "<<num2<<" ";
//Lets calculate the area of circle using call by value and call by reference
double area1, area2;
cout<<" Area Using call by Value";
//Assign to area1 the value returned by areaByValue()
area1=areaByValue();
cout<<"Area of circle is "<<fixed<<setprecision(2)<<area1;
cout<<" Area Using call by Reference";
//Call the function areaByReference passing to it area2, areaByReference(area2)
areaByReference(&area2);
cout<<"Area of circle is "<<fixed<<setprecision(2)<<area2;
return 0;
}
/*
* Function swapByValue tries to swap the input arguments(num1 and num2) that are passed by value
*/
void swapByValue (int num1,int num2)
{
cout<<" Inside Function swapByValue: The values BEFORE swap are: num1 = "<<num1<<", num2 = "<<num2<<" ";
int temp;
temp=num1;
num1=num2;
num2=temp;
cout<<" Inside Function swapByValue: The values AFTER swap are: num1 = "<<num1<<", num2 = "<<num2<<" ";
}
/*
* Function swapByReference tries to swap the input arguments(num1 and num2) that are PASSED BY REFERENCE
*/
void swapByReference(int *num1,int *num2)
{
cout<<" INSIDE FUNCTION swapByReference: The values BEFORE swap are: num1 = "<<*num1<<", num2 = "<<*num2<<" ";
int temp;
temp=*num1;
*num1=*num2;
*num2=temp;
cout<<" INSIDE FUNCTION swapByReference: The values AFTER swap are: num1 = "<<*num1<<", num2 = "<<*num2<<" ";
}
/*
* Calculates area of circle using call by value
* NOTE: the area is returned
*/
double areaByValue()
{
double area, radius;
cout<<" Enter radius of circle ";
cin>>radius;
double PI = 3.1459;
//Calculate area of circle as PI*radius*radius
area=PI*radius*radius;
return area;
}
/*
* Calculates area of circle using call by reference
* NOTE: the area is NOT returned
*/
void areaByReference(double *area)
{
double radius;
cout<<" Enter radius of circle ";
cin>>radius;
double PI = 3.1459;
//Calculate area of circle as PI*radius*radius
*area=PI*radius*radius;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.