Question

In: Computer Science

in C++ Compile and run the program for the following values: r = 2 Cm, h...

in C++

Compile and run the program for the following values: r = 2 Cm, h = 10 Cm.

The answer should be: The cross section area of the cylinder is 3.8955634 c

The side area of the cylinder is 19.474819 inch-sqr

Did you get the same answer? Explain the reason for such an error and fix the problem.

Modify the previous program to include a new function called total_area, that computes the total suface area of a cylinder. The total surface area is the sum of side area and cross section area. For the above test values the total area must be: 23.370382 inch-sqr In the above program, we used two different function names to distinguish between the function that computes the cross area and the one that computes the side area of a cylinder. Using overloading we can give both functions the same name but ask them to do two different things. The decision on which function to be chosen is made based on: 1) difference in the number of arguments, 2) difference between types of parameters, and 3) based on the difference in number and type of parameters (both 1 and 2). Here is the new version of the same program written using overloading.

// This program illustrates the local and global variables and call-by-value.

// This program computes the side area and the cross section area of a cylinder

#include

#include

using namespace std;

const double PI = 3.14159; // This variable is defined globally, known to all functions as PI

const double conversion = 0.3937; // This is the Cm to inch conversion factor

double area(double r); // Function declaration for function that computes cross section area

double area(double r, double h); // Function declaration for function that computes side area

int main(void) { double h, r; //variables local to the main function

cout << "Enter the radius and the height of the cylinder in Cm ";

cin >> r >> h;

cout << endl;

cout << "Before I do any computation or call any function, I want to let you know that \n";

cout << "you have entered r = " << r << " and h = " << h << "." << endl;

cout << "I am planning to use inch, thus in the first function, I will convert r, and " << endl;

cout << "in the second one I will convert h \n";

cout << "The cross section area of the cylinder is " << area(r) << " inch-sqr endl;

cout << "The side area of the cylinder is " << area(r,h) << " inch-sqr \n\n";

return 0;

}

double area(double r)

{

//Cross secion area includes the disks at the bottom and the top

r = r * conversion; // converting r to inch return 2*PI*pow(r,2);

}

double area(double r, double h)

{

double area; //variable local to Side_area function

h = h * conversion; // converting h to inch

r = r * conversion; // converting r to inch

area = 2*PI*r*h;

return area;

}

Note that we were able to use name overloading because of the fact that to compute cross section area, we only needed radius, r, as an argument and to compute the side area, we needed both the radius and height of the cylinder. Thus, here we used the difference between number of parameters to implement name overloading. Could we use overloading to compute the surface area and volume of a sphere? Explain your answer. The surface area of a sphere is S = 4*PI*r2 and the volume is V = (4.0/3.0)*PI*r3.

Solutions

Expert Solution

The surface area of a sphere is S = 4*PI*r2 and the volume is V = (4.0/3.0)*PI*r3.

lets simply name the function as calculate();

The usage of overloading depends on the number of paramaters and its types.Hence we could draft three cases whether name overloading can be done or not.

CASE 1 : Only 'r' radius as a parameter with the same type ie double.

double calculate(double r); --> surafce area

  double calculate(double r); --> volume

"The overloaded function definitions has to be either differed by number of formal paramaters or its types".

If r with double type alone is used as formal parameter for two different definitions there will be confusion in executing the proper definitions.

Hence not possible.

CASE 2 : Only 'r' radius as a parameter with the different type ie one take double and other take int.

double calculate(int r); --> surafce area

  double calculate(double r); --> volume

"The overloaded function definitions has to be either differed by number of formal paramaters or its types".

Here the number of paramaters is same for both the definitions ie 1,

But the type of the parameter differs one is int and other is double.

Therefore at this case the name over loading is possible.

CASE 3 : One more parameter in addition to  'r' radius as a parameter.

Lets assume we pass the pi value also as a parameter for any one of the definitions.

double calculate(double r,double pi); --> surafce area

  double calculate(double r); --> volume

"The overloaded function definitions has to be either differed by number of formal paramaters or its types".

Here we have different number of formal parameters for the different name of the functions.

Hence the name overloading is possible at this case.


Related Solutions

6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart...
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart for each of the following problems: a) Obtain three numbers from the keyboard, compute their product and display the result. b) Obtain two numbers from the keyboard, and determine and display which (if either) is the smaller of the two numbers. c) Obtain a series of positive numbers from the keyboard, and determine and display their average (with 4 decimal points). Assume that the...
Compile and run the following code then answer the following questions: a.) Run this command from...
Compile and run the following code then answer the following questions: a.) Run this command from the shell prompt: ./a.out ls -F Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Be sure to comment on the pointer arithmetic done inside the line: execvp(*(argv+1), argv+1); b.) Run this command from the shell prompt:...
A solid cylinder r = 17.5cm r = 17.5 cm, h = 16.5 cm h = 16.5 cm has a mass of 13.49 kg 13.49 kg. This cylinder is floating in water.
A solid cylinder r=17.5 cmr=17.5 cm , h=16.5 cmh=16.5 cm has a mass of 13.49 kg13.49 kg . This cylinder is floating in water. Then oil is poured on top of the water until the situation. How much of the height of the cylinder is in the oil?
Objectives To learn to code, compile, and run a program using file input and an output...
Objectives To learn to code, compile, and run a program using file input and an output file. Assignment Plan and code a program utilizing one file for input and one file for output to solve the following problem: Write a program to determine the highest number, the lowest number, their total, and the average of each line of numbers in a file. A file contains 7 numbers per line. How many lines a file contains is unknown. Note Label all...
UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the...
UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the following commands, and at the end, use CTRL+C to terminate the program: ls ls -l tryShell* date whoami hostname uname -a ctrl+C    (2) Run the program (tryShell) with "time -p" with a few commands: time -p ./tryShell (3) Edit the program (tryShell.c) so that it will exit (terminate the program) when the input command string is "exit" try shell.c code at bottom //////////// #include...
2. Run the following C program, and submit the screenshot of the result. Also briefly explain...
2. Run the following C program, and submit the screenshot of the result. Also briefly explain the result. #include <sys/types.h> #include <stdio.h> #include <unistd.h> #define SIZE 5 int nums[SIZE] = {0,1,2,3,4}; int main() { int i; pid_t pid; pid = fork(); if (pid == 0) { for (i = 0; i < SIZE; i++) { nums[i] *= -i; printf("CHILD: %d ",nums[i]); /* LINE X */ } } else if (pid > 0) { wait(NULL); for (i = 0; i <...
1. Make the flow diagram and the C ++ program that receives three integer values ​​R,...
1. Make the flow diagram and the C ++ program that receives three integer values ​​R, T and Q, determine if they satisfy the expression below, and if so, show the corresponding values ​​of R, T and Q. R'- T '+ 4 * Q? <820 2. Construct a flow chart and the corresponding program in C which, when receiving Y as data, calculates the result of the following function and prints the values ​​of X and Y.
This programming assignment will consist of a C++ program. Your program must compile correctly and produce...
This programming assignment will consist of a C++ program. Your program must compile correctly and produce the specified output. Please note that your programs should comply with the commenting and formatting described in the Required Program Development Best Practices document that has been discussed in class and is posted to the eLearning system. Please see this descriptive file on the eLearning system for more details. The name to use in the main configuration screen text box Name: [ ] in...
Let H = {A∈GL(2,R)|(detA)2= 1}. Prove that H / GL(2,R).
Let H = {A∈GL(2,R)|(detA)2= 1}. Prove that H / GL(2,R).
UNIX/LINUX LAB   (1) Compile hello.c program: gcc hello.c -o hello (2) Compile hello.c program with -static...
UNIX/LINUX LAB   (1) Compile hello.c program: gcc hello.c -o hello (2) Compile hello.c program with -static option: gcc -static hello.c -o hello1 (3) Use size command to see the memory layout of these two programs (hello and hello1). (4) Write a brief summary of what you have noted here. Submit the session history (log) of this activity and a brief summary. hello.c program bottom //////////// #include <stdio.h> #include <stdlib.h> int main() { printf("Hello World\n"); // waw hello Richard and ok...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT