C
C#4mo ago
js

function not running

int count = 1;
Console.WriteLine("Oyster Eating Competition\nHow many competitors are there?");
int numCompetitors = int.Parse(Console.ReadLine());
string[] names = new string[numCompetitors];
int[] oystersEaten = new int[numCompetitors];

for (int i = 0; i < numCompetitors; i++)
{
Console.WriteLine($"What is competitor number {count}'s name?");
names[i] = Console.ReadLine();
Console.WriteLine($"How many oysters did {names[i]} eat?");
oystersEaten[i] = int.Parse(Console.ReadLine());
count = count + 1;
}
static void bubbleSort(int[] oystersEaten)
{
int temp;

for (int i = 0;i < oystersEaten.Length - 1; i++)
{
for(int j = 0; j < oystersEaten.Length - (1 + i); j++)
{
if (oystersEaten[j] > oystersEaten[j + 1])
{
temp = oystersEaten[j + 1];
oystersEaten[j +1] = oystersEaten[j];
oystersEaten[j] = temp;
}
}
}
}
p.bubbleSort();
Console.WriteLine(string.Join(", ", oystersEaten));
int count = 1;
Console.WriteLine("Oyster Eating Competition\nHow many competitors are there?");
int numCompetitors = int.Parse(Console.ReadLine());
string[] names = new string[numCompetitors];
int[] oystersEaten = new int[numCompetitors];

for (int i = 0; i < numCompetitors; i++)
{
Console.WriteLine($"What is competitor number {count}'s name?");
names[i] = Console.ReadLine();
Console.WriteLine($"How many oysters did {names[i]} eat?");
oystersEaten[i] = int.Parse(Console.ReadLine());
count = count + 1;
}
static void bubbleSort(int[] oystersEaten)
{
int temp;

for (int i = 0;i < oystersEaten.Length - 1; i++)
{
for(int j = 0; j < oystersEaten.Length - (1 + i); j++)
{
if (oystersEaten[j] > oystersEaten[j + 1])
{
temp = oystersEaten[j + 1];
oystersEaten[j +1] = oystersEaten[j];
oystersEaten[j] = temp;
}
}
}
}
p.bubbleSort();
Console.WriteLine(string.Join(", ", oystersEaten));
it runs fine up until p.bubbleSort i think, im not sure how to call my function properly im just trying to use the sort to sort the array
13 Replies
Angius
Angius4mo ago
What's p? Your bubbleSort() takes an array of integers You call it on some p object without giving it any data
Jimmacle
Jimmacle4mo ago
yeah this code won't even compile, let alone run you'd call your function like bubbleSort(array);, because it's not a member of p (p doesn't exist at all?) you can't do p.bubbleSort(); there is a way to use that syntax called extension methods, but that's not what you have here
js
js4mo ago
i just removed the p and it worked
Console.WriteLine($int.Parse(count3),". {cnames[count2]} ate {oystersEaten[count2]} oysters");
Console.WriteLine($int.Parse(count3),". {cnames[count2]} ate {oystersEaten[count2]} oysters");
but im trying to output an integer at the start of the write line and its saying it cant be parsed or smth
Angius
Angius4mo ago
This will not compile
js
js4mo ago
yh ik its not idk what im doing wrong
Angius
Angius4mo ago
Console.WriteLine() with more than one parameters expects the first one to be a template string And the rest to be data to insert into the template Use $interpolation
MODiX
MODiX4mo ago
String interpolation is the preferred way of building strings in C#. It is easier to read than concatenation. For example:
var foo = 1;
var bar = 2;
Console.WriteLine("foo is equal to: " + foo + " and bar is equal to: " + bar);
var foo = 1;
var bar = 2;
Console.WriteLine("foo is equal to: " + foo + " and bar is equal to: " + bar);
can be written as:
var foo = 1;
var bar = 2;
Console.WriteLine($"foo is equal to: {foo} and bar is equal to: {bar}");
var foo = 1;
var bar = 2;
Console.WriteLine($"foo is equal to: {foo} and bar is equal to: {bar}");
js
js4mo ago
it was working fine till i added the int parse count 3 bit
Angius
Angius4mo ago
Well, you added it outside of the string Where the template string goes So "I hit my TV with a hammer to switch the channel and it no longer works" vibes
Jimmacle
Jimmacle4mo ago
either a) it did not work or b) you did more than just remove the p please be specific instead of saying "not work," there are a lot of possible problems from compile time errors (your program will not build and you have red squiggles) to run time errors (the program doesn't do what you want or it crashes)
js
js4mo ago
cs
File.AppendAllText(filepath, $"{count3}. {cnames[count2]} ate {oystersEaten[count2]} oysters");
cs
File.AppendAllText(filepath, $"{count3}. {cnames[count2]} ate {oystersEaten[count2]} oysters");
this line is running in a loop but each loop it isnt outputting to a new line its just adding to the end how do i go onto a new line after every iteration
Angius
Angius4mo ago
Add a newline character, for example \n Or see if maybe there is File.AppendLine() method
js
js4mo ago
thx