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

7
  • 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. Commented Jan 8, 2016 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. Commented Jan 8, 2016 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. Commented Jan 8, 2016 at 16:24
  • @MattBurland: Or -1 if it's odd and negative... I usually use num & 1 instead. Commented Jan 8, 2016 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. Commented Jan 8, 2016 at 16:32

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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: Sure, so create a List<int> with just what you need, and call ToArray() afterwards. Or change the rest of your code so that it's not as inflexible :)
So on that particular property, put NumArray into a List? and then call .ToArray() at the end?
@Brendon: No, not NumArray - but odds (and ditto for evens). Basically while you're building up the list of odd (or even) numbers.
@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.
|
-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.

8 Comments

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.
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.
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.
@JohnClifford: post- vs pre- increment absolutely makes a difference. Not only when you display a value.
The count++ is to put the even or odd number in that place in the new array
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.