# Program To Create Own Functions for Computation in C++ Language Assignment Solution

July 10, 2024
Dr. Eleanor
🇬🇧 United Kingdom
C++
Dr. Hughes holds a Ph.D. in Computer Science from the University of Cambridge and specializes in advanced algorithms and data structures. With over 800 completed orders, she has a wealth of experience in solving complex C++ assignments with precision and efficiency.
Key Topics
• Instructions
• Objective
• Requirements and Specifications
Tip of the day
News

## Instructions

### Objective

Write a program to create own functions for computation in c++ language.

## Requirements and Specifications

Module 8 Lab – Part 2

(Some of these exercises require written answers to questions. Answer those and copy and paste those answers to the bottom of the source file to which they apply).

This lab contains detailed information about functions as well as exercises using functions. Be sure to read through the entire lab. If you have any questions let me know.

Introduction to programmer-defined functions

We can write your C++ assignment to perform some computations. These types of functions are referred to as programmer-defined functions. A programmer-defined function may have three parts: 1) Function definition, 2) Function Call, and/or 3) Function declaration.

The following program (Example 4.3 from the textbook), which computes the total cost of purchases made in a store uses a function to compute the cost plus 5% sales tax of purchases made. This example is also the topic of the first video in Module 8.

Source Code

```YOUR NAME EXP 3 //�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 in this program as PI double area(double r); // Function prototype for function cross_area double area(double r, double h); // Function prototype for function Side_area double area_sphere(double r); // surface area of the sphere double volume(double r); int main(void) { double h, r; //variables local to the main function /* FIRST FIX, CHANGE PRECISION OF OPUTPUT TO 7 SIGNIFICANT FIGURES */ cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(7); 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 << "I am planning to use inch, this 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 * 0.3937; //�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 * 0.3937; //�converting h to inch area = 2 * PI*r*h; return area; } double area_sphere(double r) { double area; r = r * 0.3937; // r to inch area = 4 * PI*pow(r, 2); return area; } double volume(double r) { double vol; r = r * 0.3937; vol = (4.0 / 3.0)*PI*pow(r, 3); return vol; } /* We cannot add overloading functions to compute the sphere area, because the only parameter needed to compute the area of the sphere is the radius, so we cannot define a function named 'area' that receives a paramter 'r' because that function already exists and is the one used to calculate the cross-section area of the disk For the volume of the sphere, we do not need to use overloading because there is no function with the name of 'volume', so if we define a function named 'volume', it will be only to calculate the volume of the sphere */ YOUR NAME EXP 4 //�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 in this program as PI double area(double r); // Function prototype for function cross_area double area(double r, double h); // Function prototype for function Side_area double area_sphere(double r); // surface area of the sphere double volume(double r);// volume of sphere double volume(double r, double h); // volume of cylinder int main(void) { double h, r; //variables local to the main function /* FIRST FIX, CHANGE PRECISION OF OPUTPUT TO 7 SIGNIFICANT FIGURES */ cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(7); 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 << "I am planning to use inch, this 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" << endl; cout << "The volume of the cylinder is " << volume(r, h) << " inch-cubic\n" << endl; cout << "The surface area of the sphere is " << area_sphere(r) << " inch-sqr" << endl; cout << "The volume of the sphere is " << volume(r) << " inch-cubic\n\n"; return 0; } double area(double r) // side area of cylinder { //Cross secion area includes the disks at the bottom and the top r = r * 0.3937; //�converting r to inch return 2 * PI*pow(r, 2); } double area(double r, double h) // total area of cylinder { double area; //variable local to Side_area function h = h * 0.3937; //�converting h to inch area = 2 * PI*r*h; return area; } double area_sphere(double r) { double area; r = r * 0.3937; // r to inch area = 4 * PI*pow(r, 2); return area; } double volume(double r) // volume of sphere { double vol; r = r * 0.3937; vol = (4.0 / 3.0)*PI*pow(r, 3); return vol; } double volume(double r, double h) // volume of cylinder { double volume; r = r * 0.3937; h = h * 0.3937; volume = PI * pow(r, 2)*h; return volume; } /* In this exercise, we can use overloading only in the function to calculate the volumes of the cylinder and the sphere. This is because, both functions are different in the number of arguments that the functions receive. However, it is not possible for the area because the functions would have the same name and arguments, so there is nothing to differentiate them */ ```

## Similar Samples

Explore our diverse range of programming homework samples at ProgrammingHomeworkHelp.com. Whether you need assistance with Java, Python, C++, or databases, our samples showcase expertly crafted solutions to help you understand complex concepts and excel in your programming assignments. Browse through our examples to see how we can help you achieve academic success in programming.