Lecture Notes on Iteration Using For-Statement in C++
Introduction
- Topic: New control statement in C++ called the
for statement for iteration by counting.
- Applications: Estimating the value of logarithm and solving the Him Jindal number problem.
Iterating by Counting: The For-Statement
Basic Concept
- In counting problems, the number of iterations required (
) may be known in advance.
- While-loop alternative: While loops use a condition, but for counting
for loops are more straightforward.
Mechanism
- Counting mechanism: Initialize count, execute a block, increment count, and repeat until the count equals n.
- Code Example (While Loop):
- Initialize variables.
- Set a count variable.
- Loop while
count <= n.
- Increment the count at the end of each iteration.
Variations
- Changing increments: For example, count by 2 instead of 1, using
count += 2.
- Handling even and odd n values.
Applying Counting to Problems
Sum of n Numbers
- Algorithm: Sum n given numbers by iterating n times using a loop.
- Key Idea: No need for artificial end-of-input conditions.
- Code Example: Initialize sum and count, loop for n times to read numbers and accumulate the sum.
Finding Maximum of n Numbers
- Similar to summing numbers but track the maximum value encountered.
Calculating Factorials
- Algorithm: Multiply successive terms iteratively, starting from 1.
- Code Example (While Loop): Initialize factorial to 1, loop with a counting mechanism, multiply in each iteration.
- Improvement: Use count starting from 2 to avoid unnecessary multiplication.
The for Statement in C++
Structure
- Syntax:
for (initialization; condition; increment).
- Example:
for (count = 1; count <= n; count++) { /* statements */ }
- Actions are executed in order: initialize, check condition, execute block, increment.
- Benefits: Consolidates initialization, condition check, and increment in one line.
Implementation Examples
- Sum of n Numbers: Using
for-loop to read and sum n numbers.
- Finding Maximum: Uses similar structure to identify the largest number in n inputs.
- Sum of First n Natural Numbers:
for loop adds integers from 1 to n.
- Calculating Factorials: Uses
for loop to iterate and calculate the factorial.
Advanced For-Statement Use
- Multiple Initialization and Increment: Use commas to separate multiple statements.
- Infinite Loop:
for (;;) is equivalent to while (1), and will loop indefinitely without any conditions.
Application: Estimating Logarithms Using Riemann Sums
Concept of Riemann Integral
- Function: Integration approximated by summing areas of rectangles under the curve.
- Use Case: Estimating log(a) to the base e by computing the area under y = 1/x from 1 to a.
Approach
- Width of Rectangles: (a - 1) / n if n rectangles are used.
- Height Calculation: Depends on x-coordinate, determined by iteration index.
- Code Example: Program to read a, divide the area, and sum areas of rectangles in a loop.
Generalizing the Program
- Input value of n for the number of rectangles instead of a fixed number.
Special Topic: Him Jindal Numbers
Problem Introduction
- Scenario: Different ways to construct a wall of length using bricks of length 1 and 2.
- Historical Context: Relation to Pingala and Him Chandra's work on poetic meters.
Recurrence Relations
- Definition: Number of ways to construct n lengths = ways to construct (n-1) lengths + (n-2) lengths.
- Recursive Nature: Solve using an iterative approach.
- Initialization: Base cases known; use iterative updates to compute further values.
Code Example
- Approach: Use an iteration to calculate subsequent values based on previous two values.
- Variables: Current, previous, and next values updated iteratively.
Key Takeaways
- Iteration by counting is a fundamental programming technique.
- The
for-statement in C++ elegantly handles initialization, condition checking, and incrementation.
- Advanced applications: Estimating logarithms, solving combinatorial problems.
Final Thoughts
- Cultural Insight: Recognition of historical contributions in mathematics and algorithms.
Resources: Wikipedia articles on Indian mathematics and historical figures for further learning.