Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
96 views
in Technique[技术] by (71.8m points)

c++ - Problems with a program to get every number be greater or smaller than the numbers before and after itself

I want to write a program that get the numbers with this rule : every number be greater or smaller than the numbers before and after itself. like : 3 1 4 2 6 0 8 3 5 16

Whenever this rule was violated, stop getting number.

int a, b, c;
bool flag = true;
cin >> a;

while (flag)
{
    cin >> b;
    cin >> c;

    if ((b < a && b < c) || (b > a && b > c))
    {
        flag = true;
        a = c;
    }
    else
    {
        break;
    }
}

My code works for some inputs but for this inputs : 3 1 4 6 When i enter 6 the program must be stop, but it continue to input next number. What should i do to fix it?

question from:https://stackoverflow.com/questions/65914489/problems-with-a-program-to-get-every-number-be-greater-or-smaller-than-the-numbe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The solution to this problems involves a lot of logical evaluations. So, we need many boolean expressions and if statements.

One key to the solution, is to keep track of 2 values:

  • The current read value
  • The preivously read, old value

We can always compare those values and then make descisions. Problem is that we do not have an "previous" value in the beginning. So, we need to do a special treatment and first read a value from the user, store this as prvious value, and then always read a current value in a loop.

At the end of the loop, we will assign the current value to the "previuosValue". Then in the next loop run, we always need to read only the current value from the user.

Ant those 2 values, we can compare in a while loop.

We compare the current value with the previous value, and, depending of the outcome, define a "direction" flag for further comparisons.

This we do after having read the 2nd number. After that the direction is always defined and will never change.


Example, if the current value is bigger than the previous value, then, in the next loop, the next value must be smaller. And vice versa.

Example:

  • First value: 2
  • 2nd value: 6

The second value is bigger than the first value. So, for next values we expect

small --> big --> small --> big --> small --> big --> . . .

and so on. This will never change.


Same is valid vice versa.

  • First value: 9
  • 2nd value: 1

The second value is smaller than the first value. So, for next values we expect

big --> small --> big --> small --> big --> small --> big --> . . .


The direction flag will always be inverted after having processed the "next" number.

We can then evaluate the stop condition in the next loop run. Does the comparision result to a value, to a direction, that we expect?

If not, or if the values are equal, then we stop the input.

Of course, we will not do this evaluation in the first loop, because then, we have always a valid pair and calculate the direction afterwards.

So, you see. We always need only 2 variables.

There are many possible implementations, as always. Please see the below as an example for a solution:

#include <iostream>

int main() {

    // Read initial previous number (The first number)
    if (int previousNumber{}; std::cin >> previousNumber) {

        // Flag that indicates, if we should continue reading new numbers or not
        bool continueToRead{ true };
        // First number needs special treatment, there is no other number
        bool firstCheck{ true };

        // The "direction" of the comparison
        bool nextNumberMustBeSmaller{false};

        // Read numbers in a loop
        while (continueToRead) {

            // Read current (next) number
            if (int currentNumber{}; std::cin >> currentNumber) {

                // After heaving read the first value in the loop, we can detect the direction
                if (firstCheck) {
                    // Get the "direction" of the comparison for the next numbers
                    // If the number is bigger than last number
                    if (currentNumber > previousNumber)

                        // Then next value muste be smaller
                        nextNumberMustBeSmaller = true;

                    // If this number is smaller
                    else if (currentNumber < previousNumber)

                        // then next number must be bigger
                        nextNumberMustBeSmaller = false;
                    else
                        continueToRead = false;

                    // First check has been done
                    firstCheck = false;
                }
                else {
                    
                    // Find out the stop condition
                    if (
                        // Direction is smaller but number is bigger or
                        (nextNumberMustBeSmaller and (currentNumber > previousNumber)) ||
                        // Direction is bigger but number is smaller or
                        (not nextNumberMustBeSmaller and (currentNumber < previousNumber)) ||
                        // Or numbers are equal
                        (currentNumber == previousNumber)) {

                        // Then: Stop reading values
                        continueToRead = false;
                    }
                    nextNumberMustBeSmaller = not nextNumberMustBeSmaller;
                }

                // Remember the last value. So, for the next loop rund, the current value will become the previous one
                previousNumber = currentNumber;
            }
            else {
                std::cerr << "

Invalid input

";
                continueToRead = false;
            }

        }
    }
    else std::cerr << "

Invalid input

";

    return 0;
}

To be compiled with C++17 enabled.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...