C
Join ServerC#
help
✅ (Very Begginer) - I Get errors when I can't access method within other class and syntax
SStr1ke1/19/2023
I have a class " Course " and a class "University", in class Course there is a Code for each teacher (Between 1 and 15)
I need to find the MostCommon teacher that has the most Courses in the University.
$code
I need to find the MostCommon teacher that has the most Courses in the University.
$code
MMODiX1/19/2023
To post C# code type the following:
```cs
// code here
```
Get an example by typing
If your code is too long, post it to: https://paste.mod.gg/
```cs
// code here
```
Get an example by typing
$codegif
in chatIf your code is too long, post it to: https://paste.mod.gg/
SStr1ke1/19/2023
public int Mostcommon()
{
int[] arr = new int[15];
for(int i = 0; i < PlaceCourse.Length; i++)
{
arr[PlaceCourse.GetCode()]++;
}
int MostCommon;
for (int x = 0; x < arr.Length - 1; x++)
MostCommon = Math.Max(arr[x], arr[x + 1]);
for(int y = 0; y < arr.Length; y++)
if (MostCommon == arr[y])
{
return y;
}
}
SStr1ke1/19/2023
internal class Course
{
private string CourseName;
private int WeekHours;
private int Code;
public int GetCode()
{
return Code;
}
}
SStr1ke1/19/2023
The access other class error is fixed
SStr1ke1/19/2023
But there is syntax error with MostCommon
SStr1ke1/19/2023
(int MostCommon)
SStr1ke1/19/2023
And if you know a more efficient/better way of doing this I will be very glad to hear it
SStr1ke1/19/2023
Because I'am pretty positive mine is not so good
SStr1ke1/19/2023
public int Mostcommon()
{
int[] arr = new int[15];
for(int i = 0; i < PlaceCourse.Length; i++)
{
arr[PlaceCourse[i].GetCode()]++;
}
int MostCommon;
for (int x = 0; x < arr.Length - 1; x++)
MostCommon = Math.Max(arr[x], arr[x + 1]);
int y;
for (int y = 0; y < arr.Length; y++)
if (MostCommon == arr[y])
{
y = arr[y];
}
return y;
}
SStr1ke1/19/2023
I had to put "return y" out side the loop because it said that "not all code paths return"
SStr1ke1/19/2023
And now I get syntax error with y as well
MMODiX1/19/2023
you probably want
$scopes
Hhiyosilver1/19/2023
$scopes
MMODiX1/19/2023
scope A {
thing a;
scope B {
thing b;
}
}
thing a
is available in scope A
and scope B
thing b
is available only in scope B
Hhiyosilver1/19/2023
You are trying to return y which was defined inside your for loop.
Hhiyosilver1/19/2023
I assume that causes your syntax error.
SStr1ke1/19/2023
But isn't it if it's inside the scopes of the whole method
SStr1ke1/19/2023
it should be defined inside all of it?
Hhiyosilver1/19/2023
You could define y at the very top so it's in scope for all of it.
SStr1ke1/19/2023
It didn't change much xd
SStr1ke1/19/2023
I guess the problem is that it's value is defined inside the loop
SStr1ke1/19/2023
but I don't know how can I define it without getting the error
SStr1ke1/19/2023
because I need the loop to define it
Hhiyosilver1/19/2023
Give me a second, had to switch from phone to pc
SStr1ke1/19/2023
Yeah I noticed that xd
SStr1ke1/19/2023
Thank you
Hhiyosilver1/19/2023
No idea how people get stuff done on their phones. Just awful all around.
Hhiyosilver1/19/2023
Anyway, so I see now what this is supposed to do
Hhiyosilver1/19/2023
If we start at the top, you know how many courses there are from
PlaceCourse
no?SStr1ke1/19/2023
Only if you could see my screen...xd
SStr1ke1/19/2023
YOu mean like what is the size of PlaceCourse?
SStr1ke1/19/2023
If so, yeah that's a 1000
SStr1ke1/19/2023
private Course[] PlaceCourse;
private int current=0;
public University()
{
PlaceCourse = new Course[1000];
}
private int current=0;
public University()
{
PlaceCourse = new Course[1000];
}
SStr1ke1/19/2023
done it in a constructor
SStr1ke1/19/2023
previoulsy
Hhiyosilver1/19/2023
Oh wait, I'm dumb yeah that makes more sense
Hhiyosilver1/19/2023
Array contains up to 15 ids for each of the teachers
Hhiyosilver1/19/2023
Are you sure they go from 1 to 15?
SStr1ke1/19/2023
yes
SStr1ke1/19/2023
that is declared in the question
Hhiyosilver1/19/2023
because you are accessing the array with indices 1 to 15 then, but it only goes from 0 to 14
Hhiyosilver1/19/2023
int[] arr = new int[15];
for(int i = 0; i < PlaceCourse.Length; i++)
{
arr[PlaceCourse.GetCode()]++;
}
Hhiyosilver1/19/2023
if GetCode ever returned 15 for any teacher, this would explode
SStr1ke1/19/2023
Oh yeah, Ig it's better to write it as 16
SStr1ke1/19/2023
and start from 1
Hhiyosilver1/19/2023
you could just use
Hhiyosilver1/19/2023
GetCode() - 1
to index into the array.Hhiyosilver1/19/2023
You should probably leave it starting at 0, that's pretty standard.
SStr1ke1/19/2023
But then it will return the index with the code-1
SStr1ke1/19/2023
And that won't be the right teacher
Hhiyosilver1/19/2023
It will be if you always access it like that
Hhiyosilver1/19/2023
Lets say you ask "How many courses does the teacher with Id 8 have"?
Hhiyosilver1/19/2023
its
arr[8 - 1]
Hhiyosilver1/19/2023
i.e. the 8th index of your array.
Hhiyosilver1/19/2023
Which by convention starts at index 0, not 1 🙂
SStr1ke1/19/2023
True, but the question wants me to return a number between 1 and 15
Hhiyosilver1/19/2023
I supposed you could just increase it to 16 and just leave the 0 slot empty, but I would suggest that that's not good practice and is more likely to confuse you later.
Hhiyosilver1/19/2023
You can still do that
Hhiyosilver1/19/2023
So you want the most common teacher Id from all the courses.
SStr1ke1/19/2023
yeah
Hhiyosilver1/19/2023
You already define
int MostCommon
so thats almost certainly going to be what you want to returnHhiyosilver1/19/2023
rather than 1
Hhiyosilver1/19/2023
Now lets see how you set it
SStr1ke1/19/2023
The ' y ' is the Index (ID of the teacher)
Hhiyosilver1/19/2023
I see you are looping once to find the actual number of courses
Hhiyosilver1/19/2023
And then again to find the index of that count
Hhiyosilver1/19/2023
You could probably just do that in one go
SStr1ke1/19/2023
Once to find the teached with the most courses and then to find his index
SStr1ke1/19/2023
yeah
Hhiyosilver1/19/2023
something like
int mostCommon;
int maxCount = 0;
for(int i = 0; i < arr.Length; i++) {
if(arr[i] > maxCount) {
maxCount = arr[i];
mostCommon = i + 1;
}
}
return mostCommon;
Hhiyosilver1/19/2023
Deal with the teacher id like its 0 based for indexing purposes, then only add back one to make it human-readable at the end.
Hhiyosilver1/19/2023
Something like this, I don't know.
Hhiyosilver1/19/2023
There is probably something nicer you can do, but this should work.
SStr1ke1/19/2023
Oh
SStr1ke1/19/2023
wait
SStr1ke1/19/2023
Let me process that real quick xd
Hhiyosilver1/19/2023
actually, let me make a quick edit, that was dumb by me
Hhiyosilver1/19/2023
This is better, so you can just cleanly return
mostCommon
Hhiyosilver1/19/2023
Technically this doesn't cover a case where no teacher has any courses, not sure what you'd want to happen there.
SStr1ke1/19/2023
the question says that there is one for sure
SStr1ke1/19/2023
$code
MMODiX1/19/2023
To post C# code type the following:
```cs
// code here
```
Get an example by typing
If your code is too long, post it to: https://paste.mod.gg/
```cs
// code here
```
Get an example by typing
$codegif
in chatIf your code is too long, post it to: https://paste.mod.gg/
SStr1ke1/19/2023
public int Mostcommon()
{
int[] arr = new int[15];
for(int i = 0; i < PlaceCourse.Length; i++)
{
arr[PlaceCourse[i].GetCode()]++;
}
int MostCommon = arr[0];
int Index = 0;
for (int x = 1; x < arr.Length - 1; x++)
{
if (MostCommon < arr[x])
{
MostCommon = arr[x];
Index = x;
}
}
return Index+1;
}
SStr1ke1/19/2023
Won't it just returd 0+1 every time now?
SStr1ke1/19/2023
return*
Hhiyosilver1/19/2023
Only if Index never gets set.
Hhiyosilver1/19/2023
That would be the case, if arr[0] is already the most common id
Hhiyosilver1/19/2023
also, you are skipping 1 at the end
Hhiyosilver1/19/2023
in your for loop condition
SStr1ke1/19/2023
oy vey
SStr1ke1/19/2023
forgor to delete it from previous
Hhiyosilver1/19/2023
You dont need to do that anymore, since oyu arent comparing neighbouring indices .)
Hhiyosilver1/19/2023
Yeah
SStr1ke1/19/2023
so even tho it's value is redeclared inside a loop
SStr1ke1/19/2023
It will see that?
Hhiyosilver1/19/2023
You aren't redefining it in this case.
Hhiyosilver1/19/2023
Index refers to a variable outside the loop
Hhiyosilver1/19/2023
you are just assigning to it
Hhiyosilver1/19/2023
But that doesn't need to happen, if that assignment never gets hit,
Index
just retains the value you defined at first