-
Notifications
You must be signed in to change notification settings - Fork 3k
Open
Description
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
lowandhighinside theprint_rangefunction. - Used the
swapfunction to swaplowandhighiflowis greater thanhigh, eliminating the need for recursion. - Used
using namespace std;to avoid having to prefix standard library functions withstd::.
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.
Metadata
Metadata
Assignees
Labels
No labels