I tried using different compilers even internet sites for c# but nothing will output I don't know why. This is just practise for a test in school, the idea is to use the "netherland flag" algorithm or the tricolored flag algorithm.
static void Main(string[] args)
{
int[] a = { 14, 18, 6, 4, 7, 12, 3, 9, 11 };
int t = 0;
int o = a.Length;
int i = 0;
int privremeno = 0;
while (i<9)
{
if (a[i]%3==0)
{
privremeno = a[i];
a[t] = a[i];
a[i] = privremeno;
t ;
}
else if (a[i]%3!=0 && a[i]%7!=0)
{
privremeno = a[i];
a[o] = a[i];
a[i] = privremeno;
o--;
}
if (o==i)
{
break;
}
}
for (int j = 0; j < 9; j )
{
Console.WriteLine(a[j]);
}
}```
CodePudding user response:
nothing will output I don't know why.
You wrote while(i<9)
but you don't appear to ever increment i so your loop runs forever .. the code never reaches a point where anything is output
CodePudding user response:
You forgot to increment your i
loop variable so its value is always 0
.
Instead, why not use a for
loop or a foreach
loop since you don't really need to know what loop iteration you're on.
for ( int i = 0; i < 9; i )
Or use:
foreach ( var num in a )
Also, you need to change this:
int o = a.Length;
To this:
int o = a.Length - 1;
If you don't you will get an IndexOutOfRangeException
the first time the application enters the else if (a[i]%3!=0 && a[i]%7!=0)
branch.