Array return in C#

Asked

Viewed 25 times

0

When I assign a function that returns an array to another array, I do not have the corresponding values of each index as assigned within the function.

I need to return three percentages that are stored in the main function variable, and use them in the precedent to print the percentages. But I only have the return of the value 0, and not the percentage.

Follow the code below:

using System;

namespace poo
{
    class Program
    {
        static void Main(string[] args)
        {
            int numEleitores, numVotosVal, numVotosBran, numVotosNul;
            int[] percentuaisRetornados;
            
            Console.WriteLine("Digite a quantidade de eleitores: ");
            numEleitores = int.Parse(Console.ReadLine());
            Console.WriteLine("Digite a quantidade de votos válidos: ");
            numVotosVal = int.Parse(Console.ReadLine());
            Console.WriteLine("Digite a quantidade de votos brancos: ");
            numVotosBran = int.Parse(Console.ReadLine());
            Console.WriteLine("Digite a quantidade de votos nulos: ");
            numVotosNul = int.Parse(Console.ReadLine());

            percentuaisRetornados = calculoPercentualVotos(numEleitores, numVotosVal, numVotosBran, numVotosNul);
            imprimirPercentuais(percentuaisRetornados, numEleitores);
        }

        static int[] calculoPercentualVotos(int qtdEleitores, int qtdVotosVal, int qtdVotosBran, int qtdVotosNul)
        {
            /**
            * Votos válidos: índice 0;
            * Votos brancos: índice 1;
            * Votos nulos: índice 2;
            */
            int[] percentuaisVotos = new int[3];

            percentuaisVotos[0] = (qtdVotosVal/qtdEleitores) * 100;
            percentuaisVotos[1] = (qtdVotosBran/qtdEleitores) * 100;
            percentuaisVotos[2] = (qtdVotosNul/qtdEleitores) * 100;

            return percentuaisVotos;
        }

        static void imprimirPercentuais(int[] percentArgs, int totalEleitores)
        {
            Console.WriteLine($"O total de eleitores é de: {totalEleitores}");
            Console.WriteLine($"Percentual de votos válidos: {percentArgs[0]}%");
            Console.WriteLine($"Percentual de votos brancos: {percentArgs[1]}%");
            Console.WriteLine($"Percentual de votos nulos: {percentArgs[2]}%");
        }
    }
}
  • Please clarify your specific problem or provide Additional Details to Highlight Exactly what you need. As it’s Currently Written, it’s hard to Tell Exactly what you’re asking.

  • 1

    Returns a tuple (plus) instead of returning a array, is better for several reasons, including not having to comment for using the wrong tool. It won’t solve all the code errors, but it already gets better and solves the reported problem (I don’t even know if it actually occurs, I think it only has a math problem by using an integer in a value that probably gives decimal part.

No answers

Browser other questions tagged

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