Need Help with my Array Program (Probs easy for you advanced programmers) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Need Help with my Array Program (Probs easy for you advanced programmers)

Hey guys firstly, sorry that the comments in the Program are in german, but they are for a homework for my uni course and I have to explain what I do, I am certain most of you more advanced programmers will easily see what I have done. My Excercise was to program something that reads a starting number, then as many new numbers as the size of the original starting Number . (First input 20, it will then make you input 20 new numbers so to say) Output of the code was supposed to be the Plateau with the longest equal following Numbers, f,e: Input : 7,7,7,7,3,3,2,2,2,1,5 Output: 4, because the number chain of the 4 7'eners is the longest chain My Problem is it only works if I type in like: 4 and then type in 4 numbers with just one chain, it will tell me how long that chain is, which is what I want of course. However, and this is the problem, if I type in like 6 and then 3,3,3,2,2,1, it will output the longest chain is 4 because it just counts +1 to the highscore int that is counting how long simply all chains are together . Can someone explain to me how to code it so it knows when a new chain should be "memorized" that is not the same as the old chain? (And then also make it output the length of only the longest chain?) Huge thanks in advance to anyone that can help me!!! I dont know how to put my code here guys, can someone tell me ? :(

5th Nov 2017, 9:36 PM
Beginnerboi
3 Answers
+ 5
To post the link to your code - go to the code playground and tap on the 3 dots, choose 'Copy to Clipboard' and then Paste it here. To keep the longest chain of equal numbers, just keep it in a variable. Example: int maxChain = 0; int current = 5; if (current > maxChain) { maxChain = current; }
5th Nov 2017, 10:27 PM
Boris Batinkov
Boris Batinkov - avatar
+ 1
Ich habs jetzt nicht durchlaufen lassen aber ich glaube, du brauchst zwei variablen: Zum einen den Highscore, zum anderen einen Counter, der mitzählt, wie groß das Plateau ist. Wenn die zwei aufeinanderfolgenden Zahlen im Array nicht mehr gleich sind, setzt du den Counter auf 0 und aktualisierst ggf. den Highscore, wenn der Counter größer ist als der aktuelle Highscore.
5th Nov 2017, 10:43 PM
Schindlabua
Schindlabua - avatar
0
This is my code guys :/ using System; using System.Collections.Generic; namespace HA1 { class Program { static void Main(string[] args) { //Anzahl von Zahleneingaben erfragen Console.WriteLine("Geben sie eine Anzahl ein"); int Anzahl = Convert.ToInt32(Console.ReadLine()); //Liste anlegen für die einzugebenden Zahlen int [] Zahlen = new int[Anzahl]; //Schleife die dafür sorgt, das genau so viele Zahlen eingelesen werden wie die Anzahl groß ist, und das diese in einem Feld gespeichert werden for (int zl=0;zl<Anzahl;++zl) { Console.WriteLine("Bitte geben sie eine Zahl ein"); Zahlen[zl] = Convert.ToInt32(Console.ReadLine()); } //Eine weitere Schleife die prüft, ob die eingebene Zahl den gleichen Wert hat wie die nächste Zahl, wenn dieser Fall eintretet, dann wird ein int highscore um 1 erhöht) int highscore= 1; for (int nr = 0;nr<Anzahl-1;++nr) { if (Zahlen[nr] == Zahlen[nr+1]) { highscore= highscore+1; } //Das Längste Plateau wird rausgegeben Console.WriteLine("Das bisher größte Plateau ist" + highscore); } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } }
5th Nov 2017, 9:31 PM
Beginnerboi