+ 3

[SOLVED] (Thank you) Queue it up Sololearn C# challenge

I'm attempting to complete the challenge Queue it up, but I've encountered an issue with the Array.Sort(); command. the challenge reads: Write a program that will take 3 numbers as input and store them in a defined queue. Also, add code to output the sorted sequence of elements in the queue separated by a space. sample input 6 3 14 sample output Queue: 6 3 14 Sorted: 3 6 14 To copy the queue into a new array use the ToArray() method of the queue: int[] arr = queue.ToArray(); Then, recall the Array.Sort() method my code: (Edited - this code now completes the challenge) https://code.sololearn.com/cA5A14a5a2a4 id appreciate it if someone could take a look and explain where I'm going wrong please

28th Jun 2021, 5:39 AM
Andrew Hall
Andrew Hall - avatar
6 Answers
+ 6
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace consoleapp1 { class program { static void Main(string[] args) { Queue<int> q = new Queue<int>(); while (q.Count <3) { int num = Convert.ToInt32(Console.ReadLine()); q.Enqueue(num); } Console.Write("Queue: "); foreach (int i in q) Console.Write(i + " "); Console.WriteLine(); Console.Write("Sorted: "); int []x = q.ToArray(); Array.Sort(x); foreach (int i in x) Console.Write(i + " "); } } }
11th Nov 2022, 3:27 PM
Pooja Patel
Pooja Patel - avatar
+ 1
q.ToArray(); This returns an Array from the Queue and needs to be set to a variable of the appropriate type. In this case an int array. Like; int[] x = q.ToArray(); q.Sort(); Sort is a static method of the Array class, it sorts the array in-place and should be passed the value of the new previously created Array like; Array.Sort(x); Then you just need to change the last for loop from q to x.
28th Jun 2021, 6:39 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Cheers again guys. I've used the advice given and the code in my link now completes the challenge should anyone else experience similar issues with this one.
28th Jun 2021, 7:42 PM
Andrew Hall
Andrew Hall - avatar
0
Cheers for the input guys. Ill have a proper look when i get home 👍
28th Jun 2021, 8:58 AM
Andrew Hall
Andrew Hall - avatar
0
using System; using System.Collections.Generics; using System.Linq; using System.Text; namespace Standard { class Program { static void Main(string[] args) { Queue<int> q = new Queue<int>(); for(int i = 0; i < 3; i++) { int num = Convert.ToInt32(Console.ReadLine()); q.Enqueue(num); } Console.Write("Queue: "); foreach (int i in q) Console.Write(i + " "); Console.WriteLine(); Console.Write("Sorted: "); int[] array = q.ToArray(); Array.Sort(x); foreach (int j in x) Console.Write(i + " "); } } }
1st Nov 2022, 6:53 PM
Krzysztof Jura
Krzysztof Jura - avatar
0
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace consoleapp1 { class program { static void Main(string[] args) { Queue<int> q = new Queue<int>(); while (q.Count <3) { int num = Convert.ToInt32(Console.ReadLine()); q.Enqueue(num); } Console.Write("Queue: "); foreach (int i in q) Console.Write(i + " "); Console.WriteLine(); Console.Write("Sorted: "); int []x = q.ToArray(); Array.Sort(x); foreach (int i in x) Console.Write(i + " "); } } }
28th Jul 2023, 8:51 PM
Melta