Growth ranges of an array’s values

Asked

Viewed 196 times

2

I’d like to know a way to determine growth intervals and decreasing of a vector in C#.

Example:

Array[0]=0; Array[1]=1; Array[2]=2;

Array[3]=3; Array[4]=4; Array[5]=3;

Array[6]=2; Array[7]=1; Array[8]=0;

Array[9]=1; Array[10]=2;

Growth ranges: 0-4, 8-10

Decreasing intervals: 4-8

EDIT:

From position 0 to 4 and from position 8 to 10 of the example vector the values are growing and from position 4 to 8 the values are decreasing.

  • 1

    I couldn’t understand your question. Could you please explain to me? :/

  • I’ll edit here friend

  • You want to generate random numbers to insert into the array, but when it is index 0 to 4 or 8 to 10 it has to be in ascending order, but when it is index 4 to 8 you have to enter in descending order?

  • no, I already have the vector with the filled positions, I want to find the intervals where the values are going up or down.

  • I set up a code like CONSOLE Application fast, I will provide to have an idea of what you want... let’s see if the administrators here do not get angry with me... I want to earn points here, but the JAVA people do not let... KKK!

  • And what is the criterion?

  • From position 5 to position 8 is decreasing...

Show 2 more comments

3 answers

2


Seems to be ok:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace ConsoleApp2
{
    class Interval
    {
        public int Min { get; set; }
        public int Max { get; set; }

        public Interval(List<int> list, int current)
        {
            list.Add(current);

            this.Min = list.Min();
            this.Max = list.Max();
        }

        public override string ToString()
        {
            return string.Format("{0}-{1}", Min, Max);
        }
    }

    class Program
    {        
        static void Main()
        {
            var intArr = new int[] { 0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2 };

            var listAux = new List<int>();

            bool? isCres = null;

            var intervalCres = new List<Interval>();
            var intervalDecres = new List<Interval>();

            for (int i = 0; i < intArr.Length - 1; i++)
            {
                if (intArr[i] < intArr[i + 1])
                {
                    if (isCres == false)
                    {
                        intervalDecres.Add(new Interval(listAux, i));
                        listAux.Clear();
                    }
                    isCres = true;
                    listAux.Add(i);
                }
                else
                {
                    if (isCres == true)
                    {
                        intervalCres.Add(new Interval(listAux, i));
                        listAux.Clear();
                    }
                    isCres = false;
                    listAux.Add(i);
                }
            }

            if ((bool)isCres) intervalCres.Add(new Interval(listAux, intArr.Length - 1));
            else intervalDecres.Add(new Interval(listAux, intArr.Length - 1));

            Console.WriteLine("Crescimento:");
            intervalCres.ForEach(x => Console.WriteLine(x));

            Console.WriteLine("\nDecrescimento:");
            intervalDecres.ForEach(x => Console.WriteLine(x));

            Console.ReadKey();
        }
    }
}

Output:

Growth: 0-4 8-10

Decrease: 4-8

2

Code example:

class Program
{
    static void Main(string[] args)
    {
        int[] intNum = new int[11];

        intNum[0] = 0;
        intNum[1] = 1;
        intNum[2] = 2;
        intNum[3] = 3;
        intNum[4] = 4;
        intNum[5] = 3;
        intNum[6] = 2;
        intNum[7] = 1;
        intNum[8] = 0;
        intNum[9] = 1;
        intNum[10] = 2;

        for (int i = 1; i < intNum.Length; i++)
        {
            if (intNum[i - 1] < intNum[i])
            {
                Console.WriteLine("Cresceu");
            }
            else
            {
                Console.WriteLine("Decresceu");
            }
        }

        Console.ReadKey();
    }
}

1

See also on Ideone.

The example below shows the start and end indices of each interval and the sign of the first derivative (+1 if positive, 0 if constant, and -1 if negative).

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        int[] arr = { 0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2 };

        Console.WriteLine("arr");
        foreach (var intervalo in arr.IntervalosCrescimento())
        {
            Console.WriteLine($"Start {intervalo.Item1}, end {intervalo.Item2}, sinal {intervalo.Item3}");
        }

        int[] arr2 = { 0, 1, 2, 3, 3, 3, 4, 5, 6, -1, -8, 0, 8 };

        Console.WriteLine();
        Console.WriteLine("arr2");
        foreach (var intervalo in arr2.IntervalosCrescimento())
        {
            Console.WriteLine($"Start {intervalo.Item1}, end {intervalo.Item2}, sinal {intervalo.Item3}");
        }
    }
}

public static class ExtensionMethod
{
    public static IEnumerable<Tuple<int, int, int>> IntervalosCrescimento<T>(this IEnumerable<T> en) where T : IComparable
    {
        bool first = true;

        Tuple<int, int, int> result;

        int start = 0, end = 0;

        T previous = default(T);

        int? previousSign = null;
        int? sign = null;

        int index = 0;

        foreach (T current in en)
        {
            if (first)
            {
                first = false;
                start = index;
                previous = current;
            }
            else
            {
                sign = current.CompareTo(previous);

                if (previousSign == null)
                {
                    previousSign = sign;
                }
                else if (previousSign != sign)
                {
                    end = index;

                    result = Tuple.Create(start, end, previousSign.Value);

                    start = index;
                    previousSign = sign;

                    yield return result;
                }

                previous = current;
                ++index;
            }
        }

        end = index;

        if (start < end)
            yield return Tuple.Create(start, end, sign.Value);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.