Problem adding the units, tens and hundreds of each element of the vector

0

I have a problem adding the units, tens and hundreds of a vector. Showing it in an example:

vector [0] = 123 vector 1 = 456 vector [2] = 78

And that in units there are 17, in tens 14 and in hundreds 5. Adding in vertical line.

for (int i = 0; i < vector.GetLength(0); i++)
        {
            if (vector[i] < 10)
            {
                u = u + vector[i];

            }
            if (vector[i] < 100)
            {
                valor = Convert.ToString(vector[i]);
                d = d + Convert.ToInt32(valor[0]);
                u = u + Convert.ToInt32(valor[1]);
            }
            else
            {
                valor = Convert.ToString(vector[i]);
                c = c + Convert.ToInt32(valor[0]);
                d = d + Convert.ToInt32(valor[1]);
                u = u + Convert.ToInt32(valor[2]);
            }

But I have this problem, I get big numbers, where is the anomaly?

    
asked by Churri 26.05.2018 в 04:07
source

1 answer

0

I do not know if I understood your problem well.

But I guess with this income:

  • vector [0] = 123
  • vector [1] = 456
  • vector [2] = 78

And the result would be,

  • hundreds 1 + 4 = 5
  • tens 2 + 5 + 7 = 14
  • units 3 + 6 + 8 = 17

I did a program in console in .Net Core 2.0 , the algorithm is not to my liking, but they are quick mathematical calculations.

I only checked the happy path with the entry you provided.

namespace Nueva_carpeta
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var u = 0;
            var d = 0;
            var c = 0;

            int[] scores = new int[] { 123, 456, 78};

            for (int i = 0; i < scores.Length; i++)
            {
                c += calcularCentenas(scores[i]);
                d += calcularDecenas(scores[i]);
                u += calcularUnidades(scores[i]);
            }

            Console.WriteLine(c);
            Console.WriteLine(d);
            Console.WriteLine(u);

        }

        static int calcularCentenas(int number){

            if(number < 100) return 0;
            else{
                return  number / 100;
            }
        }

        static int calcularDecenas(int number){
            if (number <= 100) return number / 10;
            else{
                int auxiliar = number / 100;
                int auxiliar2 = auxiliar * 100;
                return (number - auxiliar2) / 10;
            }
        }

        static int calcularUnidades(int number){
                if (number <= 100) {
                int auxiliar = number / 10;
                int auxiliar2 = auxiliar * 10;
                return (number - auxiliar2);
            }
            else{
                int auxiliar = number / 100;
                int auxiliar2 = auxiliar * 100;
                int auxiliar3 = (number - auxiliar2);

                int auxiliar4 = auxiliar3 / 10;
                int auxiliar5 = auxiliar4 * 10;
                return (auxiliar3 - auxiliar5);
            }
        }
    }
}

What I do, is simple and basic. The use of Integer is key.

Any doubt, I answer the why of the algorithm like this. I repeat, it's not very nice.

Greetings

    
answered by 29.05.2018 / 00:50
source