Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C++ Given a sequence of positive numbers, we can know whether the values of these numbers becomes larger and larger (including the same value), or
C++
Given a sequence of positive numbers, we can know whether the values of these numbers becomes larger and larger (including the same value), or smaller and smaller (including the same value), or change the direction. For instance, given the sequence (1, 2, 3, 3), we know that the sequence goes up or stay unchanged without changing the direction (that is, we count that there is 1 up-segment); given the sequence (8, 4, 2, 1, 0), we know the sequence goes down without changing the direction (that is, there is 1 down-segment); and given the sequence (3, 44, 0, 34, 3, 4, 5, 31, 40), we can find out that 3->44 goes up, then 44->0 goes down, and then 0->34 goes up, and then 34->3 goes down, and finally 3->4->5->31->40 goes up. So, for these three exemplified sequences, there are 1 up- segment, 1 down-segment, and 3 up-segments plus 2 down-segments, respectively. Write a program to accept a sequence of positive number ended with the value of -1, and the program will output the total number of up-segments and total number of down-segments in the inputted sequence of positive numbers. Sample 1 Input a sequence of numbers (ended with -1): -1 No. of Up Movement is: 0 No. of Down Movement is: 0 Sample 2 Input a sequence of numbers (ended with -1): 1 2 3 4 3 2 1 - 1 No. of Up Movement is: 1 No. of Down Movement is: 1 Sample 3 Input a sequence of numbers (ended with -1): 3 4 4 2 9 6 8 4 -1 No. of Up Movement is: 3 No. of Down Movement is: 3 Sample 4 Input a sequence of numbers (ended with -1): 1 3 4 6 8 0 9 7 6 5 4 3 27 8 9 0 6 5 4 3 2 0 No. of Up Movement is: 8 No. of Down Movement is: 7 6 5 4 3 6 4 3 2 3 4 - 1 Sample 5 Input a sequence of numbers (ended with -1): 1 1 1 4 4 4 -1 No. of Up Movement is: 1 No. of Down Movement is: 6Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started