Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ch 1 , ex 11 yo u can make it more simple with this #843

Open
Aayush8106 opened this issue Nov 17, 2024 · 0 comments
Open

Ch 1 , ex 11 yo u can make it more simple with this #843

Aayush8106 opened this issue Nov 17, 2024 · 0 comments

Comments

@Aayush8106
Copy link

Here's a simplified version of the code that doesn't pass arguments to the function:

#include <iostream>
using namespace std;

void print_range() {
    int low, high;
    cout << "Enter two integers:\n";
    cin >> low >> high;

    if (low > high) {
        swap(low, high);
    }

    for (int i = low; i <= high; ++i) {
        cout << i << " ";
    }
}

int main() {
    print_range();
    return 0;
}

Changes made:

  • Removed function arguments and instead declared low and high inside the print_range function.
  • Used the swap function to swap low and high if low is greater than high, eliminating the need for recursion.
  • Used using namespace std; to avoid having to prefix standard library functions with std::.

Note: The original code used recursion to handle cases where low is greater than high. This version uses the swap function to achieve the same result more efficiently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant