Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

do program in c++ A function is a subprogram that is included in a C++ program to perform a particular task such as obtaining data,

do program in c++

A function is a subprogram that is included in a C++ program to perform a particular task such as obtaining data, carrying out some calculations, displaying some information or messages, and displaying the output data. In C++, there are many predefined functions that are written to simplify the computations. Following is an example in which a predefined function can significantly simplify the computations. Here, we want the program to compute 24, 33, and 54 all in the same program and displays the results. // P31_1.cpp This C++ program computes the value of a to the power of b (a^b) for three cases. #include using namespace std; int main(void) { int i = 0, p = 1; int a = 2, b = 4; while (i < b) // computing 2^4 { p = p * a; i++; } cout << a << " to the power of " << b << " is = " << p << endl; i = 0; p = 1; a = 3, b = 3; while (i < b) // computing 3^3 { p = p * a; i++; } cout << a << " to the power of " << b << " is = " << p << endl; i = 0; p = 1; a = 5, b = 4; while(i < b) // computing 5^4 { p = p * a; i++; } cout << a << " to the power of " << b << " is = " << p << endl; return 0; } The heart of this program is the part in red font. Imagine, you wanted to compute hundreds of these calculations in a program. For now, it seems that we have to repeat several lines, which are almost identical, hundreds of times. But, we may have a better option. Here is a question for you to think about. In program P31_1.cpp, all variables are declared as int. Could you do the same computation for real values, i.e., could you make changes in that program such that it would compute something like 2.35.48? There is a predefined function in C++ that computes a number to the power of another. The function is called pow. This function will take two numbers as its arguments and will compute one to the power of the other and will return the result which is referred to as value returned. For example, in the above program, to compute 34, we can use p = pow(3,4). Here, 3 and 4 are the arguments to this function and the result, is the value returned which is assigned to p. Using this predefined function, the above code can be simplified significantly. Please note that in order to use the pow predefined function, you need to include the cmath directive, i.e. #include Exercise 3.1 Modify the P31_1.cpp program and use the predefined function pow to compute the a^b power. Call your new program ex31.cpp. Note that the pow function allows us to compute a real number to the power of another real number as well. Here is the definition of the pow function: pow - computes powers It takes two parameters of type double, a and b and its value returned is of type double as well, p = pow(a,b) Exercise 3.2 We can further improve the new program using a while loop, so that it asks the user to input an a and a b (basically of any type) and computes the pow(a, b) to compute p = pow(a,b) and displays the result. Call your new improved program ex32.cpp. There are more predefined functions in C++, here are some of them: abs - computes absolute value It takes one parameter of type integer, a, and its value returned is of type integer as well. p = abs(a) fabs - computes absolute value It takes one parameter of type double, a, and its value returned is of type double as well. p = fabs(a) labs - computes absolute value It takes one parameter of type long, a, and its value returned is of type long as well. p = labs(a) sqrt - computes square root It takes one parameter of type double, a, and its value returned is of type double as well. p = sqrt(a) ceil - ceiling (round up) It takes one parameter of type double, a, and its value returned is of type double as well. p = ceil(a) floor - floor (round down) It takes one parameter of type double, a, and its value returned is of type double as well. p = floor(a) As you may remember in Lab2, you were asked to solve the quadratic equation. The solutions to a quadratic equation, ax2 + bx + c = 0, are: 2 1 4 2 b b ac x a + = and 2 1 4 2 b b ac x a = The possible solutions were discussed in Lab2. Here, we only mention that before you compute these two roots, you had to make sure that the value under the radical was not negative and that a was not zero.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

More Books

Students also viewed these Databases questions

Question

Explain the treatments for domestic violence

Answered: 1 week ago