Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solución Reto #34 C# #693

Open
BrunoMarijanovic opened this issue Aug 27, 2022 · 0 comments
Open

Solución Reto #34 C# #693

BrunoMarijanovic opened this issue Aug 27, 2022 · 0 comments

Comments

@BrunoMarijanovic
Copy link

using System;

namespace Reto_34
{
class Program
{
/*
* Enunciado: Dado un array de enteros ordenado y sin repetidos,
* crea una función que calcule y retorne todos los que faltan entre
* el mayor y el menor.
* - Lanza un error si el array de entrada no es correcto.
*/
static void Main(string[] args)
{
int[] numeros = new int[] {0, 3, 8, 9, 20};
int[] numFaltan;
bool correcto;

        correcto = ComprovarArrayCorrecta(numeros);

        if (correcto)
        {
            numFaltan = NumerosQueFaltan(numeros);

            Console.Write("LISTADO DE NÚMEROS -> ");
            ImprimirArray(numeros);

            Console.Write("\nFALTAN ESTOS NÚMEROS -> ");
            ImprimirArray(numFaltan);
        }
        else
        {
            Console.Write("LISTADO DE NÚMEROS INCORRECTO -> ");
            ImprimirArray(numeros);
        }
    }

    /// <summary>
    /// Comprueva que los números enteros de la array estan ordenados (acendente) y no
    /// existen valores repetidos.
    /// Si cumple con estas condiciones volverà el valor TRUE.
    /// </summary>
    /// <param name="numeros"></param>
    public static bool ComprovarArrayCorrecta(int[] numeros)
    {
        bool correcto = false;
        int numAnterior, numActual;
        int i = 1;
        
        if(numeros.Length > 1)
        {
            numAnterior = numeros[0];
            numActual = numeros[1];
            correcto = numAnterior < numActual;

            while (correcto && i < numeros.Length - 1)
            {
                numAnterior = numeros[i];
                numActual = numeros[i+1];
                i++;

                correcto = numAnterior < numActual;
            }
        }

        return correcto;
    }

    /// <summary>
    /// Apartir de un array de numeros enteros ordenados (acendente) y no repetidos,
    /// devuelve todos los que faltan entre el mayor y el menor.
    /// </summary>
    /// <param name="numeros"></param>
    /// <returns></returns>
    public static int[] NumerosQueFaltan(int[] numeros)
    {
        int e = 0;
        int j = 1;
        int numFaltan;
        int numGrande = numeros[numeros.Length - 1];
        int numPequeno = numeros[0];
        numFaltan = (numGrande - numPequeno) - numeros.Length + 1;
        int[] arrayNumFaltan = new int[numFaltan];

        for(int i = 0; i < numeros.Length - 1; i++)
        {
            while(numeros[i] + j != numeros[i + 1])
            {
                arrayNumFaltan[e] = numeros[i] + j;
                e++;
                j++;
            }
            j = 1;
        }

        return arrayNumFaltan;
    }

    /// <summary>
    /// Acción de imprimir todos los numeros enteros de un array separados por ','
    /// </summary>
    /// <param name="numeros"></param>
    public static void ImprimirArray(int[] numeros)
    {
        for (int i = 0; i < numeros.Length; i++)
        {
            if (i == numeros.Length - 1) Console.Write(numeros[i]);
            else Console.Write(numeros[i] + ", ");
        }
    }
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant