Given a one-dimensional array, check if the elements in the array are in ascending / descending order

Write your question here.
# include <iostream>
# include <algorithm>
using namespace std;
int main()
{
int n[5] {74, 67,54, 42, 25};
int i;
for (i = 0; i < 1; i++)
if (n[0] < n[1] < n[2] < n[3] < n[4])
{
cout << "element are in ascending order"<< endl;
}
else if (n[0] > n[1] > n[2] > n[3] > n[4])
{
cout << "element are in descending order"<<endl;
}
system("pause");
return 0;
}

cout i display ascending order if it is in ascending order but if it is in descending order cout will still display ascending order instead of descending order

Last edited on
Hello jidris,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Although this would be nice if it worked, if (n[0] < n[1] < n[2] < n[3] < n[4]), but it does not. You need something more like: if (n[0] < n[1] && n[1] < n[2] && n[2] < n[3]).

The else if statement would be similar, but would be slightly different in it comparisons.

Next thing to consider int n[100] . Do you really want to write an if statement to check all 100 elements?

This is where the for loop would be useful. With your code the for loop is not necessary.

You may want to consider an else statement to catch anything that does not match the if or the else if.

Andy
if (n[0] < n[1] < n[2] < n[3] < n[4])
This doesn't do what you think it does. It's one of the maddening things about C++ where something that makes sense intuitive sense compiles but does something other than the intuitive interpretation.

What it actually does is:
if ((((n[0] < n[1]) < n[2]) < n[3]) < n[4])
So first it evaluates n[0] < n[1]. The result is a boolean value (true or false)
Next it evaluates (bool) < n[2]). To do this, it converts the bool to an int value of 1 or 0. The result of the comparison is another bool.
and so on and so on.

So it's equivalent to:
1
2
3
4
int x = (n[0] < n[1]);    // x will get value 1 or 0
int y = (x < n[2]);
int z = (y < n[3]);
if (z < n[4]) ...


What need to explicitly "and" together the conditions:
if (n[0] < n[1] && n[1] < n[2] && n[2] < n[3] && n[3] < n[4])

But the code is still not quite right.
- What's the point of the for loop if you check explicitly check the elements of the array? Either check with a loop, in which case each pass should check just two items, or check everything at once.
- What if the elements are in neither ascending nor descending order? You won't print anything.
- What if two of the elements are equal? Are 1,2,2,3 in ascending order? Strictly speaking they are not, but reread the assignment to be certain you're printing what's required.
do you need to be aware of equality? Depending on the question specifics, 3,3,3 is in sorted order, its in ascending order, and its in descending order all three, though some problems may not allow duplicates or accept that so you need to know how to respond if you see this.

Hello jidris,

Working on your program I noticed that your program includes the "<algorithm>" header file. I am not sure what you intended to us here, but there is a function "std::is_sorted()" that you can make use of.

a simple:
1
2
3
4
5
6
if(std::is_sorted(...))
    // <--- For ascending order
else if(std::is_sorted(...))
    // <--- For descending order.
else
    // <--- For anything that is not ascending or descending. 

If you want you can precede this with a for loop to output the numbers in the array.

Andy
thank you so much for all your support, the challenges am facing right in programming is my compiler, please can someone describe a good compiler for me,and secondly i haven't understand how to paste my code on this platform by using code tag.
Hello jidris,


i haven't understand how to paste my code on this platform by using code tag.


Check out the second link in my first reply.

Press the <> button to the right and then paste your code in between.

I would say that the 2 most often used IDEs are MSVS and Code::Blocks. VS uses the clang compiler and most of the other compilers use the MinGW compiler. Both are good, but still different. Probably due to the way Microsoft likes to be different.

Andy
As was said b4 you could check to see if its sorted then loop through until
 
n[x]!=n[x+1]


Then just determine which one is bigger depending on which one is larger will obviously tell you what the order is. If you get to the end and the condition never happens then you know its both or neither depending on your perspective.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <algorithm>
#include <functional>

int main()
{
	const size_t nos {5};
	//const int n[nos] {74, 67, 54, 42, 25};
	//const int n[nos] {1, 2, 3, 4, 5};
	const int n[nos] {1, 2, 6, 4, 5};

	if (std::is_sorted(n, n + nos))
		std::cout << "Elements are in ascending order\n";
	else
		if (std::is_sorted(n, n + nos, std::greater<int>()))
			std::cout << "Elements are in descending order\n";
		else
			std::cout << "Numbers are not sorted\n";
}

Last edited on
Is the sequence { 1, 1, 1, 1, 1 } in ascending order, descending order, or generally standing on the fence?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int strictlyOrdered( int *A, int n )
{ 
   if ( n <= 1 ) return 0;
   int differ = ( A[1] > A[0] ) - ( A[1] < A[0] );
   for ( int i = 1; i < n - 1; i++ ) if ( differ * ( A[i+1] - A[i] ) <= 0 ) return 0;
   return differ;
}

int main()
{
   const char * outcome[] = { "Descending", "Neither", "Ascending" };
   int A[] = { 74, 67, 54, 42, 25 };
   int N = sizeof A / sizeof A[0];
   cout << outcome[strictlyOrdered( A, N ) + 1];
}
Topic archived. No new replies allowed.