<< Chapter < Page Chapter >> Page >

cout<<"The circumference of the circle is "

<<circumference<<endl;

return 0;

}

The output of the above program:

The circumference of the circle is 12.5664

Focus on problem solving

In this section, we present a programming problem to further illustrate both the use of cin statements to accept user input data and the use of library functions for performing calculations.

Problem: Approximating the Exponential Function

The exponential function e^x, where e is known as Euler’s number (and has the value 2.718281828459045…) appears many times in descriptions of natural phenomena. The value of e^x can be approximated using the following series:

1 + x/1 + x^2/2 + x^3/6 + x^4/24 + x^5/120 + x^6/720 + …

Using this polynomial as a base, assume you are given the following assignment: Write a program that approximates e raised to a user input value of x using the first four terms of this series. For each approximation, display the value calculated by C++’s exponential function, exp(), the approximate value, and the absolute difference between the two. Make sure to verify your program using a hand calculation. Once the verification is complete, use the program to approximate e^4.

Using the top-down development procedure, we perform the following steps.

Step 1: analyze the problem

The statement of the problem specifies that four approximations are to be made using one, two, three, and four terms of the approximating polynomial, respectively. For each approximation, three output values are required: the value of produced by the exponential function, the approximate value, and the absolute difference between the two values. The structure of the required output display is as below (in symbolic form).

Realizing the each line in the display can only be produced by executing a cout statement, it should be clear that four such statements must be executed. Additionally, since each output line contains three computed values, each cout statement will have three items in its expression list.

The only input to the program consists of the value of x. This will, of course, require a single prompt and a cin statement to input the necessary value.

Step 2: develop a solution

Before any output items can be calculated, the program needs to prompt the user for a value of x and then accept the entered value. The output display consists of two title lines followed by four lines of calculated data. The title lines can be produced using two cout statements. Now let’s see how the data being displayed are produced.

The first item on the first data output line illustrated in Table 3.4 can be obtained using the exp() function. The second item on this line, the approximation to ex , can be obtained by using the first term in the polynomial that was given in the program specification. Finally, the third item on the line can be calculated by using the abs() function on the difference between the first two lines. When all of these items are calculated, a single cout statement can be used to display the three results on the same line.

Questions & Answers

what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
Damian
yes that's correct
Professor
I think
Professor
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
How we are making nano material?
LITNING Reply
what is a peer
LITNING Reply
What is meant by 'nano scale'?
LITNING Reply
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
Rafiq
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
Daniel
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Good
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask