Autor Beitrag
Vegeto
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 262



BeitragVerfasst: Do 22.05.14 13:01 
Hallo,

ich glaube das was ich suche ist eigentlich ganz einfach, doch irgendwie komme ich da nicht drauf.
Ich habe eine Methode geschrieben die liest ein Array aus und gibt es als Histogramm wieder, doch in der WAAGERECHTENFORM !
Methode von mir:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
        static void writeHistrogramm(int[] Array)
        {
            float max = 0;
            for (int i = 0; i < Array.Length; i++)
            {
                if (Array[i] > max) 
                    max = Array[i];
            }
            for (int i = 0; i < Array.Length; i++)
            {
                for (int j = 0; j < (int)(Array[i] / max * 25f); j++)
                {
                    Console.Write('*');
                }
                Console.WriteLine();
            }
        }


So wird die Methode genutzt:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
int[] iArray = new int[5];
iArray[0] = 11;
iArray[1] = 3;
iArray[2] = 0;
iArray[3] = 21;
iArray[4] = 6;

writeHistrogramm(iArray);


Ausgabe wäre zum Beispiel so:
ausblenden Quelltext
1:
2:
3:
4:
5:
***********
***

*********************
******


_____________________________________________________________________________________



Doch ich möchte das die Ausgabe wie folgt sein soll:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
        *
*       *
*   *   *
*   *   *
* * *   * *
* * *   * *




Ich hoffe jemand kann mir auf die schnelle weiterhelfen.

Lg

Moderiert von user profile iconChristian S.: Code-Tags hinzugefügt
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 22.05.14 14:19 
Wie kommt dann auf das untere Histogramm? :gruebel:

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Do 22.05.14 14:49 
Hallo Vegeto,

erzeuge dir dynamisch ein 2-dimensionales Array und fülle es. Und am Ende einfach mittels zweier Schleifen Zeichen für Zeichen ausgeben.


@Christian: ich denke, daß untere Beispiel war exemplarisch gemeint.
Vegeto Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 262



BeitragVerfasst: Do 22.05.14 14:53 
Hallo

user profile iconChristian S. hat folgendes geschrieben Zum zitierten Posting springen:
Wie kommt dann auf das untere Histogramm? :gruebel:


Wie schon Th69 es gesagt hat, es sollte nur ein BEISPIEL sein, so habe ich mir das vorgestellt.

@Th69 Ich werde es versuchen, danke für den Tipp
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Do 22.05.14 15:34 
Hallo,

2-Dim Array?
Man kann den größten Wert suchen und dann in einer Schleife alle Spalten abfragen, ob sie größer sind , als der momentane Wert.
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
Suche Maximalen Wert Max
Solange Max > 0 
Begin
  Für alle Spalten von 0 bis n
    Falls Histogram[spalte] > Max dann
      Ausgabe "#"
    sonst
      Ausgabe " "
  Ausgabe(NeueZeile) 
  Max = Max-1
end


Gruß Horst
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Do 22.05.14 15:55 
Wer ein Array Array nennt sollte eigentlich vom Compiler ausgebuht werden ;)


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
public static void WriteHistogramm(int[] array)
{
    int max = array.Max();
    array = array.Select(x => (int)((float)x / max * 25f)).ToArray();

    for (int i = max; i >= 0; i--)
    {
        for (int j = 0; j < array.Length; j++)
        {
            if (array[j] > i)
                Console.Write("* ");
            else
                Console.Write("  ");
        }
        Console.WriteLine();
    }
}
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Do 22.05.14 16:31 
Hallo,

Hollah, ist alles ganz anders.
Die Zahlen werden auf max == 25 normiert.
Ich hoffe, so wird gerundet.
ausblenden C#-Quelltext
1:
array = array.Select(x => (int)((float)x / max * 25f + 0.5f)).ToArray();					

Das geht bei max= 0 voll daneben.
Die Schleife bei Normierung auf 25 lautet dann wohl
   for (int i = 25; i >= 0; i--)

Gruß Horst
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Do 22.05.14 16:51 
Das mit dem +0.5f wird zwar auch funktionieren ist aber irgendwie nicht C#-like.

ausblenden C#-Quelltext
1:
2:
3:
if (max == 0)
    return;
array = array.Select(x => (int)Math.Round((float)x / max * 25f0, MidpointRounding.AwayFromZero)).ToArray();