Monotonous

The string of numbers ends with the value 0, where 0 is not part of the heights (only marks the end of the string).
Specify whether the range of heights is ascending, descending, or non-monotonous.
Input Output
1 2 5 5 10 11 0 ascending
16 7 3 0 descending
1 2 2 1 0 non-monotonous

Solving as much as possible with while and if.
Restrictions and clarifications:
- 0 is not part of the string of heights, only marks the end of the string
-heights are numbers between 0 and 10,000
What is the c++ question? What have you attempted so far? Post your existing code so that we can provide guidance.
The question is: how can I reach the results that the problem demands?
This is my code, but is totally wrong.

#include <iostream>


using namespace std;


int main() {
int x, n, i = 0;
cin >> x >> n;
while (n != 0) {
cin >> n;
}
if (x > n) {
cout << "ascending";
} else if (x < n) {
cout << "descending";
} else {
"non-monotonous";
}

}
Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <vector>

enum Classify {asc, desc, non};

const char* text[] {"ascending", "descending", "non-monotonic"};

std::vector<int> getnums(const std::string& s)
{
	std::istringstream iss(s);
	std::vector<int> vi;

	for (int i; (iss >> i) && (i != 0); vi.push_back(i));

	return vi;
}

Classify doClassify(const std::vector<int>& vi)
{
	if (std::is_sorted(vi.begin(), vi.end()))
		return asc;

	if (std::is_sorted(vi.rbegin(), vi.rend()))
		return desc;

	return non;
}

int main()
{
	const std::string s1 {"1 2 5 5 10 11 0"};
	const std::string s2 {"16 7 3 0"};
	const std::string s3 {"1 2 2 1 0"};

	std::cout << s1 << "  " << text[doClassify(getnums(s1))] << '\n';
	std::cout << s2 << "  " << text[doClassify(getnums(s2))] << '\n';
	std::cout << s3 << "  " << text[doClassify(getnums(s3))] << '\n';
}



1 2 5 5 10 11 0  ascending
16 7 3 0  descending
1 2 2 1 0  non-monotonic

Last edited on
I believe we usually say "monotonic" instead of "monotonous". :-)
Last edited on
:) :) I thought it didn't seem to read right....... Text changed in code above.

Last edited on
What would the string "1 1 0" be?
There's no need to sort the data or even store all of it. Just keep track of the previous number and whether the current number is higher or lower.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using std::cin;
using std::cout;

int main()
{
    unsigned n, last;

    while (cin >> last) {		// get first number if this set
	unsigned flags = 0;
	constexpr unsigned Increasing = 1;
	constexpr unsigned Decreasing = 2;

	while (cin >> n) {
	    if (n == 0) break;	// end of input
	    if (n > last) {
		flags |= Increasing; // we saw it going up
	    } else if (n < last) {
		flags |= Decreasing; // we saw it going down
	    }
	    last = n;
	}

	const char *classification[4] = {
					 "even",
					 "ascending",
					 "descending",
					 "non-monotonic"
	};
	cout << classification[flags] << '\n';
    }
}


Input:
1 2 5 5 10 11 0
16 7 3 0
1 2 2 1 0
3 3 3 3 0
9 0

Output:
ascending
descending
non-monotonic
even
even

Topic archived. No new replies allowed.