Little problem



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
  #include <iostream>

using namespace std;

int main()
{
	int a,b,ascend=1,descend=1,mono=1;
	cin>>a;
	while (a!=0){
		cin>>b;
		if (b==0)
		   a=0;
		if (a<b && descend==1)
			descend=0;
		else if (a>b && ascend==1)
			   ascend=0;
		     else if (ascend==0 && descend==0)
			        cout<<"non-monotonic";
			        //else mono=0;
		a=b;

	}
	if (ascend==1)
	cout<<"ascending";
	if (descend==1)
	cout<<"descending";
	return 0;
}

My code : input 0 => output: ascendingdescending , input: 1 1 1 1 0 => output:ascendingdescending. wrong

input 0 => output: non-monotonic and
input: 1 1 1 1 0 , (when the elements in the string are equal) output: non-monotonic.
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
#include <iostream>

int main()
{
    bool strictly_ascending = true ; 
    bool ascending = true ;
    bool strictly_descending = true ;
    bool descending = true ;
    
    int first ;
    std::cin >> first ;
    int second ;
    while( std::cin >> second )
    {
        if( second > first ) strictly_descending = descending = false ; // eg. 5 7
        if( second < first ) strictly_ascending = ascending = false ; // eg.5 3
        else strictly_ascending = strictly_descending = false ; // eg. 5 5
        
        first = second ;
    }
    
    const bool strictly_monotonic == strictly_ascending | strictly_descending ;
    const bool monotonic == ascending | descending ;
     
    // TO DO: print result
}

Thanks, but your code is too complicated. I must resolve with while, if, else if.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;

int main()
{
   string outcomes[] = { "Non-monotonic", "Descending", "Flat", "Ascending" };
   int a, b, state = 0;
   cin >> a;
   while ( cin >> b && b != 0 )
   {
      int test = ( b > a ) - ( b < a );
      if ( state == 0 ) state = test;
      else if ( state * test < 0 )
      {
         state = -2;
         break;
      }
      a = b;
   }
   cout << outcomes[state+2] << '\n';
}


2 2 2 0
Flat

4 4 5 5 6 0
Ascending

7 6 2 2 1 0
Descending

1 2 3 2 1 0
Non-monotonic
Thanks, but your code is too complicated. I must resolve with while, if, else if.


??? That is simple code. It only uses the allowed while, if/else, conditions and assignments. Nothing complicated and doesn't use any of the STL algorithms.

Why is it too complicated?
> Why is it too complicated?
"too complicated" == "I can't hand that in as an answer without it being obviously written by someone else".
:) :) :)
Well for simplicity, 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
#include <iostream>

int main()
{
	int res {}, no1 {}, no2 {};

	std::cin >> no1 >> no2;

	if (no1 > no2)
		res = 2;
	else
		if (no1 < no2)
			res = 1;

	no1 = no2;

	while ((std::cin >> no2) && (no2 != 0)) {
		if (res != 0)
			if ((res == 1) && (no2 < no1))
				res = 0;
			else
				if ((res == 2) && (no2 > no1))
					res = 0;
		no1 = no2;
	}

	if (res == 1)
		std::cout << "ascending\n";
	else
		if (res == 2)
			std::cout << "descending\n";
		else
			std::cout << "neither!\n";
}



1 2 3 4 5 6 0
ascending

6 5 4 3 2 1 0
descending

1 1 1 0
neither!

Is good, but 1 1 1 2 0 => ascending but in this exemple is neither or 9 9 8 0 same.
Neither should only display if no1 is 0 or when the elements in the string are equal.
Last edited on
OK. Try:

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
#include <iostream>

int main()
{
	int res {}, no1 {}, no2 {};

	std::cin >> no1 >> no2;

	if (no1 > no2)
		res = 2;
	else
		if (no1 < no2)
			res = 1;

	no1 = no2;

	while ((std::cin >> no2) && (no2 != 0)) {
		if (no1 < no2)
			if (res == 0 || res == 1)
				res = 1;
			else
				res = 3;

		if (no1 > no2)
			if (res == 0 || res == 2)
				res = 2;
			else
				res = 3;
		no1 = no2;
	}

	if (res == 1)
		std::cout << "ascending\n";
	else
		if (res == 2)
			std::cout << "descending\n";
		else
			std::cout << "neither!\n";
}



1 2 3 0
ascending

5 4 3 2 0
descending

1 1 1 2 0
ascending

5 5 3 3 1 0
descending

1 1 1 0
neither!

Thanks!
Topic archived. No new replies allowed.