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