Using C# How can we check to see if a List of numbers has decreasing numbers?

lastcoder 1 Reputation point
2021-03-07T23:22:10.833+00:00

True: 1 , -2 , -3 ,-4
True: -3,-15,-20,-30

False ( this has a positive number) -4,2,-5,-7,-9....

Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-03-08T01:47:31.447+00:00

    According to your description, the following code might be useful:

            public static bool IsTrue(int[] array)   
            {  
                bool re = true;  
                for (int i = 0; i < array.Length-1; i++)  
                {  
                    if (array[i] < array[i + 1])   
                    {  
                        re = false;  
                        break;  
                    }  
                }  
                return re;  
            }  
    

    But the third example also seems to be True, which is decreasing.

    Update:

            public static bool IsTrue(List<int> array)   
            {  
                bool re = true;  
                for (int i = 0; i < array.Count-1; i++)  
                {  
                    if (array[i] < array[i + 1])   
                    {  
                        re = false;  
                        break;  
                    }  
                }  
                return re;  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.