Home > Mobile >  How to process list of numbers(arrays) in C#
How to process list of numbers(arrays) in C#

Time:12-10

I have a list of multiple consecutive numbers. I am trying to figure it out how to know the number of increase or decrease in relation to to past value . For example

102, 201, 198, 200

That is 2 increase (102, 201 and 198, 200) and 1 decrease (201, 198). It is a long list of number so manual is tedious. I am a beginner using C#.

CodePudding user response:

There are many ways, from querying with help of Linq:

  using System.Linq;

  ...

  int[] source = new int[] { 102, 201, 198, 200 }; 

  ...

  // Probably, the most generic approach
  var result = source.Aggregate(
    (Inc: 0, Dec: 0, prior: (int?)null),
    (s, a) => (s.Inc   (s.prior < a ? 1 : 0), s.Dec   (s.prior > a ? 1 : 0), a));

  Console.Write($"Increasing: {result.Inc}; decreasing: {result.Dec}");

up to good old for loop:

  int Inc = 0;
  int Dec = 0;

  // Probably, the easiest to understand solution
  for (int i = 1; i < source.Length;   i)
    if (source[i - 1] > source[i])
      Dec  = 1;
    else if (source[i - 1] < source[i])
      Inc  = 1;  

  Console.Write($"Increasing: {Inc}; decreasing: {Dec}");

Edit: Linq Aggregate explained.

  Aggregate(
    (Inc: 0, Dec: 0, prior: (int?)null),
    (s, a) => (s.Inc   (s.prior < a ? 1 : 0), s.Dec   (s.prior > a ? 1 : 0), a));

In order to obtain single value from a cursor, we use Aggregate.

First argument

  (Inc: 0, Dec: 0, prior: (int?)null)

is the initial value (named tuple, to combine several properties in one instance). Here we have 0 increasing and decreasing and null for the previous item.

Second argument

  (s, a) => (s.Inc   (s.prior < a ? 1 : 0), s.Dec   (s.prior > a ? 1 : 0), a)

Is a rule how to add a next item a to aggregated items s. We should

  1. Increment s.Inc in case prior item is smaller than current a: s.Inc (s.prior < a ? 1 : 0)
  2. Increment s.Dec in case prior item is bigger than current a: s.Dec (s.prior > a ? 1 : 0)
  3. We should assign current item a as the next prior element.

Let's put it a bit wordy but I hope more readable:

.Aggregate(
   (Inc: 0,           // no increasing 
    Dec: 0,           // no decreasing
    prior: (int?)null // no prior item
   ),
   (s, a) => ( // s - partial aggregation, a - to add item
      Inc:   s.Inc   (s.prior < a ? 1 : 0), // Increment Inc if required
      Dec:   s.Dec   (s.prior > a ? 1 : 0), // Increment Dec if required 
      prior: a                              // Set a as a prior  
   )
 )

Hope, now it's clearer what's going under the hood of Aggregate

CodePudding user response:

    int[] source = new int[] { 102, 201, 198, 200 };
    int Increment = 0;
    int Decrement= 0;
    int k;
    for (k=1; k< array.length; k  )
    if (array[k - 1] > array[k])
      Decrement  ;
    else 
    {
    if (array[k - 1] < array[k])
      Increment  ;  
    }

  Console.Write("Increasing: {Increment}, decreasing: {Decrement}");
   }

CodePudding user response:

In this situation you can use a for loop:

for (int i = 0; i < listOfNumbers.Length; i  )
{
    int currentEntry = listOfNumbers [i];
    if(i > 0){
        int previousEntry = listOfNumbers [i - 1];
        Console.Log("Change from previous : "   (currentEntry-previousEntry));
    }else{
        Console.Log("No previous entry so no change can be found.");
    }
}
  • Related