C
C#•15mo ago
Shark

i need help understanding this

No description
36 Replies
Shark
SharkOP•15mo ago
What the first if does In this code static void Main(string[] args) { int[] a = { 1, 2, 3, 3, 5, 1,1 }; Print(Q15P117(a)); Console.ReadKey(); } static int[] Q15P117(int[] a) { int c = 0; int save = 0; for (int i = 0; i < a.Length; i++) { save = a[i]; if (i + 1 < a.Length && CheckRepeat(a, a[i])&& a[i+1]!=save) c++; else if (!CheckRepeat(a, a[i])) c++; } int[] b = new int[c]; int k = 0; for (int i = 0; i < a.Length; i++) { if (!CheckArray(b, a[i])) { b[k] = a[i]; k++; } } return b; } static void Print(int[] a) { for (int i = 0; i < a.Length; i++) Console.Write("{0} ", a[i]); Console.WriteLine(); } static bool CheckRepeat(int[] a, int num) { int c = 0; for (int i = 0; i < a.Length; i++) { if (a[i] == num) c++; } if (c > 1) return true; else return false; } static bool CheckArray(int[] arr, int num) { for (int i = 0; i < arr.Length; i++) { if (arr[i] == num) return true; } return false; }
Angius
Angius•15mo ago
Checks a few conditions
i + 1 < a.Length
&& // and
CheckRepeat(a, a[i])
&& // and
a[i+1] != save
i + 1 < a.Length
&& // and
CheckRepeat(a, a[i])
&& // and
a[i+1] != save
Shark
SharkOP•15mo ago
The i+1<a.length is to stay inside the bounds pf the array right ?
Angius
Angius•15mo ago
Seems like a reasonable assumption, yes
Shark
SharkOP•15mo ago
Can it stay open and i wait for someone to come His name is leowest
Angius
Angius•15mo ago
I mean, sure
Shark
SharkOP•15mo ago
Ty anyway dude Appreciate it
leowest
leowest•15mo ago
I asked u to open this so others could help u it will take me a few hours for me to be available again. if u want to wait that much instead of having some one else assist u that is fine, otherwise you could let @ZZZZZZZZZZZZZZZZZZZZZZZZZ help u
Shark
SharkOP•15mo ago
Bro i have till 22/2 I aint rushing anything We have 50 questions to answer
leowest
leowest•15mo ago
that's fine all im sayings is that others can help u as much or better than I can 🙂 I will be back in like 2 to 3 hours from now anyway bbl 😉
Shark
SharkOP•15mo ago
Noo bc you helped solve the question you know what im talking about Im not sayin that they cant
leowest
leowest•15mo ago
alright I back let me know if you're around @ahmadhija🌵
Shark
SharkOP•15mo ago
Was sleeping
leowest
leowest•15mo ago
well let me know when you're available again, I just got up will be free for a few hours then I have work
Shark
SharkOP•15mo ago
Hi @leowest I found a problem in the code 2,2,3,3,5,1,1 Try this in the code static void Main(string[] args) { int[] a = { 2, 2, 3, 3, 5, 1,1 }; Print(Q15P117(a)); Console.ReadKey(); } static int[] Q15P117(int[] a) { int c = 0; int save = 0; for (int i = 0; i < a.Length; i++) { save = a[i]; if (i + 1 < a.Length && CheckRepeat(a, a[i])&& a[i+1]!=save) c++; else if (!CheckRepeat(a, a[i])) c++; } int[] b = new int[c]; int k = 0; for (int i = 0; i < a.Length; i++) { if (!CheckArray(b, a[i])) { b[k] = a[i]; k++; } } return b; } static void Print(int[] a) { for (int i = 0; i < a.Length; i++) Console.Write("{0} ", a[i]); Console.WriteLine(); } static bool CheckRepeat(int[] a, int num) { int c = 0; for (int i = 0; i < a.Length; i++) { if (a[i] == num) c++; } if (c > 1) return true; else return false; } static bool CheckArray(int[] arr, int num) { for (int i = 0; i < arr.Length; i++) { if (arr[i] == num) return true; } return false; }
Angius
Angius•15mo ago
$code
MODiX
MODiX•15mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Shark
SharkOP•15mo ago
BlazeBin - haxpnhxiynav
A tool for sharing your source code with the world!
Shark
SharkOP•15mo ago
@leowest Its ready It should print 2,3,5,1 But it says index out of array bounds
Angius
Angius•15mo ago
You're looping, in line 11, from 0 to the length of a In line 14, you're trying to access index i+1 What happens when the loop gets to the last index? What will be i+1? Will it be an index that exists?
Shark
SharkOP•15mo ago
No
Angius
Angius•15mo ago
Exactly
Shark
SharkOP•15mo ago
So i need to remove the i+1 Bc it will skip one
Angius
Angius•15mo ago
Or you need to prevent that check from happening For example by checking if i + 1 is out of range
Shark
SharkOP•15mo ago
But it is in if If(i+1<a.length)
Angius
Angius•15mo ago
Huh, true In that case, I can only recommend debugging
Shark
SharkOP•15mo ago
I will wait for @leowest bc he helped me Ty anyway
Angius
Angius•15mo ago
If you don't want to learn how to debug code and would prefer to wait however long, then sure I'll just drop $debug here
MODiX
MODiX•15mo ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Shark
SharkOP•15mo ago
No ik I cant figure out whtas the problem
leowest
leowest•15mo ago
Well I can't help you if when im online you're not online...
Shark
SharkOP•15mo ago
im here i guess @leowest
leowest
leowest•15mo ago
hey 🙂 I will be back in 30~40 minutes lunch if ur still here we can do this
Shark
SharkOP•15mo ago
Hi can you get on in like 19:00 45 minutes I will eat and come back yea bro they was more like hour and half but im here dude xD
leowest
leowest•15mo ago
ALRIGHT so can you tell me what your teacher asked u to do before we go over your code again @ahmadhija🌵
Shark
SharkOP•15mo ago
yeaaa

Did you find this page helpful?