For Loops: Printing Numbers II in C++ (Time Complexity: O(n))


Let's see an example where we change the iteration statement.

We can print all even numbers from 2 through 10:

for (int i = 2; i <= 10; i += 2) {
	cout << i << endl;
}

The output of this code is:

2
4
6
8
10

Let's break down this code:

  • The initialization statement is int i = 2, so we start iterating from number 2.

  • The condition statement is i <= 10 so we end at number 10. We could've written i < 11 and it would still be correct.

  • The iteration statement is i += 2, so we increase our number by 2 every time.


Assignment

Let's print all odd numbers from 7 through 23 using a for loop.


Hint
Look at the examples above if you get stuck.


Understanding the Problem

The core challenge of this problem is to correctly set up a for loop that iterates through a specific range of numbers and prints only the odd numbers. This is a common task in programming that helps in understanding loop constructs and iteration control.

Significance: This problem is fundamental in learning how to control loops and is widely applicable in scenarios where specific conditions need to be met during iteration.

Potential pitfalls include incorrect initialization, condition, or iteration statements that could lead to infinite loops or incorrect outputs.

Approach

To solve this problem, we need to:

Naive Solution

A naive solution might involve checking each number in the range to see if it is odd, but this is not optimal as it involves unnecessary checks.

Optimized Solution

The optimized solution directly iterates through the odd numbers by starting at 7 and incrementing by 2 each time:

for (int i = 7; i <= 23; i += 2) {
    cout << i << endl;
}

Algorithm

Step-by-step breakdown:

  1. Initialize the loop variable i to 7.
  2. Set the loop condition to i <= 23.
  3. In each iteration, print the value of i.
  4. Increment i by 2.
  5. Repeat until the condition is no longer met.

Code Implementation

#include <iostream>
using namespace std;

int main() {
    // Loop from 7 to 23, incrementing by 2 each time
    for (int i = 7; i <= 23; i += 2) {
        // Print the current value of i
        cout << i << endl;
    }
    return 0;
}

Complexity Analysis

The time complexity of this approach is O(n), where n is the number of iterations. In this case, n is the number of odd numbers between 7 and 23, which is a constant. The space complexity is O(1) as we are using a fixed amount of extra space.

Edge Cases

Potential edge cases include:

Testing

To test the solution comprehensively:

Thinking and Problem-Solving Tips

When approaching such problems:

Conclusion

Understanding how to control loops and iterate through specific ranges is crucial in programming. This problem helps in grasping these concepts and applying them effectively.

Encourage readers to practice similar problems to strengthen their understanding and problem-solving skills.

Additional Resources

For further reading and practice: