How to stop printing positions = 0 of a vector?

Asked

Viewed 110 times

0

In the example below, how I would fail to print the positions of this vector that are equal to 0

int[] caluculo = {50,6,0,59,6,7,0,6,8};

for(int i = 0 ; i < 9 ; i++)
{    
    Console.WriteLine("{0}",calculo[i]);    
}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

5

Whenever you need to filter something you should if:

for (var i = 0 ; i < 9 ; i++) if (calculo[i] != 0) WriteLine(calculo[i]);

Or you can do

foreach (var item in calculo) if (item != 0) WriteLine(item);

I put in the Github for future reference.

You can also use LINQ, but I don’t think it pays and it’s more advanced.

Browser other questions tagged

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