+ 2

[SOLVED] (Thank you) Top of the leaderboard

I'm working on top of the leaderboard challenge which reads like this: "You need to write a program for the game to sort player scores. The program you are given takes N number as input, which represents the number of players, and defines a score list. Complete the program to take N count of numbers (the scores) as input, store them in a scores list, sort and output them, each separated by a space. Sample Input 3 12 4 5 Sample Output 4 5 12 You need to execute the Add() method inside the while loop" my code is here: https://code.sololearn.com/ca4a9a76A10a its taking the input correctly but refusing to output. I'd appreciate it if someone could take a look and nudge me in the direction of where I'm going wrong please.

12th Jun 2021, 7:00 AM
Andrew Hall
Andrew Hall - avatar
5 ответов
+ 3
The main problem is there: foreach (int playerscore in scores) { scores.Add(score); } This loop will never run because before running this loop there will be no value inside scores. So it never adds anything to scores and therefore in the end you get no output. Next, You have written like this to print the output: foreach (int score in scores) { scores.Sort(); Console.Write(scores + " "); } I don't really think you need to sort again and again inside the loop. It's better to sort outside the loop and then print only the numbers. Also, you are printing scores not score. So it would print the object, not the value . That's why it's better not to use similar words. So after solving these bugs your code would be like this: https://code.sololearn.com/c5a24a75A22A
12th Jun 2021, 7:35 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 3
yeah! thanks bro
11th Jul 2022, 9:39 AM
Cristiano Ronaldo
Cristiano Ronaldo - avatar
+ 1
The correct answer: while (count<numOfPlayers) { int score = Convert.ToInt32(Console.ReadLine()); //your code goes here scores.Add(score); count++; } //sort the list and output elements scores.Sort(); foreach (int playerScore in scores) { Console.Write(playerScore + " "); }
28th Sep 2023, 7:02 AM
Mohammad Sadeq Alziadi
Mohammad Sadeq Alziadi - avatar
0
Always welcome bro.
12th Jun 2021, 8:09 AM
The future is now thanks to science
The future is now thanks to science - avatar
- 1
thank you very much. that did the trick. I've adjusted the code in my original post to reflect the changes, and now the code works to complete the challenge!
12th Jun 2021, 8:05 AM
Andrew Hall
Andrew Hall - avatar