Loops
Basic Loops
Below are two example problems involving loops. Note that there may be more valid solutions than those provided. Ensure you understand the solutions, then write programs to solve the remaining problems.
Problem 1
Write a program that:
- Prompts the user to enter an integer N.
- Prompts the user to enter N floating-point numbers (i.e., values of type double).
- Calculates and prints the average of the entered numbers.
To calculate the average, use the formula:
Here, “Sum of values” refers to the total of all floating-point numbers entered by the user, and “Number of values” refers to N.
Problem 2
Write a program that:
- Prompts the user to enter an integer.
- Checks if the entered integer is a prime number.
- Prints:
- “That number is prime!” if the number is prime.
- “That number is composite!” if the number is not prime.
An integer is considered prime if it is only divisible by 1 and itself. Examples of prime numbers include: 1, 2, 3, 5, 7, 11, 13, 17, and 19.
A composite number (non-prime) is divisible by other integers besides 1 and itself. Examples:
- 9 is composite because it is divisible by 3.
- 15 is composite because it is divisible by 3 and 5.
- All even numbers greater than 2 are composite because they are divisible by 2.
To check divisibility of an integer by another integer , use the following boolean expression:
a % b == 0
This expression evaluates to true
if a
is divisible by b
.
You only need to check divisors smaller than . Numbers cannot be divisible by integers larger than themselves.
Problem 3
Write a program that:
- Prompts the user to enter 10 integers.
- For each entered integer, computes and prints its factorial.
he factorial of a number is represented in mathematics as (this exclamation point denotes the factorial operator).
The factorial of is defined as the product of all positive integers less than or equal to :
For example, . The value of is .
Use a loop to perform the multiplication required to compute the factorial for each number.
Approximating Pi
Imagine a unit circle (a circle with a radius of 1) inscribed within a square. The setup looks like this:
The area of a circle with radius is calculated using the formula . For a unit circle, this becomes:
Since the radius of the circle is 1, the side length of the square that encloses the circle must be 2. The area of the square is:
Let P represent the ratio of the area of the circle to the area of the square. For this scenario:
By rearranging the equation algebraically, we get:
This relationship shows that if we can approximate , we can multiply it by 4 to approximate .
Monte Carlo Methods
Monte Carlo methods are a class of computational algorithms that rely on random sampling to approximate solutions. In this case, our goal is to approximate P using a Monte Carlo method.
Here’s the approach:
- If we randomly sample Cartesian points within the square, some proportion of these points will also fall inside the circle.
- If the sampling is uniform (i.e., every point within the square is equally likely to be chosen), then the probability of a point landing inside the circle is exactly equal to .
- To approximate , we simply need to sample many points within the square and calculate the proportion of those points that fall inside the circle.
Coordinates of the Square and Circle
- The circle is centered at the origin .
- The square has a side length of 2, with its corners at the following coordinates:
- Top-left:
- Top-right:
- Bottom-right:
- Bottom-left:
Sampling Points
To randomly sample a point within the square:
- Generate an X-coordinate within the range
- Generate a Y-coordinate within the range
- Together, these values form the sampled point
Checking if a Point is Inside the Circle
A point lies inside the circle if it satisfies the circle’s equation:
Implementation
Implement the Monte Carlo method described above to approximate and .
Use the Monte Carlo method to sample 100,000 random points within the square. This provides a sufficiently large number of samples for an accurate approximation. Determine the proportion of sampled points that fall inside the circle. This proportion represents . Multiply by 4 to calculate an approximation of . Print the calculated value of to the standard output. Print the approximation of to the standard output.
Avoid using integers to sample coordinates, as truncation would lead to a highly inaccurate approximation. Instead, use floating-point values (e.g., double).
To randomly sample a floating-point value within a specific interval , use the following expression:
(static_cast<double>(rand()) / RAND_MAX) * (b - a) + a