C
C#3y ago
Jules

✅ array.sort help

I got the stuff down, but I don’t think it’s right
112 Replies
Jules
JulesOP3y ago
So I am stuck with array.sort, I need to sort the array from a text file and then find the median. But I don't think I'm doing the sorting correct...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace assignment5_2_
{
class Program
{
static void Main(string[] args)
{
const int SIZE = 43;
int[] numbers = new int[SIZE];

int index = 0;
int total = 0;
//Console.WriteLine("Hello");

StreamReader inputFile;
inputFile = File.OpenText("Scores.txt");

while (index < numbers.Length && !inputFile.EndOfStream)
{
numbers[index] = int.Parse(inputFile.ReadLine());
//Console.WriteLine(numbers[index]);
index++;
}
inputFile.Close();
int highest = numbers[0];
int lowest = numbers[0];

for (int i = 0; i < numbers.Length; i++)
{
total += numbers[i];
}
Console.WriteLine(total/numbers.Length);
for (int h = 1; h < numbers.Length; h++)
{
if (numbers[h] > highest)
{
highest = numbers[h];
}
}
Console.WriteLine(highest);
for (int l = 1; l < numbers.Length; l++)
{
if (numbers[l] < lowest)
{
lowest = numbers[l];
}
}
Array.Sort(numbers);

Console.WriteLine(lowest);
Console.WriteLine(total);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace assignment5_2_
{
class Program
{
static void Main(string[] args)
{
const int SIZE = 43;
int[] numbers = new int[SIZE];

int index = 0;
int total = 0;
//Console.WriteLine("Hello");

StreamReader inputFile;
inputFile = File.OpenText("Scores.txt");

while (index < numbers.Length && !inputFile.EndOfStream)
{
numbers[index] = int.Parse(inputFile.ReadLine());
//Console.WriteLine(numbers[index]);
index++;
}
inputFile.Close();
int highest = numbers[0];
int lowest = numbers[0];

for (int i = 0; i < numbers.Length; i++)
{
total += numbers[i];
}
Console.WriteLine(total/numbers.Length);
for (int h = 1; h < numbers.Length; h++)
{
if (numbers[h] > highest)
{
highest = numbers[h];
}
}
Console.WriteLine(highest);
for (int l = 1; l < numbers.Length; l++)
{
if (numbers[l] < lowest)
{
lowest = numbers[l];
}
}
Array.Sort(numbers);

Console.WriteLine(lowest);
Console.WriteLine(total);
Console.ReadLine();
}
}
}
Kouhai
Kouhai3y ago
Array.Sort sorts an array, you can find the median by getting the middle value.
Jules
JulesOP3y ago
but how would i find median with the array sorting?
Kouhai
Kouhai3y ago
Let's take a simple example We have this set [1, 2, 3, 4, 5] what's the median value for that set?
Jules
JulesOP3y ago
3
Kouhai
Kouhai3y ago
Great, if we take this set [2, 3, 4, 5] What's the median here as well?
Jules
JulesOP3y ago
technically, both 3 and 4? sorry kinda learning disabled
Kouhai
Kouhai3y ago
You're correct, so when the set has an even number of values we take both these numbers add them together and then divide them by two! So it's (3+4)/2
Jules
JulesOP3y ago
3.5
Kouhai
Kouhai3y ago
Awesome, in your case the number of values in the set (array) is odd, right?
Jules
JulesOP3y ago
43 so yes but how would i do a sort with a text Thonk
Kouhai
Kouhai3y ago
Great, let's break done the code you've written Initialize the array
const int SIZE = 43;
int[] numbers = new int[SIZE];
const int SIZE = 43;
int[] numbers = new int[SIZE];
Read file and fill the array
StreamReader inputFile;
inputFile = File.OpenText("Scores.txt");

while (index < numbers.Length && !inputFile.EndOfStream)
{
numbers[index] = int.Parse(inputFile.ReadLine());
index++;
}
StreamReader inputFile;
inputFile = File.OpenText("Scores.txt");

while (index < numbers.Length && !inputFile.EndOfStream)
{
numbers[index] = int.Parse(inputFile.ReadLine());
index++;
}
The numbers array is most likely not sorted, we can however call Array.Sort(numbers) Which will sort the array in place!
Jules
JulesOP3y ago
is my Array.Sort in the right place??
mtreit
mtreit3y ago
What do you think?
Jules
JulesOP3y ago
I think it should be under int total = 0 imo
Kouhai
Kouhai3y ago
But the array won't be filled yet
mtreit
mtreit3y ago
What would be in the array at that point?
Jules
JulesOP3y ago
so would i put it in after the input file declaration?
mtreit
mtreit3y ago
Your algorithm should be: Read all of the data into the array. Sort the array. Compute the median. Where to put the sort should follow from that.
Jules
JulesOP3y ago
alright, got it but im still confused on how to find the median ohgawsh
mtreit
mtreit3y ago
I think @Kouhai already gave a great explanation of how to do that. What part is confusing you?
Jules
JulesOP3y ago
im also supposed to display what the median is would i just do Console.WriteLine(Array.Sort(numbers));?
mtreit
mtreit3y ago
What do you intend that to do? First, notice that Array.Sort returns void.
Jules
JulesOP3y ago
show the median
mtreit
mtreit3y ago
So you just asked if you should do: Console.WriteLine(void);
Jules
JulesOP3y ago
... oh
mtreit
mtreit3y ago
Remember, when you call a method you can mentally replace that call with the result that it returns.
Jules
JulesOP3y ago
im sorry, i'm still a bit confused like im really slow
Kouhai
Kouhai3y ago
as mtreit said Array.Sort returns void, that means it returns nothing it doesn't make sense to call Console.WriteLine with nothing, right?
Jules
JulesOP3y ago
That’s right
Kouhai
Kouhai3y ago
Okay great, basically Array.Sort sorts an array in place, what does in place mean? it means it mutates the array passed to it instead of returning a newly sorted array!
Jules
JulesOP3y ago
But how would I show the result of the sorting?
Kouhai
Kouhai3y ago
You only need the median right? So you don't have to show the whole array!
Jules
JulesOP3y ago
yep! how would i show the median then?
Kouhai
Kouhai3y ago
Let's ask another question first. How to show the first element in an array?
Jules
JulesOP3y ago
uhhh i wouldnt do a console writeline, right?
Kouhai
Kouhai3y ago
Console.WriteLine as the name suggests just writes something to the console (terminal) and moves the caret to a new line (i.e the next time your write something to the console it'll be on an a new line) So you can use Console.WriteLine More importantly how would you go about accessing the first element of the array?
Jules
JulesOP3y ago
numbers[index]?
Kouhai
Kouhai3y ago
what's index in this case?
Jules
JulesOP3y ago
the 43 numbers
Kouhai
Kouhai3y ago
But that means the first element is at index 43
Jules
JulesOP3y ago
oh im really trying but i really am having trouble understanding, im sorry
Kouhai
Kouhai3y ago
No worries! Let's take an example
int[] nums = new[] {4, 9, 12, 80};
int[] nums = new[] {4, 9, 12, 80};
The value 9 is at what index?
Jules
JulesOP3y ago
2
Kouhai
Kouhai3y ago
Close, but not quite right Array's in c# are 0 indexed, meaning the first element starts at index 0 With that info, 9 is at what element now?
Jules
JulesOP3y ago
OHHHH 1
Kouhai
Kouhai3y ago
Awesome, what about the index of the last element? What is it?
Jules
JulesOP3y ago
Uh 3?
Kouhai
Kouhai3y ago
Spot on! Let's try another array
int[] nums = new[] {1, 2, 3, 4, 5};
int[] nums = new[] {1, 2, 3, 4, 5};
Where are the first and last elements @Jules ?
Jules
JulesOP3y ago
0, 1 and 4, 5?
Kouhai
Kouhai3y ago
Yup that's exactly right! So let's say you want to get the middle element, what would be it's index?
Jules
JulesOP3y ago
2, 3?
Kouhai
Kouhai3y ago
That's indeed correct, now isn't that value the median for our set?
Jules
JulesOP3y ago
Yes?
Kouhai
Kouhai3y ago
Okay, so to get the median of an array with 5 elements, we need to access the 2nd element Let's assume we have an array of 43 elements What would be it's median's index?
Jules
JulesOP3y ago
uhhhh lemme do the math uhhh i got 22/23
Kouhai
Kouhai3y ago
How did you calculate it?
Jules
JulesOP3y ago
i uh wrote down 0-43 and crossed like 43, 0, 42, 1
Kouhai
Kouhai3y ago
You wrote the numbers from 0 to 43 and then crossed 0, 1, 42, 43?
Jules
JulesOP3y ago
no i crossed from 43, 0, 42, and 1 i did 0 to 43 tho
Kouhai
Kouhai3y ago
Okay, let's first try a smaller array first, we have an array with 9 elements Where is the median?
Jules
JulesOP3y ago
0, 1, 2, 3, 4, 5, 6, 7, 8
Kouhai
Kouhai3y ago
That means your array has 10 elements no?
Jules
JulesOP3y ago
(4+5)/2 = 4.5? ... OH Would it be Thonk
Kouhai
Kouhai3y ago
Yup the index would be 4! Now let's go back to our 43 element array What's the median's index now?
Jules
JulesOP3y ago
would it it be 22?
Kouhai
Kouhai3y ago
Why would it be 22? How is it calculated?
Jules
JulesOP3y ago
uhhhhhh gimme min ima redo it wait is it 21?
Pobiega
Pobiega3y ago
Yes. But why is it 21? Can you come up with a formula?
Jules
JulesOP3y ago
is it this?
for (int row = 0; row < numbers.GetLength(0); row++)
{
for (int col = 0; col < numbers.GetLength(1); col++)
{
Console.WriteLine(numbers[row, col])
}
}
for (int row = 0; row < numbers.GetLength(0); row++)
{
for (int col = 0; col < numbers.GetLength(1); col++)
{
Console.WriteLine(numbers[row, col])
}
}
cause im looking over videos as well but im confused
Kouhai
Kouhai3y ago
Before code or anything Mathematically how would you calculate it? let's say you have 13981 elements, it would be REALLY tedious to do it by hand and cross entries
Jules
JulesOP3y ago
OHHH thats what youre Smadge i dont know, i only know to cross which is kinda embarrassing\
Kouhai
Kouhai3y ago
😅 Okay, if the set's element count is odd, we can easily calculate it like this [n+1]/2 where n is the count, but that assumes a set starts at index 1, which isn't true for C#, so how would you fit it with a 0 indexed array?
Jules
JulesOP3y ago
[n+0]/2?
Kouhai
Kouhai3y ago
Well, why not give it a try with 43?
Jules
JulesOP3y ago
[n+43]/2??
Kouhai
Kouhai3y ago
Give this one more read
Jules
JulesOP3y ago
tryna figure it out im sorryu [43+0]/2? Is this right?
Kouhai
Kouhai3y ago
That's indeed right, that expression could be simplified right?
Jules
JulesOP3y ago
yeah?
Kouhai
Kouhai3y ago
How would you go about simplifying it?
Jules
JulesOP3y ago
43/2??
Kouhai
Kouhai3y ago
Yes correct, not going back to your very first question How would you apply this information with your program logic to get the median of the array?
Jules
JulesOP3y ago
You mean the code?
Kouhai
Kouhai3y ago
yes!
Jules
JulesOP3y ago
for (int row = 0; row < numbers.GetLength(0); row++)
{
for (int col = 0; col < numbers.GetLength(21); col++)
{
Console.WriteLine(numbers[row, col])
}
}
for (int row = 0; row < numbers.GetLength(0); row++)
{
for (int col = 0; col < numbers.GetLength(21); col++)
{
Console.WriteLine(numbers[row, col])
}
}
?
Kouhai
Kouhai3y ago
I meant your original code at the start of the thread
mtreit
mtreit3y ago
Where did a multi-dimensional array suddenly come from in this discussion? 👀
Jules
JulesOP3y ago
OHHHH Videos
mtreit
mtreit3y ago
What do you mean exactly?
Jules
JulesOP3y ago
My instructors videos… aren’t the greatest :/ I asked her about this a few hours ago before this thread
mtreit
mtreit3y ago
Well, you just pasted code that seems to be from a completely unrelated problem to the one being discussed here.
Jules
JulesOP3y ago
All she sent me was this video that had this code on it That’s why I’m kinda lost
mtreit
mtreit3y ago
I think I would forget what she sent you for the moment and focus on what @Kouhai has taught you. You might be overcomplicating things.
Jules
JulesOP3y ago
Probably am overcomplicating things nekocry really sorry I’m guessing… would it be Array.Sort(numbers[43/2]);? Or am I wrong..?
mtreit
mtreit3y ago
What is numbers[43/2] going to return? Do you know what "indexing into an array" means?
Jules
JulesOP3y ago
It errors
mtreit
mtreit3y ago
I'm sure it does You effectively did this:
int index = numbers[21];
Array.Sort(index);
int index = numbers[21];
Array.Sort(index);
Does that make any sense? You can't sort a single integer value. Sort should only be done once on the entire array. Once the array is sorted you can compute the median index and look up the value at that index. That's what your code needs to do: Create the array Sort the array Compute the median index Fetch the value at that index Return that value (Sticking with the simpler case that there are an odd number of values in the array)
Jules
JulesOP3y ago
Like I understand what you guys are telling me but it’s really hard for me to understand with my code ;__;
mtreit
mtreit3y ago
public int GetMedian(int[] unsortedNumbers)
{
// Sort the array.

// Compute the median index.

// Fetch the value at that index in the array.

// Return the value.
}
public int GetMedian(int[] unsortedNumbers)
{
// Sort the array.

// Compute the median index.

// Fetch the value at that index in the array.

// Return the value.
}
Translate the comments into code. If you are not sure how to do that for one of those comments, ask. I know you know how to do the first two.
Jules
JulesOP3y ago
fetching the value, im not sure how to do that
mtreit
mtreit3y ago
You "index into" the array. Like this:
var item = someArray[index];
var item = someArray[index];
If you don't know how to index into an array I might suggest you go back through a basic C# tutorial. We usually recommend $helloworld
MODiX
MODiX3y ago
mtreit#6470
REPL Result: Success
using System;
string[] someArray = new string[] { "A", "B", "C" };

// Fetch the second element!
int index = 1;
string secondString = someArray[index];

Console.WriteLine($"Fetched the string '{secondString}' at index {index} in the array!");
using System;
string[] someArray = new string[] { "A", "B", "C" };

// Fetch the second element!
int index = 1;
string secondString = someArray[index];

Console.WriteLine($"Fetched the string '{secondString}' at index {index} in the array!");
Console Output
Fetched the string 'B' at index 1 in the array!
Fetched the string 'B' at index 1 in the array!
Compile: 672.460ms | Execution: 43.349ms | React with ❌ to remove this embed.
Jules
JulesOP3y ago
public int GetMedian(int[] unsortedNumbers)
{
// Sort the array.
Array.Sort(numbers);
// Compute the median index.
int index = numbers[43]
// Fetch the value at that index in the array.
int index = 21;
string secondstring = numbers[index];
// Return the value.
Console.WriteLine([index]);
}
public int GetMedian(int[] unsortedNumbers)
{
// Sort the array.
Array.Sort(numbers);
// Compute the median index.
int index = numbers[43]
// Fetch the value at that index in the array.
int index = 21;
string secondstring = numbers[index];
// Return the value.
Console.WriteLine([index]);
}
like.. this?
mtreit
mtreit3y ago
The fact that you have a variable named secondString when your code doesn't use strings at all implies you are just copy pasting code and hoping it will work. Stop doing that and think carefully about what you are doing. Use variable names that make sense for the problem you are working on. Why did you write this line of code?
int index = numbers[43]
int index = numbers[43]
You should replace your Console.WriteLine with this:
// Return the value
return median;
// Return the value
return median;
Jules
JulesOP3y ago
i did have return
mtreit
mtreit3y ago
And then figure out how to make the rest of your code create a variable called median with the correct value.
Jules
JulesOP3y ago
but would it not actually put it in the console like console.writeline?
mtreit
mtreit3y ago
If you want to print out the result, do that in the code that calls the GetMedian function.
var median = GetMedian(numbers);
Console.WriteLine(median);
var median = GetMedian(numbers);
Console.WriteLine(median);
Jules
JulesOP3y ago
oh
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Closed!
Want results from more Discord servers?
Add your server