+ 1

Lists and BitArray

So I'm not sure why my code is not working right now. I have tried several different types of codes but nothing has worked. using System; using System.Collections.Generic; namespace SoloLearn { class Program { static void Main(string[] args) { int numOfPlayers = Convert.ToInt32(Console.ReadLine()); List<int> scores = new List<int>(); int count = 0; while (count<numOfPlayers) { int score = Convert.ToInt32(Console.ReadLine()); //your code goes here // scores.Add(59); // scores.Add(3); // scores.Add(12); // scores.Add(4); // scores.Add(5); scores.Add (score); scores.Sort(); for (int x = 0; x <= scores.Count; count++); Console.WriteLine (scores[x] + " "); } //sort the list and output elements } } }

12th Jan 2022, 10:36 PM
Chris
Chris - avatar
6 Answers
+ 2
you can change the for loop to: for (int x =0; x < scores.Count; x++){ Console.WriteLine (scores[x] + " "); count++; } I added { } and moved count++ inside and x < scores.Count instead of x <= your for loop doesn't increment x++ it results in an infinite loop.
12th Jan 2022, 11:01 PM
Bahhaāµ£
Bahhaāµ£ - avatar
+ 1
Thank you i was trying to figure it out. I just knew i needed to add count++ but i wasnt sure where at.
12th Jan 2022, 11:33 PM
Chris
Chris - avatar
+ 1
Brit I didn't know it was a code challenge. I tried it with predefined values. you need to move the for loop outside of while { } loop while (count<numOfPlayers) { int score = Convert.ToInt32(Console.ReadLine()); //your code goes here scores.Add(score); count++; } //end of while loop //sort the list and output elements scores.Sort(); for(int x=0; x<scores.Count;x++){ Console.Write(scores[x] + " "); } //end of for loop
13th Jan 2022, 7:59 AM
Bahhaāµ£
Bahhaāµ£ - avatar
+ 1
Thank you
13th Jan 2022, 8:52 PM
Chris
Chris - avatar
0
Hey, Bahha I tried what you suggest but no improvement.. using System; using System.Collections.Generic; namespace SoloLearn { class Program { static void Main(string[] args) { int numOfPlayers = Convert.ToInt32(Console.ReadLine()); List<int> scores = new List<int>(); int count = 0; while (count<numOfPlayers) { int score = Convert.ToInt32(Console.ReadLine()); //your code goes here scores.Add (score); scores.Sort(); for (int x =0; x < scores.Count; x++) { Console.WriteLine (scores[x] + " "); count++; } } //sort the list and output elements } } }
13th Jan 2022, 2:10 AM
Chris
Chris - avatar
0
Hi.. You can use foreach for scores.Add
13th Jan 2022, 2:13 AM
Mehdi
Mehdi - avatar