Pergunta

I have this code I'm using to create a program that takes a a range and outputs to the Console the prime numbers. I have one problem, I'm trying to iterate through the array I built so the loop should only write to the console the values that are prime using my method's return value. The problem I'm having is that I have the second condition set to numArray.Length but it seems to give me the Index out of Range Exception. I just want the loop to iterate through all values in the numArray and stop when it's done figuring out whether the last value is prime or not.

public struct Prime
{
    public int x;


    // constructor for Prime
    public Prime(int x1)
    {
        x = x1;

    }

    public int IsPrime(int number)
    {
        int i;
        for (i = 2; i * i <= number; i++)
        {
            if (number % i == 0) return 0;
        }
        return 1;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter an Integer");
        int num1 = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter a Second Integer of Greater Value");
    //    int num2 = 0;
        int num2 = Convert.ToInt32(Console.ReadLine());

    /*    if (num2temp > num1)
        {
            num2temp = num2;
        }
        else
        {
            Console.WriteLine("You Did Not Enter An Integer Greater Than the First Integer, Please Enter Your Integers Again.");
            Environment.Exit(0);
        }
    */    int index = 1;
        int[] numArray = new int[num2];
        for (int i = num1; i <= num2; i++)
        {
            numArray[index] = i;

            Console.WriteLine(" index: {0} assignment: {1}", index, i);
            index++;
            Console.WriteLine("index: {0}",index);
        }

        Console.WriteLine("value: {0}", numArray[40]);

    /*    Prime myprime = new Prime();

        if (myprime.IsPrime(numArray[12]) == 1)
        {
            Console.WriteLine("true");
        }
        else
        {
            Console.WriteLine("False");
        } */


        Prime myprime = new Prime();


       int value = 0;
        for (int y = 1; y <= num2; y++)
        {
            if (myprime.IsPrime(numArray[y]) == 1)
            {
                value = numArray[y];
                Console.Write("{0} \t", value);

            }

        } 
Foi útil?

Solução

You're currently trying to iterate up to and including the size of the array. Arrays are 0-indexed in C#. So this:

int[] numArray = new int[num2];
for (int i = num1; i <= num2; i++)

should be:

for (int i = num1; i < num2; i++)

And note that to get at the first element array, num1 would have to be 0, not 1.

Likewise, your initial assignment of index as 1 should be 0 instead. Basically you need to go through all your code (it's confusing at the moment with lots of bits commented out) and check everywhere that you're assuming arrays are 1-based, and instead change your code as they're 0-based.

(In some cases you may just want to make the array one bigger, of course. If you want an array which logically contains the values 1 to x inclusive, you can either create an array of size x and subtract one from each index all the time, or create an array of size x + 1.)

Outras dicas

Your index starts from 1 but should start from 0:

int index = 0; //CHANGE HERE
int[] numArray = new int[num2];
for (int i = num1; i <= num2; i++)
{
    numArray[index] = i;

    Console.WriteLine(" index: {0} assignment: {1}", index, i);
    index++;
    Console.WriteLine("index: {0}",index);
}

and then here y should also be 0 and check should if it is less than num2:

for (int y = 0; y < num2; y++)
{
    if (myprime.IsPrime(numArray[y]) == 1)
    {
        value = numArray[y];
        Console.Write("{0} \t", value);

    }

} 

because array indexing in C# start from 0.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top