Method that takes 3 integer values per parameter and returns them ordered in ascending order as an array

Asked

Viewed 567 times

2

I can’t get the array filled returned be printed

class Program
{

    static void Main(string[] args)
    {
        int valor, valor2, valor3;

        Maior maior = new Maior();



        Console.WriteLine("Digite um numero");
        valor = Int32.Parse(Console.ReadLine());

        Console.WriteLine("Digite um numero");
        valor2 = Int32.Parse(Console.ReadLine());

        Console.WriteLine("Digite um numero");
        valor3 = Int32.Parse(Console.ReadLine());


        int[] maiorvalor = maior.Maior1(valor, valor2, valor3);


        Console.WriteLine(maiorvalor[2]);
        Console.WriteLine(maiorvalor[1]);

        Console.ReadLine();
    }

   }
}



class Maior
{
    public int[] Maior1 (int valor, int valor2, int valor3)
    {
        int[] array = new int[3];


        if (valor > valor2 && valor > valor3)
        {
            array[0] = valor;

        }

        if (valor2 > valor && valor2 > valor3)
        {
            array[1] = valor2;

        }

        if (valor3 > valor && valor3 > valor2)
        {
            array[2] = valor3;

        }


        return array;


    }
}

2 answers

2

For only 3 data, despite more code, the most performatic would be like this:

using static System.Console;

public class Program {
    public static void Main() {
        foreach (var item in Maior1(2, 3, 1)) WriteLine(item);
        WriteLine();
        foreach (var item in Maior1(1, 2, 3)) WriteLine(item);
        WriteLine();
        foreach (var item in Maior1(3, 2, 1)) WriteLine(item);
        WriteLine();
        foreach (var item in Maior1(1, 3, 2)) WriteLine(item);
        WriteLine();
        foreach (var item in Maior1(2, 1, 3)) WriteLine(item);
        WriteLine();
        foreach (var item in Maior1(3, 1, 2)) WriteLine(item);
    }
    public static int[] Maior1(int v1, int v2, int v3) {
        var array = new int[3];
        if (v1 > v2 && v1 > v3) {
            array[0] = v1;
            if (v2 > v3) {
                array[1] = v2;
                array[2] = v3;
            } else {
                array[1] = v3;
                array[2] = v2;
            }
        } else if (v2 > v1 && v2 > v3) {
            array[0] = v2;
            if (v1 > v3) {
                array[1] = v1;
                array[2] = v3;
            } else {
                array[1] = v3;
                array[2] = v1;
            }
        } else {
            array[0] = v3;
            if (v1 > v2) {
                array[1] = v1;
                array[2] = v2;
            } else {
                array[1] = v2;
                array[2] = v1;
            }
        }
        return array;
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

But the biggest reason I answered is to avoid a list allocation without any need.

Besides don’t use the Parse() where you used, the correct is the TryParse(). See how it would be.

0


His method Maior1 does not cover all possibility. Best output is to use a list sorts-there and devoler the array:

public int[] Maior1 (int valor, int valor2, int valor3)
    {
        List<int> lista = new List<int>(new int[]{valor, valor2, valor3});

        lista.Sort();

        return lista.ToArray();
    }
}

Browser other questions tagged

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