C
C#2y ago
Str1ke

✅ (very begginer)

$code
26 Replies
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Str1ke
Str1ke2y ago
public int[] Big(int[,] arr)
{
for(int i = 0; i < arr.GetLength(0); i++)
{
int[] max = new int[arr.GetLength(0)];
max[i] = arr[i, 0];
for(int x = 0; x < arr.GetLength(1); x++)
{
if (max[i] < arr[i, x])
{
max[i]= arr[i, x];
}
}
}
return max;
public int[] Big(int[,] arr)
{
for(int i = 0; i < arr.GetLength(0); i++)
{
int[] max = new int[arr.GetLength(0)];
max[i] = arr[i, 0];
for(int x = 0; x < arr.GetLength(1); x++)
{
if (max[i] < arr[i, x])
{
max[i]= arr[i, x];
}
}
}
return max;
Why I can't return the array max? It says " the name 'max' does not exist in the curretn context"
TheBoxyBear
TheBoxyBear2y ago
Missing a bracket
Str1ke
Str1ke2y ago
there is bracket under
TheBoxyBear
TheBoxyBear2y ago
Also max only exists in the scope of the first for since that's where it's defined
Str1ke
Str1ke2y ago
its just not seen
TheBoxyBear
TheBoxyBear2y ago
$scope
MODiX
MODiX2y ago
you probably want $scopes
TheBoxyBear
TheBoxyBear2y ago
$scopes
MODiX
MODiX2y ago
scope A {
thing a;
scope B {
thing b;
}
}
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
Str1ke
Str1ke2y ago
oh if I create it outside the for and then define it inside for
TheBoxyBear
TheBoxyBear2y ago
Then it will be available to return
Str1ke
Str1ke2y ago
with the variables? or empty?
TheBoxyBear
TheBoxyBear2y ago
When you create it inside for, it also creates the array every iteration of the loop You can define it outside and set the value in the loop But then it will be null if the first loop never runs
Str1ke
Str1ke2y ago
But it will return the value even tho it's defined inside the loop, right?
public int[] Big(int[,] arr)
{
int[] max = new int[arr.GetLength(0)];
for (int i = 0; i < arr.GetLength(0); i++)
{
max[i] = arr[i, 0];
for(int x = 0; x < arr.GetLength(1); x++)
{
if (max[i] < arr[i, x])
{
max[i]= arr[i, x];
}
}

}
return max;
}
public int[] Big(int[,] arr)
{
int[] max = new int[arr.GetLength(0)];
for (int i = 0; i < arr.GetLength(0); i++)
{
max[i] = arr[i, 0];
for(int x = 0; x < arr.GetLength(1); x++)
{
if (max[i] < arr[i, x])
{
max[i]= arr[i, x];
}
}

}
return max;
}
like this
TheBoxyBear
TheBoxyBear2y ago
When you define the array, since it's a nullable type it will start as null if not assigned
Str1ke
Str1ke2y ago
You right and then if I define it inside the for loop
TheBoxyBear
TheBoxyBear2y ago
You can only reference things defined in the same scope or an outer scope
Str1ke
Str1ke2y ago
will it return it's values that he got inside the loop?
TheBoxyBear
TheBoxyBear2y ago
If it was assigned that then yes Whenever the loop iterates, a new array is created and the old one is discarded
Str1ke
Str1ke2y ago
oh yeah that was dumb xd
Buddy
Buddy2y ago
Please name your title better
Str1ke
Str1ke2y ago
I didn't think about that
Buddy
Buddy2y ago
be more descriptive of what the issue is in your title
Str1ke
Str1ke2y ago
I will be sure to do so next time Thank you BoxyBear ♥️
TheBoxyBear
TheBoxyBear2y ago
np
Want results from more Discord servers?
Add your server
More Posts