Obtain the smallest and largest number of data in an array in C #

3

I know that for some this may be a question too easy, but I'm just starting with this programming. The exercise is about obtaining the highest and lowest value of 15 data. My problem is that I can not get the minimum value of the whole arrangement and I need your help to know what my error is, this is the code:

 static void Main(string[] args)
    {
        int[] ventas = new int[15];
        int v, max = ventas[0], min = ventas[0];
        for(int i = 0; i < 15; i++)
        {
            Console.Write("ingresos en ventas: ");
            v = int.Parse(Console.ReadLine());
            ventas[i] = v;
        }
        for (int i = 0; i < 15; i++)
        {
            if (ventas[i] > max)

                max = ventas[i];


            else if (ventas[i] < min)

                min = ventas[i];
        }   

        Console.Write("\n\nLa mayor venta es: " + max);
        Console.Write("\n\nLa menor venta es: " + min);
        Console.ReadKey();

    }

The output in cosola only shows the highest value, but with the lowest it happens that it is always 0.

Thanks in advance for your answers.

    
asked by Larjos 13.04.2018 в 02:28
source

2 answers

2

One way is using Linq:

  • var menorDeTodos = ventas.Min(); : find the minimum value.
  • var mayorDeTodos = ventas.Max(); : you will find the maximum value.
  • Your code would look something like this:

    static void Main(string[] args)
    {
        int[] ventas = new int[15];
        int v, max = ventas[0], min = ventas[0];
        for(int i = 0; i < 15; i++)
        {
            Console.Write("ingresos en ventas: ");
            v = int.Parse(Console.ReadLine());
            ventas[i] = v;
        }
    
        Console.Write("\n\nLa mayor venta es: " + ventas.Max());
        Console.Write("\n\nLa menor venta es: " + ventas.Min());
        Console.ReadKey();
    }
    

    For this it is necessary that you make reference to the library of Linq using System.Linq;

        
    answered by 13.04.2018 / 06:55
    source
    1

    Although they have given you an answer, I am going to tell you why your code fails.

    Your problem is that you assign the variables min and max the values before reading the data, so min and max are initialized to 0

    If you change your code a bit, it will work.

    static void Main(string[] args)
        {
            int[] ventas = new int[15];
            // int v, max = ventas[0], min = ventas[0]; VENTAS[0] ESTA SIN INICIALIZAR y tienen el valor 0
    int v;
            for(int i = 0; i < 15; i++)
            {
                Console.Write("ingresos en ventas: ");
                v = int.Parse(Console.ReadLine());
                ventas[i] = v;
            }
            int  max = ventas[0], min = ventas[0];
            for (int i = 0; i < 15; i++)
            {
                if (ventas[i] > max)
    
                    max = ventas[i];
    
    
                else if (ventas[i] < min)
    
                    min = ventas[i];
            }   
    
            Console.Write("\n\nLa mayor venta es: " + max);
            Console.Write("\n\nLa menor venta es: " + min);
            Console.ReadKey();
    
        }
    
        
    answered by 13.04.2018 в 09:03