need help

can anyone help me with this question plz?

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.


Example 1:
Input a sequence of numbers (ended with -1):
-1
No. of Up Movement is: 0
No. of Down Movement is: 0

Example 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

Example 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

Example 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 8 7 6 5 4 3 6 6 6 4 3 2 3 4 -1
No. of Up Movement is: 8
No. of Down Movement is: 7
Your examples are wrong
1. Store the numbers in a vector<int> container. Why vector? Because the standard guarantees that the elements of a vector are stored contiguously in memory, a feature that will be used in checking up and down segments
2. Initialize the vector with the required numbers - I have used an initialization list, but a loop with push_back() can also be used
3. Declare an iterator to traverse the vector
4. Check contiguous vector elements for up or down segments using the feature that contigous elements are also contiguous in memory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<vector>
using namespace std;

int main(){
vector<int> v {1, 3, 4, 6, 8, 0, 9, 7, 6, 5, 4, 3, 27, 8, 9, 0, 6, 5, 4, 3, 2, 0, 8, 7, 6, 5, 4, 3, 6, 6, 6, 4, 3, 2, 3, 4, -1};
int up{}, down{};
for (vector<int>::iterator itr = v.begin()+1; itr!= v.end(); ++itr){
    if(*(itr-1)<*(itr)){
        up++;
    }
    if(*(itr-1)>*(itr)){
       down++;}
}
cout<<"Up Segments: "<<up<<"\t Down Segments: "<<down<<endl;
}


PS: OP: @SamuelAdams is right, your count of up and down segments are not correct
Topic archived. No new replies allowed.