c# tablica, petłe

0

Stwórz funkcję decimal ElementMax(decimal[] tablica ) , która zwróci element maksymalny z tablicy tablica.
mam dwa pytania: pierwsze to jak wywołać tego typu funkcję, żeby się wyświetliła w konsoli a drugie to czy macie jakiś pomysł na zrobienie tego zadania?

0

W Javie to będzie tak:

public class WriteOnlyPL {

     public static void main(String []args) {
        System.out.println(max(new long[]{0,1,2,3,4,5,6}));
        System.out.println(max(new long[]{6,5,4,3,2,1,0}));
        System.out.println("WriteOnly.pl");
     }
     
    private static long max(long[] array) {
        final int index = array.length - 1;
        return max_(index - 1, array[index], array);
    }
    
    private static long max_(int index, long value, long[] array) {
        return index == -1 ? value : max_(index -1, max(value, array[index]), array);
    }
    
    private static long max(long a1, long a2) {
        return a1 < a2 ? a2 : a1;
    }
}

C# podobno podobny więc łatwo przerobisz

0

Dwie pomocnicze funkcje i rekurencja, to przesada, (chyba, że Chciałeś dać "zobfuskowanego" gotowca :)); acha i w Javie jest Math.max

2

Czy tylko mnie się wydaje, że ten wpis nie ma sensu?

Trochę przesadzasz. Kod działający dla C#

using System;

namespace Rextester
{
public class Program {

     public static void Main(String []args) {
        Console.WriteLine(max(new decimal[]{0,1,2,3,4,5,6}));
        Console.WriteLine(max(new decimal[]{6,5,4,3,2,1,0}));
        Console.WriteLine("WriteOnly.pl");
     }

    private static decimal max(decimal[] array) {
        int index = array.Length - 1;
        return max_(index - 1, array[index], array);
    }

    private static decimal max_(int index, decimal value, decimal[] array) {
        return index == -1 ? value : max_(index -1, max(value, array[index]), array);
    }

    private static decimal max(decimal a1, decimal a2) {
        return a1 < a2 ? a2 : a1;
    }
}
}
0

Dziękuję za pomoc :)

2
        public static T Max<T>(this IEnumerable<T> enumerable) where T : IComparable<T>
        {
            if (enumerable == null)
                throw new ArgumentNullException(nameof(enumerable));

            var enumerator = enumerable.GetEnumerator();

            if (!enumerator.MoveNext())
                throw new ArgumentException(nameof(enumerable));

            var max = enumerator.Current;
            while (enumerator.MoveNext())
                if (enumerator.Current.CompareTo(max) == 1)
                    max = enumerator.Current;

            return max;
        }

👿

1 użytkowników online, w tym zalogowanych: 0, gości: 1