Recursive Algorithm does not work

2

I have to solve an exercise, and it says:

  

Do a procedure that receives an integer n positive number. O   procedure should print all numbers in the range between 0 and n   which are divisible by 2 and 3 (simultaneously).

I'm starting at the moment, and I have little luggage in data structure. I've developed the code below, but it does not return the numbers that are divisible.

using System;

namespace revisao
{
class Program
    {               
        static void Imprimir(int[] vet, int indice)
        {               
            if (indice > 0 && indice < vet.Length)
            {
                if((vet[indice] % 2 == 0) && (vet[indice] % 3 == 0))
                {
                    Console.Write("{0,3}", vet[indice]);    
                    Imprimir(vet, indice + 1);
                }
                else
                {
                    Console.Write("Números não divisíveis por 2 e 3 simultaneamente!");
                }
            }
        }

        public static void Main(string[] args)
        {
            int i, tamanho;

            Console.Write("Digite o tamanho do vetor: ");
            tamanho = Convert.ToInt32(Console.ReadLine());

            int[] vetor = new int[tamanho];

            for(i = 0; i < vetor.Length; i++)
            {
                Console.Write("Digite o elemento {0}: ", i);
                vetor[i] = Convert.ToInt32(Console.ReadLine());
            }

            Imprimir(vetor, i);

            Console.ReadKey();
        }
    }
}
    
asked by anonymous 08.10.2017 / 18:55

2 answers

1

I've improved some things for the code to be more C # style. I also solved some other problems that the code had that are not in the statement, but if it does not deal it gives error. For lack of a specific definition when something is spelled wrong I assumed what I wanted to solve.

I think the difficulty is the recursive function that the statement does not ask for and there is no need to use it there. This is not a clearly recursive case, it is sequential, so a loop is more interesting.

If you are using an old version you have to declare the variable tamanho out of the TryParse() function before to use.

using static System.Console;

namespace revisao {
    public class Program {   
        public static void Main(string[] args) {
            Write("Digite o tamanho do vetor: ");
            if (int.TryParse(ReadLine(), out var tamanho)) {
                int[] vetor = new int[tamanho];
                for(int i = 0; i < vetor.Length; i++) {
                    Write($"Digite o elemento {i}: ");
                    vetor[i] = int.TryParse(ReadLine(), out var valor) ? valor : 0;
                }
                Imprimir(vetor);
            }
        }
        private static void Imprimir(int[] vetor) {
            WriteLine("Valores divisíveis por 2 e 3 simultaneamente");
            foreach (var item in vetor) {
                if (item % 6 == 0) {
                    WriteLine($"{item}");
                }
            }
        }
    }
}

See running on .NET Fiddle . Also I put GitHub for future reference .

    
08.10.2017 / 19:18
1

You are asked to print the numbers from 0 to n , n supplied, that are divisible by 2 and 3 simultaneously. As @Maniero realized, it needs to be a multiple of 6.

As the question does not say anything about storing in a vector, or in subsequent entries, there is only one reading for each instance of the problem. This is the point I was dissatisfied with his response and also with the AP's attempt to respond.

Since the initial number is 0, and 0 because it is an absorbent element in the ring of integers is a multiple of all other numbers, just start from 0 and increment from 6 by 6 units until you get to n (I'm assuming open range in n ). Based on the Maniero code:

using static System.Console;

namespace revisao {
    public class Program {   
        public static void Main(string[] args) {
            Write("Digite n: ");
            if (int.TryParse(ReadLine(), out var n)) {
                WriteLine("Valores divisíveis por 2 e 3 simultaneamente");
                for (int i = 0; i < n; i += 6) {
                    WriteLine($"{i}");
                }
            }
        }
    }
}

Only multiples of 2 and 3 are printed simultaneously.

The recursive function for this would be kind of rough:

public static void imprimeRecursivo(int atual, int n) {
    if (atual < n) {
        WriteLine($"{atual}");
         imprimeRecursivo(atual + 6, n);
    }
}

The first call must pass as atual 0.

If you want to turn it into a head recursion, you'll only need one parameter:

public static void imprimeRecursivoCabeca(int i) {
    if (i >= 0) {
        imprimeRecursivoCabeca(i-1);
        if (i % 6 == 0) {
            WriteLine($"{i}");
        }
    }
}

If you want the interval open in% with%, the first parameter is% with%. Note that it will converge to 0, where it will begin to return from recursions, printing in ascending order the multiple numbers of 6 found.

    
10.10.2017 / 15:53