Show prime numbers in a vector in C#

Asked

Viewed 887 times

0

My code searches for prime numbers in a vector of 10 positions and displays only the prime numbers themselves, only the following error prevents their execution:

CS0165 Error: Unassigned local variable usage "prime"

using System;

namespace Vetor1
{
    class Program2
    {
        static int Main(string[] args)
        {
            var vetor = new int[10];
            int i, posicao, numeroprimo;

            for (i = 0; i < vetor.GetLength(0); i++)
            {
                Console.Write($"Digite o {i}° número: ");
                vetor[i] = int.Parse(Console.ReadLine());
                do
                {
                    if (vetor[i] <= 0)
                    {
                        Console.Write($"Digite o {i}° número: ");
                        vetor[i] = int.Parse(Console.ReadLine());
                    }
                } while (vetor[i] <= 0);

            }

            for (i = 0; i < vetor.GetLength(0); i++)
            {
                if (vetor[i] > 1)
                {
                    for (i = 2; i <= vetor.GetLength(0) - 1; i++)
                    {
                        if (vetor[i] % i == 0)
                        {
                            numeroprimo = 0;
                            break;
                        }
                    }
                    if (numeroprimo == 1)
                    {
                        posicao = i;
                        Console.WriteLine($"Posição {posicao}");
                    }
                }
            }
            Console.ReadKey();
            return 0;
        }
    }
}

2 answers

6


The error itself is exactly what is written, you need to assign a value to the variable numeroprimo, then you should do numeroprimo = 0.

There are several other code problems that do not prevent it from working, including it is not idiomatic in C#, so it is learning to do strange things for most C#programmers. And it’s not performatic. And it’s pretty redundant.

much better this way:

using static System.Console;
using static System.Math;

namespace Vetor1 {
    class Program {
        static int Main(string[] args) {
            var vetor = new int[10];
            for (int i = 0; i < vetor.Length; i++) {
                do {
                    Write($"Digite o {i}° número: ");
                    if (!int.TryParse(ReadLine(), out vetor[i])) {
                        WriteLine("Valor inválido, tente de novo.");
                        continue;
                    }
                } while (vetor[i] < -1);
            }
            for (int i = 0; i < vetor.Length; i++) if (EhPrimo(vetor[i])) WriteLine($"Posição {i}");
            return 0;
        }
        public static bool EhPrimo(int numero) {
            if (numero <= 1) return false;
            if (numero == 2) return true;
            if (numero % 2 == 0) return false;
            var limite = (int)Floor(Sqrt(numero));
            for (int i = 3; i <= limite; i += 2) if (numero % i == 0) return false;
            return true;        
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • I hadn’t noticed that you coded, went to see the code I posted and fixed some things and posted my own corrected code.

  • That previous question of mine to identify if an element of the vector is empty (no value typed) I still don’t understand what you mean.

0

After looking at certain things I ended up correcting and the code was like this:

using System;

namespace Vetor1
{
    class Program2
    {
        static int Main(string[] args)
        {
            var vetor = new int[10];
            int i, posicao;

            for (i = 0; i < vetor.GetLength(0); i++)
            {
                Console.Write($"Digite o {i}° número: ");
                vetor[i] = int.Parse(Console.ReadLine());
                do
                {
                    if (vetor[i] <= 0)
                    {
                        Console.Write($"Digite o {i}° número: ");
                        vetor[i] = int.Parse(Console.ReadLine());
                    }
                } while (vetor[i] <= 0);

            }

            for (i = 0; i < vetor.GetLength(0); i++)
            {
                if (vetor[i] % 2 == 1)
                {
                    posicao = i;
                    Console.WriteLine($"Número {vetor[i]} é primo e está na posição {posicao}");
                }
            }
            Console.ReadKey();
            return 0;
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.