0

I am trying to output the odd and even numbers of an array, i have got it all working but for some reason when i create a foreach loop and output each number that is odd/even it starts with a zero?

The way it is setup that the user inputs 10 numbers with a comma in between each letter (Starts as a string) and then it removes the comma and converts the numbers into an int and places it in an int array, to then be used in the other class. I input 1,2,3,4,5,6,7,8,9,1 to test it.

    Odds(ai.Odds);
    Evens(ai.Evens);



    Console.ReadKey();
}
    public static void Odds(int[] odds)
    {
        Console.WriteLine("\nOdds:");
        foreach (var odd in odds)
        {
            Console.Write(odd + " ");   
        }
    }

public static void Evens(int[] evens)
{
    Console.WriteLine("\nEvens:");
    foreach (var even in evens)
    {
        Console.Write(even + " ");
    }
}

and in the separate class doing the magic:

public int[] Odds
        {
            get
            {
                int count = 0;
                int[] odds = new int[NumArray.Length];
                string[] oddNums = {"1", "3", "5", "7", "9"};
                foreach (int num in NumArray)
                {
                    foreach (string oddNum in oddNums)
                    {
                        if (num.ToString().EndsWith(oddNum))
                        {
                            odds[count++] = num;
                        }
                    }
                }
                Array.Sort(odds);
                return RemoveDuplicates(odds);
            }
        }

        public int[] Evens
        {
            get
            {
                int count = 0;
                int[] evens = new int[NumArray.Length];
                string[] evenNum = {"0", "2", "4", "6", "8"};
                foreach (int num in NumArray)
                {
                    foreach (string oddNum in evenNum)
                    {
                        if (num.ToString().EndsWith(oddNum))
                        {
                            evens[count++] = num;
                        }
                    }
                }
                Array.Sort(evens);
                return RemoveDuplicates(evens);
            }
        }

Here is the RemoveDuplicates Method:

private int[] RemoveDuplicates(int[] numbers)
        {
            if (numbers.Length == 0)
            {
                return numbers;
            }
            int[] uniques = new int[numbers.Length];

            int previousVal = numbers[0];
            uniques[0] = numbers[0];
            int count = 1;
            for (int i = 1; i < numbers.Length; i++)
            {
                if (numbers[i] == previousVal) continue;
                previousVal = numbers[i];
                uniques[count++] = numbers[i];
            }
            Array.Resize(ref uniques, count);
            return uniques;
        }

as an output i get(vvv), but i don't want the 0's at the beginning of them both, i don't want a 0 at all

Alexandru Chichinete
  • 1,166
  • 12
  • 23
Brendon
  • 334
  • 1
  • 2
  • 14
  • 2
    It would make it a lot easier to help you if you would post a single short-but-complete example program, formatting it appropriately, rather than bits and pieces starting with code half way through a method. – Jon Skeet Jan 08 '16 at 16:20
  • *I input 1,2,3,4,5,6,7,8,9,1 to test it.*, it would be useful to include what the output is from that input and what you expected it to be. – Matt Burland Jan 08 '16 at 16:21
  • Also, that's a pretty horrible way to determine if something is odd or even. Rather than relying on the last digit in a string, the easy way to deal with it is to convert your string to a number and then do `num%2`, which will be `0` if even and `1` if odd. – Matt Burland Jan 08 '16 at 16:24
  • @MattBurland: Or -1 if it's odd and negative... I usually use `num & 1` instead. – Jon Skeet Jan 08 '16 at 16:29
  • 1
    @JonSkeet: Fair point, but I usually only test if `num%2 == 0`, so if odd is `1` or `-1` isn't a concern (if it's not even, then it's odd). But I do like the idea of `& 1`, I might have to use that from now on. – Matt Burland Jan 08 '16 at 16:32
  • i didn't want to put the whole code because it is quite a big piece of code as there are other things that i am trying to gather from the array. I can add some more of the first bit of code i put in there if you want? – Brendon Jan 08 '16 at 16:35
  • Also, as an aside (because again, I assume this is homework), there are better ways to remove duplicates from an array of `int`. Use [Enumerable.Distinct](https://msdn.microsoft.com/library/bb348436(v=vs.100).aspx). – Matt Burland Jan 08 '16 at 16:45

2 Answers2

6

but i don't want the 0's at the beginning of them both, i don't want a 0 at all

Then you shouldn't create an array with 0s in, which is what you're doing.

Before you call Array.Sort(odds), your array will have all the odd numbers from NumArray, followed by a bunch of 0 values (as many as there are even values in NumArray), because when you construct an array, it is automatically filled with the default value of the element type for each element. You'll only have no 0 values if the whole of NumArray is odd.

You only want as many elements as you need, so it would be better to use List<int>, and just add to that when you need to. You can call ToArray() to create an array at the end, if you really need to.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for the comment. Without the Array.Sort(odds) it puts the 0 at the end. I am putting it into an array, as it is a task that i have been assigned and it requires an array. This task also has other little task in it other than trying to find evens and odds, these others also need an array to work. – Brendon Jan 08 '16 at 16:32
  • 2
    @Brendon: Sure, so create a `List` with just what you need, and call `ToArray()` afterwards. Or change the rest of your code so that it's not as inflexible :) – Jon Skeet Jan 08 '16 at 16:33
  • So on that particular property, put NumArray into a List? and then call .ToArray() at the end? – Brendon Jan 08 '16 at 16:37
  • @Brendon: No, not `NumArray` - but `odds` (and ditto for `evens`). Basically while you're building up the list of odd (or even) numbers. – Jon Skeet Jan 08 '16 at 16:39
  • oh ok i get it now, ill test that quick then ill get back to you thanks – Brendon Jan 08 '16 at 16:40
  • 1
    @Brendon: If for some reason you cannot use `List` (because, I'm assuming this is some kind of homework assignment), then your alternative would be to resize you `odd` and `even` arrays *before* you sort them, thus removing the extra zeroes before the sort. – Matt Burland Jan 08 '16 at 16:43
  • Thank you very much, that has worked perfectly. It has removed the zeros from the beginning. Thanks – Brendon Jan 08 '16 at 16:43
  • I was ok to change the odds and evens array to a list it was just that i couldnt change NumArrays to a list that was where i was getting confused. – Brendon Jan 08 '16 at 16:44
-1

When you're assigning values to the evens and odds arrays, you're using [count++] as the index. This means that it's never assigning index 0 and it'll have a default value of 0 from when the array was initialised.

Andy
  • 49,085
  • 60
  • 166
  • 233
John Clifford
  • 821
  • 5
  • 10
  • Nope, `count++` will result in 0 for the first iteration, because it's a *post-increment*, not a *pre-increment*. The 0 values will be at the *end* of the array. – Jon Skeet Jan 08 '16 at 16:25
  • As Conrad pointed out, the only time post-increment and pre-increment are relevant is when displaying the values; in terms of logic it makes no difference. – John Clifford Jan 08 '16 at 16:27
  • 1
    Um, no. `count++` increments the variable but the result of the expression is the *original* value. Try it! `int[] x = new int[10]; int count = 0; x[count++] = 5; Console.WriteLine(x[0]);` That will print 5, so it *is* assigning to index 0. – Jon Skeet Jan 08 '16 at 16:29
  • 1
    @JohnClifford: post- vs pre- increment absolutely makes a difference. Not only when you display a value. – Matt Burland Jan 08 '16 at 16:29
  • The count++ is to put the even or odd number in that place in the new array – Brendon Jan 08 '16 at 16:33
  • Thanks trying anyay :) – Brendon Jan 08 '16 at 16:38
  • Man, it must have been a longer day at work than I thought. I totally deserved that downvote: I reread this when I got home from the office and facepalmed at what an idiot I made myself look. Of course that isn't what caused it; I have code in MY OWN WORK PROJECTS that relies on pre/post-increment working exactly the opposite way that I said. My only defence is that I was so excited to finally be able to post something useful on SO. Sorry for wasting your time. XD – John Clifford Jan 08 '16 at 21:21
  • Haha don't worry about it we always have mistakes. You didn't waste my time don't worry xD. – Brendon Jan 11 '16 at 16:40