Problem:
Write an algorithm that asks the user to enter the number n of positive integers to be read, then reads these n integers and places them in an array so that first come the prime numbers, last the numbers even , and in between the numbers that are neither prime nor even .
In each group, the numbers must be placed according to their order of arrival.
For example, if the user enters the following numbers:
14, 23, 17, 18, 13, 15, 12, 3, 42, 21
then the table will be as follows:
23 17 13 3 15 21 14 18 12 42
My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace HelloWorld
{
public class Program
{
public static void Main(string[] args)
{int n, i, value, f, j, nbeven, nbprime, k,p,q, aux;
int[] t;
Console.WriteLine("give n:");
n=int.Parse(Console.ReadLine());
t=new int[n];
nbeven=0;nbprime=0;
for(i=0;i<n;i++)
{
Console.WriteLine("give value:");
t[i]=int.Parse(Console.ReadLine());
if(t[i]%2==0)
nbeven=nbeven+1;
else
{
f=2;
for(j=2;j<=t[i]/2;i++)
{
if(t[i]%j==0)
f=f+1;
}
if(f==2)
nbprime=nbprime+1;
}
}
k=0;p=0;q=0;
for(i=0;i<n;i++)
{
if(t[i]%2==0)
{
aux=t[i];
t[i]=t[n-nbeven+k];
t[n-nbeven+k]=aux;
k=k+1;
}
else
{
f=2;
for(j=2;j<=t[i]/2;j++)
{
if(t[i]%j==0)
f=f+1;
if(f==2)
{
aux=t[i];
t[i]=t[p];
t[p]=aux;
p=p+1;
}
else
{
aux=t[i];
t[i]=t[nbprime+q];
t[nbprime+q]=aux;
q=q+1;
}
}
}
}
for(i=0;i<n;i++)
Console.Write("{0} ",t[i]);
}
}
}
Error:
Unhandled exception: index was outside the bounds of the array.
Why am i getting this error?