Michael M
Michael M

Reputation: 141

How do I exclude the zeros from outputting in my array?

I have this code. I would like to only display the parts of the array that are greater than 0. For example... A bunch of numbers are entered and stored as a variable. But not all of the variables in the array will be used. The ones that aren't are stored as 0. So when I display the array, the numbers are displayed as: "140, 180, 298, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, " etc. I don't want the zeros to be displayed.

int[] scores = {score1, score2, score3, score4, score5, score6, score7, score8, score9, score10, score11, score12, score13, score14, score15};

Console.WriteLine("Your scores as you entered them: " + (string.Join(", ", scores)));
Console.ReadKey();

Upvotes: 0

Views: 1729

Answers (3)

Gilad Green
Gilad Green

Reputation: 37299

Use linq's Where:

string.Join(", ", scores.Where(x => x != 0))

In the description above you also said the parts of the array that are greater than 0. So if that is the case you can change it to:

string.Join(", ", scores.Where(x => x > 0))

For a non linq solution use a List<int> and a simple foreach (or for) loop:

List<int> result = new List<int>();
foreach(int item in scores)
{
    if(item != 0)
        result.Add(item);
}

Upvotes: 10

apocalypse
apocalypse

Reputation: 5884

If you don't understand LINQ yet, you can look into this piece of code:

int[] scores = { 1, 2, 3, 0, 0, 4, 5, 0, 0, 6};

int[] withoutZeros = WithoutZeros (scores);

And the core method:

public static int[] WithoutZeros (int[] input)
{
    int zerosCount = 0; // we need to count how many zeros are in the input array

    for (int i = 0; i < input.Length; i++)
    {
        if (input[i] == 0) zerosCount = zerosCount + 1;
    }

    // now we know number of zeros, so we can create output array
    // which will be smaller than then input array (or not, if we don't have any zeros in the input array)

    int[] output = new int[input.Length - zerosCount]; // can be smaller, equal, or even empty

    // no we need to populate non-zero values from the input array to the output array

    int indexInOutputArray = 0;

    for (int i = 0; i < input.Length; i++)
    {
        if (input[i] != 0)
        {
            output[indexInOutputArray] = input[i];

            indexInOutputArray = indexInOutputArray + 1;
        }
    }

    return output;
}

Upvotes: 1

Rand Random
Rand Random

Reputation: 7440

To get the answer of your question have a look at Gilad Green's answer.

I just wanted to give you a little feedback about the code I see.

Since, I dont see all your code I am making a lot of assumptions here, so sorry if I am wrong.

If you want to fill an array with 15 values, you should consider refactoring your code to use a loop.

In the following example I will use a for loop but it could be solved with different loops.

int[] scores = new int[15]; //initialize an array of ints with a length of 15
for (int i = 0; i < scores.Length; i++) //loop from 0 till smaller than the length of the array (0-14 = 15 iterations)
    scores[i] = int.Parse(Console.ReadLine()); //fill the scores array with the index of i with the console input

Upvotes: 0

Related Questions