Write a C++ program that asks the user to input a positive integer representing the elapsed number of seconds. Your program should then output the elapsed hours, minutes, and seconds in the following format: hours : minutes : seconds Sample Input Outrut: Please enter the number of elapsed seconds: 7250 2:0: 50 Submit your solution in formatted_time.cpp.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter4: Selection Structures
Section4.3: Nested If Statements
Problem 7E
icon
Related questions
Question
C++ coding
1. Formatted Time
Write a C++ program that asks the user to input a positive integer representing the elapsed number
of seconds. Your program should then output the elapsed hours, minutes, and seconds in the
following format:
hours : minutes : seconds
Sample Input/Output:
Please enter the number of elapsed seconds: 7250
2:0:50
Submit your solution in formatted_time.cpp.
2. Monte Carlo Area Calculations
You are not required to solve nor submit this problem, just read it and understand its solution (I am
attaching its solution with this assignment) Understanding it will make it easier for you to solve problem
3 below.
Refer to the figure below, and write a C++ program which prompts the user to enter the values of
the rectangle width (W), its length (L), and the circle radius (r). You can assume that W S L and
rsW/2 (the user is expected to provide input that satisfies these conditions; i.e. you are not
required to check or handle the case when the input is invalid). You are asked to calculate the blue
area in the figure below using two methods:
(a) Method 1- Simply calculate the area using the equation (Use n = 3.1416):
Blue area = Rectangle Area - Circle Area = L x W – ar².
(b) Method 2 - Use the Monte Carlo method (See detailed explanation on the next page).
Origin
Complete the solutions and provide two cout statements which print the results after using each
of the two methods, so that you can compare them.
About the Monte-Carlo Method
The Monte Carlo method relies on repeated random sampling to obtain numerical results, and it is
well-known and used in different engineering and other applications. We will use Monte Carlo in
this problem to obtain an approximate value of the blue area. The idea is to generate a large number
of random points within the rectangle area, and then count the number of points that fall within the
blue area (i.e. outside the circle). The blue area can then be estimated to be equal to area of the
rectangle multiplied by the percentage of points that fell outside the circle. For example, if 70% of
the generated random points fall outside the circle and 30% fall inside it, then the blue area is
approximately equal to 0.7xrectangle area. The larger the number of points, the more accurate the
approximation will be
How to implement it in C++
As you know from the carlier assignments, in order to generate random values in C+, you will
need to include the stdlib.h and time.h libraries (#include<stdlib.h> and #include<time.h>). You
also need to add the srand(time(0)); statement at the beginning of main (include the srand
statement only once; do not repeat it for every random value). You can then use the rand( )
function and the RAND_MAX constant to generate a random point (x, y) within the area of the
rectangle. That is, you need to generate random x and y coordinates such that:
s xs
and -< ys
where we have assumed that the circle and rectangle are co-centered at the origin for simplicity.
In order to check if the point is outside the circle, we simply need to check if (x? + y? > r?).
You will need to generate a large number of random points (e.g. 1000 or 10000 points) to get a
good estimate of the area.
Transcribed Image Text:1. Formatted Time Write a C++ program that asks the user to input a positive integer representing the elapsed number of seconds. Your program should then output the elapsed hours, minutes, and seconds in the following format: hours : minutes : seconds Sample Input/Output: Please enter the number of elapsed seconds: 7250 2:0:50 Submit your solution in formatted_time.cpp. 2. Monte Carlo Area Calculations You are not required to solve nor submit this problem, just read it and understand its solution (I am attaching its solution with this assignment) Understanding it will make it easier for you to solve problem 3 below. Refer to the figure below, and write a C++ program which prompts the user to enter the values of the rectangle width (W), its length (L), and the circle radius (r). You can assume that W S L and rsW/2 (the user is expected to provide input that satisfies these conditions; i.e. you are not required to check or handle the case when the input is invalid). You are asked to calculate the blue area in the figure below using two methods: (a) Method 1- Simply calculate the area using the equation (Use n = 3.1416): Blue area = Rectangle Area - Circle Area = L x W – ar². (b) Method 2 - Use the Monte Carlo method (See detailed explanation on the next page). Origin Complete the solutions and provide two cout statements which print the results after using each of the two methods, so that you can compare them. About the Monte-Carlo Method The Monte Carlo method relies on repeated random sampling to obtain numerical results, and it is well-known and used in different engineering and other applications. We will use Monte Carlo in this problem to obtain an approximate value of the blue area. The idea is to generate a large number of random points within the rectangle area, and then count the number of points that fall within the blue area (i.e. outside the circle). The blue area can then be estimated to be equal to area of the rectangle multiplied by the percentage of points that fell outside the circle. For example, if 70% of the generated random points fall outside the circle and 30% fall inside it, then the blue area is approximately equal to 0.7xrectangle area. The larger the number of points, the more accurate the approximation will be How to implement it in C++ As you know from the carlier assignments, in order to generate random values in C+, you will need to include the stdlib.h and time.h libraries (#include<stdlib.h> and #include<time.h>). You also need to add the srand(time(0)); statement at the beginning of main (include the srand statement only once; do not repeat it for every random value). You can then use the rand( ) function and the RAND_MAX constant to generate a random point (x, y) within the area of the rectangle. That is, you need to generate random x and y coordinates such that: s xs and -< ys where we have assumed that the circle and rectangle are co-centered at the origin for simplicity. In order to check if the point is outside the circle, we simply need to check if (x? + y? > r?). You will need to generate a large number of random points (e.g. 1000 or 10000 points) to get a good estimate of the area.
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning