Autor Beitrag
estrella1410
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 46



BeitragVerfasst: Di 10.02.09 09:31 
Einen guten Morgen,

hab da mal ein kleines Problem ...wahrscheinlich ist es..oder besser gesagt, sicherlich ist es nicht schwierig umzusetzn aber ich hab da jetzt solange dran rumgefummelt um es hinzukriegn und bin schon total verwirrt ^^

also folgendes:

Ich habe ein Array mit 8 Werten, die sortiert sind ....ich möchte nun in ein weiteres array reinschreiben, wie oft welcher wert vorkommt (könnte auch n 2 dimensionales array machen aber hab mich derweil einfach mal für n 2tes entschiedn, was ja im grunde auch egal ist...)
diese schleife könnte ja im grunde so aussehn, dass er von obn zählt und ab dem punkt wo ne neue zahl kommt soll er den bisherigen wert der alten zahl ins array schreibn und da weiter machen....

aber wie setz ich das nun korrekt um?!
hab grad ne blokade irgendwie


wäre dankbar wenn mir da einer helfn könnt,

gruß
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Di 10.02.09 11:12 
Hallo estrella1410,

den Algorithmus um Elemente in einem sortierten! Array zu zählen und in ein "zweites Array" zu packen, hast du ja bereits beschrieben. Ich packe die Anzahl der Elemente nicht in ein Array, sondern in ein Dictionary (weil ich vorher die Anzahl der Elemente nicht genau weiß und so eine Verbindung zwischen Zahl (key) und Anzahl (value) habe).

Hier mein Vorschlag für eine Methode die für einen sortiertes (int) Array die Anzahl von Elementen in einem Dictionary (key Zahl, value Anzahl) zurück gibt:
ausblenden volle Höhe c#
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
private static Dictionary<intint> countSortedContent(int[] array)
{
    Dictionary<intint> list = new Dictionary<intint>();

    // check arguments
    if (array.Length == 0)
    {
        // return null : better throw an exception
        return null;
    }

    int currentKey = 0, count = 0;
    currentKey = array[0];
    for (int i = 0; i < array.Length; i++)
    {
        // check if key is different now
        if (currentKey != array[i])
        {
            // new content, add the amount of the old content
            list.Add(currentKey, count);
            // reset counter and key
            count = 0;
            currentKey = array[i];
        }

        count++;
    }

    // Add the amount of the last content
    list.Add(currentKey, count);

    return list;
}


Ich hoffe das Hilft,
Gruß Daniel

@edit: ein klein bisschen schneller ;)

Anmerkung: generisch ist das natürlich schicker:

ausblenden volle Höhe c#
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
      private static Dictionary<objectint> countSortedContent(object[] array)
        {
            Dictionary<objectint> dictionary = new Dictionary<objectint>();

            if (array.Length == 0)
            {
                return null;
            }

            object currentKey = 0;
            int count = 0;
            currentKey = array[0];
            for (int i = 0; i < array.Length; i++)
            {
                // check if key is different now
                if (!currentKey.Equals(array[i]))
                {
                    // new content, add the amount of the old content
                    dictionary.Add((object) currentKey, count);
                    // reset counter and key
                    count = 0;
                    currentKey = array[i];
                }

                count++;
            }

            // Add the amount of the last content
            dictionary.Add(currentKey, count);

            return dictionary;
        }


Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt


Zuletzt bearbeitet von danielf am Di 10.02.09 11:19, insgesamt 1-mal bearbeitet
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Di 10.02.09 11:14 
Hallo,

in Pseudo-Code (denn Du sollst auch noch etwas produzieren können):
ausblenden XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
Dictionary<int, int> dict = new Dictionary<int, int>()
letzterWert = array[max]
anzahl = 1 
for(int x1 = max; x1 >= 0; x1--)
begin
   if (array[x1] != letzterWert)
   then begin
      dict.Add(letzterWert, anzahl)
      letzterWert = array[x1]
      anzahl = 0
   end
   anzahl++
end
//  der letzte Wert fehlt noch in der Aufstellung
dict.Add(letzterWert, anzahl)

Dictionary<T, U> halte ich für das beste in der Situation: Du hast die eindeutigen Werte und zu jedem die Anzahl; Du hast keine Probleme mit der Zuordnung eines Anzahl-Arrays zum Werte-Array. Außerdem brauchst Du Dir keine Gedanken über die Größe des Anzahl-Arrays zu machen (bedenke: Arrays haben immer eine konstante Länge).

Gruß Jürgen

/Edit
Mist, Daniel war schneller. Ich habe mich an die Vorgabe "vom größten zum kleinsten Wert zählen" gehalten, aber für ein Dictionary ist das völlig unwichtig.
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Di 10.02.09 15:04 
user profile icondanielf hat folgendes geschrieben Zum zitierten Posting springen:
Anmerkung: generisch ist das natürlich schicker:
Wirklich schick generisch wäre eher
ausblenden C#-Quelltext
1:
Dictionary<T, int> countSortedContent<T>(IEnumerable<T> elements)					

;) .
Und mit 3.5 ist auch die Implementierung kein großes Problem mehr:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
    static IEnumerable<KeyValuePair<T, int>> GroupAndCount<T>(IEnumerable<T> elements)
    {
      return
        from e in elements
        group e by e into g
        select new KeyValuePair<T, int>(g.Key, g.Count()); // Tupel für Arme
    }

    // Oder auch
    static IEnumerable<KeyValuePair<T, int>> GroupAndCount2<T>(IEnumerable<T> elements)
    {
      return elements
        .GroupBy(e => e)
        .Select(g => new KeyValuePair<T, int>(g.Key, g.Count()));
    }

_________________
>λ=
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Di 10.02.09 15:37 
hey Kha,

sehr schön :) Den Ansatz hab ich auch probiert... bin aber auf die schnelle gescheitert und habs dann lieber old style gemacht. THX für das Beispiel.

Gruß,

Daniel