C
C#•10mo ago
AhmadHija

i need help understanding this

No description
36 Replies
AhmadHija
AhmadHijaOP•10mo 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•10mo 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
AhmadHija
AhmadHijaOP•10mo ago
The i+1<a.length is to stay inside the bounds pf the array right ?
Angius
Angius•10mo ago
Seems like a reasonable assumption, yes
AhmadHija
AhmadHijaOP•10mo ago
Can it stay open and i wait for someone to come His name is leowest
Angius
Angius•10mo ago
I mean, sure
AhmadHija
AhmadHijaOP•10mo ago
Ty anyway dude Appreciate it
leowest
leowest•10mo 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
AhmadHija
AhmadHijaOP•10mo ago
Bro i have till 22/2 I aint rushing anything We have 50 questions to answer
leowest
leowest•10mo 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 😉
AhmadHija
AhmadHijaOP•10mo ago
Noo bc you helped solve the question you know what im talking about Im not sayin that they cant
leowest
leowest•10mo ago
alright I back let me know if you're around @ahmadhija🌵
AhmadHija
AhmadHijaOP•10mo ago
Was sleeping
leowest
leowest•10mo 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
AhmadHija
AhmadHijaOP•10mo 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•10mo ago
$code
MODiX
MODiX•10mo 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/
AhmadHija
AhmadHijaOP•10mo ago
BlazeBin - haxpnhxiynav
A tool for sharing your source code with the world!
AhmadHija
AhmadHijaOP•10mo ago
@leowest Its ready It should print 2,3,5,1 But it says index out of array bounds
Angius
Angius•10mo 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?
AhmadHija
AhmadHijaOP•10mo ago
No
Angius
Angius•10mo ago
Exactly
AhmadHija
AhmadHijaOP•10mo ago
So i need to remove the i+1 Bc it will skip one
Angius
Angius•10mo ago
Or you need to prevent that check from happening For example by checking if i + 1 is out of range
AhmadHija
AhmadHijaOP•10mo ago
But it is in if If(i+1<a.length)
Angius
Angius•10mo ago
Huh, true In that case, I can only recommend debugging
AhmadHija
AhmadHijaOP•10mo ago
I will wait for @leowest bc he helped me Ty anyway
Angius
Angius•10mo 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•10mo 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.
AhmadHija
AhmadHijaOP•10mo ago
No ik I cant figure out whtas the problem
leowest
leowest•10mo ago
Well I can't help you if when im online you're not online...
AhmadHija
AhmadHijaOP•10mo ago
im here i guess @leowest
leowest
leowest•10mo ago
hey 🙂 I will be back in 30~40 minutes lunch if ur still here we can do this
AhmadHija
AhmadHijaOP•10mo 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•10mo ago
ALRIGHT so can you tell me what your teacher asked u to do before we go over your code again @ahmadhija🌵
AhmadHija
AhmadHijaOP•10mo ago
yeaaa
Want results from more Discord servers?
Add your server