question

lastcoder avatar image
0 Votes"
lastcoder asked ·

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

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


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

dotnet-csharp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered ·

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.

· 4 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sorry the first number was negative

0 Votes 0 ·

Can you convert this to use List<int>?

0 Votes 0 ·

I edited the question. THanks!

0 Votes 0 ·

@lastcoder
Only minor changes are required,please check.

0 Votes 0 ·