Error in if ultimalization

Asked

Viewed 68 times

1

My intention with this code is to show how many numbers are odd and how many are even. (e.g. Odd: 3 Even: 2).

for (int i = 0; i < 6;i++)
{
     int SePar, SeImpar, cont;

     SePar = 0;
     SeImpar = 0;
     cont = 0;                               

     if (atividade[i] % 2 == 0)
     {
         SePar = SePar + 1;
     }

     if (atividade[i] % 2 != 0)
     {
         SeImpar = SeImpar + 1;                  
     }

     cont = cont + 1;

     if (cont == 6)
     {
         Console.WriteLine("par: " + SePar + "Impar: " + SeImpar);
     }
}

inserir a descrição da imagem aqui

3 answers

3

First:

The declaration of variables that count numbers that are even and odd should be made outside the loop for, otherwise they will always be reset to each executed loop.

According to:

Your variable cont, can be replaced by i for it is he who makes the bond continue through i++.

Third:

In this case, the test to check if your index (I changed from cont for i) is == 6 will never be hit because the loop starts at 0 and goes up to 6, ie it actually goes up to 5.

Code as it should be:

public static void par(int[] atividade)
{
    int SePar, SeImpar;

    SePar = 0;
    SeImpar = 0;

    for (int i = 1; i < 6;i++)
    {                                                
        if (atividade[i] % 2 == 0)
        {
            SePar = SePar + 1;
        }

        if (atividade[i] % 2 != 0)
        {
            SeImpar = SeImpar + 1;
        }

        if (i == 5)
        {
            Console.WriteLine("par: " + SePar + " Impar: " + SeImpar);
        }
    }
}
  • Actually the 6 was part of the error, but it is not counting the number.

  • If your activity vector[] has no value, it will count no amount. What you asked in the question was one thing and now you want another.

  • Values are entered by the user previously.

  • Yeah, I checked the other questions you asked earlier that you were having second thoughts about that. After you read the user data and fill in the vector, enter the code above. That is, first your program will ask the user to enter the values. Once this is done, the above code will scan your vector with the numbers and count how many are even and how many are odd.

  • Follow the link as you are now, see what happens in the last part (https://dotnetfiddle.net/tCP7u1).

  • I just updated your code. I will edit my answer.

  • A lot has changed? I am without a computer now.

  • No, I just changed what I reported in my reply, took the statement of variables from inside the for and put them out. I updated my answer with your method. Copy it and replace it with your method in fiddle and run. It will work.

  • I will check as soon as possible and return.

  • I stand by.

  • It worked I still have much to learn, grateful for the help.

  • No problem, happy to help. Remember to mark as an answer if I helped you! Thanks.

Show 7 more comments

2

There is a logic error there, Count will always be 0, as it is being initialized within the for loop

The correct is:

int SePar, SeImpar, cont;

SePar = 0;
SeImpar = 0;
cont = 0;  

for (int i = 0; i < 6;i++)
{



     if (atividade[i] % 2 == 0)
     {
         SePar = SePar + 1;
     }

     if (atividade[i] % 2 != 0)
     {
         SeImpar = SeImpar + 1;                  
     }

     cont = cont + 1;


}
 if (cont == 5)
 {
    Console.WriteLine("par: " + SePar + "Impar: " + SeImpar);
 }

0

Only to demonstrate the same code, but refactored.

var atividade = new [] {1,2,3,4,5,6};
var totalImpar = 0;
for (var i = 0; i < atividade.Length; i++)
{
    totalImpar += atividade[i] % 2 == 0
        ? 0
        : 1;
}
Console.WriteLine($"Pares: {atividade.Length - totalImpar}  Impares: {totalImpar}");

See working on Netfiddle.

  • I need him to check the user’s input values.

  • But that you know how to do, and you’re already doing, from what I saw in your question. :)

Browser other questions tagged

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