0

I feel like I might be missing something here, but something is telling me that I might just be making this more difficult that it has to be, but from the book, "C++ Primer, 5th ed.," I'm stuck on this problem:

Exercise 1.11: Write a program that prompts the user for two integers. Print each number in the range specified by those two integers.

Up until this time in the book, the only loop being used is the while loop, and no conditional expressions, like if, have been introduced. The problem would be simple if it were not for the fact that a user could put the values into the integers asked for in ascending or descending order, so simple subtraction WOULD find the difference, but without testing which way to increment.

How could I absolutely print the range between the numbers, guaranteeing not to just increment towards infinity without testing the outcome of such math and/or without comparing the two numbers? This is what I have; it works when the first value: v1 is less than or equal to the second: v2, but not otherwise:

#include <iostream>

int main()  
{  
    int v1 = 0, v2 = 0;  
    std::cout << "Enter two integers to find numbers in their range (inclusive): "  
              << endl;  
    std::cin >> v1 >> v2;  
    while (v1 <= v2)  
    {  
        std::cout << v1;  
        ++ v1;  
    }  
    return 0;  
}  

Any help would be greatly appreciated!

6
  • 1
    It should be....cin>>v1>>v2;
    – Recker
    Jan 3, 2013 at 20:26
  • min && max can also be implemented with avg(x,y)-+0.5*abs(diff(x,y)) Jan 3, 2013 at 20:28
  • Well, seeing as how you found the difference part of it, you can always use std::min to find the lower one and count that many numbers up.
    – chris
    Jan 3, 2013 at 20:29
  • @AkiSuihkonen, in practice this 0.5 will likely ruin the whole thing. Jan 3, 2013 at 20:30
  • @AkiSuihkonen, I'd vote for either >> or integer division. Jan 3, 2013 at 20:48

6 Answers 6

0

Here is a simple rewrite where you use min and max to determine the range you want to iterate on:

#include <iostream>

int main()  
{  
    int v1 = 0, v2 = 0;  
    std::cout << "Enter two integers to find numbers in their range (inclusive): " << std::endl;  
    std::cin >> v1 >> v2;  
    int current =  std::min(v1, v2);
    int max = std::max(v1, v2);
    while (current <= max)  
    {  
        std::cout << current << std::endl;  
        ++ current;  
    }  
    return 0;  
}

This also allows you to keep the two inputs "intact", because you're using another variable to iterate on the values.

Also note that the ++current could be done on the exact same line it is printed if it was replaced by current++. The later would return the current value of current and THEN increment it.

Edit

Here's what Michael suggested I believe, in working code:

#include <iostream>

int main()  
{  
    int v1 = 0, v2 = 0;  
    std::cout << "Enter two integers to find numbers in their range (inclusive): " << std::endl;  
    std::cin >> v1 >> v2;  
    int increment = (v2 - v1) / std::abs(v2 - v1);
    int current = v1;
    int max = v2 + increment;
    while (current != max)  
    {  
        std::cout << current << std::endl;  
        current += increment;  
    }  
    return 0;  
}
11
  • Well, first, it ill not print in reverse order (as the OP seems to want) and second, using min and max sounds like cheating to me ;-) Jan 3, 2013 at 20:36
  • @MichaelKrelin-hacker I'm sorry but I don't see the OP asking for reverse order. Also, while learning how to program, I find it way more clearer to use standard methods like min and max than using a division to find out in which direction to "increment"
    – emartel
    Jan 3, 2013 at 20:38
  • @ermartel, I do — if it were not for the fact that a user could put the values into the integers asked for in ascending or descending order. As for the clarity, it sounds more like an exercise than production code and I see math tag here… Helps to understand the nature of numbers, which is important in programming. Jan 3, 2013 at 20:41
  • Yeah and sorry for unfortunate wording (about the direction to "increment"), my bad. Jan 3, 2013 at 20:41
  • @emartel, I wouldn't go quite as far as to say, "cheating," but those parts of standard library weren't introduced yet. But yes, that is the outcome I wish to achieve.
    – Louis C.
    Jan 3, 2013 at 20:55
0

You can use (v2-v1)/abs(v2-v1) or some such for increment. (provided they're not equal). And for loop condition you may check if current number is still in between, like (v1<=v && v<=v2) || (v2<=v && v<=v1).

And to avoid the case of zero, I'd turn it into do while loop and use v1!=v2 && ... as a condition.

To sum it all up:

int v = v1;
do {
   std::cout << v << std::endl;
}while( v1!=v2 && ( (v1<=(v+=(v2-v1)/std::abs(v2-v1)) && v<=v2) || (v2<=v && v<=v1) ) );

P.S. I trust you can resolve the input issue mentioned in comments and in the other answer on your own.

17
  • I fixed it, thank ye kindly, sir. It looks like your formula would equal 0 for any values of v1 and v2... Care to elaborate?
    – Louis C.
    Jan 3, 2013 at 20:33
  • @LouisC. Why would it? For 1 and 3 it would be (3-1)/(3-1)==1 Jan 3, 2013 at 20:34
  • And for 3 and 1, it will be (1-3)/(1-3)==-1. Jan 3, 2013 at 20:34
  • 1
    (1-3) / (1-3) == 1 by the way
    – emartel
    Jan 3, 2013 at 20:42
  • Which means I wrote some utter crap… hmm. Jan 3, 2013 at 20:44
0

I tried my amateur way and got the result. I hope it will be helpful.

#include <iostream>

using namespace std;

int main()
{
int a;
int b;
int val;
cout << "Please enter small value "<< endl;
cin >> a;
cout << "Please enter bigger value than the last one " << endl;
cin >> b;

while (val>a)
{
    val = --b;
     cout << "The numbers between a & b are "<< val << endl;
}
return 0;
}
0

Since I still remember this problem from the C++ Primer, I think the way they wanted you to solve it was with the basics of programming they provided you until then. In my opinion it should have been solved like this, if this was this exact problem from this book:

    #include <iostream>
int main()
{
    int v1 = 0, v2 = 0;
    std::cout << "Enter 2 numbers" << std::endl;
    std::cin >> v1 >> v2;
    while (v1 < v2) {
        std::cout << v1 << std::endl;
        ++v1;
    }
}
0

I also came up with a solution based on my C++ knowledge (and two simple if statements) up to the point of that task in the book.

Write a program that prompts the user for two integers. Print each number in the range specified by those two integers:

#include <iostream>

int main()
{
    std::cout << "Enter two numbers: " << std::endl;

    int n1 = 0, n2 = 0;

    std::cin >> n1 >> n2;

    if (n1 == n2)
    {
        std::cout << "No integers in between the numbers" << std::endl;
    }
    else
    {
        while (n1 != n2)
        {
            if (n1 > n2)
            {
                std::cout << n1 << std::endl;
                n1--;
            }
            else if (n1 < n2)
            {
                std::cout << n1 << std::endl;
                n1++;
            }
        }
        std::cout << n2 << std::endl;
    }

    return 0;
}

Peace!

0

This could work for cases where the user can input any value for the range, and if you're only allowed while loops and boolean logic. Introduce a "Done" variable, and set to 1 if you enter the first while loop, which would skip the second while loop:

#include <iostream>
int main(){
    int first, second, done = 0;
    std::cout << "Input two numbers: " << std::endl;
    std::cin >> first >> second;

    while (first <= second)
    {
        std::cout << first << std::endl;
        first++;
        done = 1;
    }
    
    while (first >= second && !done)
    {
        std::cout << first << std::endl;
        first--;
    }
    return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.