Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of odds = 81 I just cant seem to figure it out in the code.

``````            Console.WriteLine("");
Console.WriteLine("Problem #3 For loop - Add Up the Odds or Evens");
Console.WriteLine("");

Console.WriteLine("Please select 1 for the sum of even numbers or select 2 for sum of odd numbers");
int usrinnum = int.Parse(usrin);
int sumeven = 0;
int sumodd = 0;
int[] Numbers = new int[6] { 25, 26, 27, 28, 29, 30 };

if (usrinnum == 1)
{
foreach (int i in Numbers)
{
if (i % 2 == 0)

sumeven += i;
Console.WriteLine("The sum of the Evens is " + sumeven);
}
}
else

{
foreach (int i in Numbers)
{
if (i % 2 == 1)

sumodd += i;

Console.WriteLine("The Sum of the odds is " + sumodd);
}
}``````
7
Contributors
10
Replies
62
Views
1 Year
Discussion Span
Last Post by rubberman
Featured Replies
• 2

> ... or use bitwise operator `if(i&1)` if true even else odd. I suppose if you want readers of your code to think you're clever, or have legitimately profiled the code for performance, run all practical test cases, and determined that this micro-optimization is warranted *and* not actively harmful. > …

The % simply returns the remainder of the division of the first and second number.

So if i = 25, then 25 % 2 would return 5, which doesn't meet either of your if statements.

``````        Console.WriteLine("");
Console.WriteLine("Problem #3 For loop - Add Up the Odds or Evens");
Console.WriteLine("");
Console.WriteLine("Please select 1 for the sum of even numbers or select 2 for sum of odd numbers");
int usrinnum = int.Parse(usrin);
int sumeven = 0;
int sumodd = 0;
int[] Numbers = new int[6] { 25, 26, 27, 28, 29, 30 };
if (usrinnum == 1)
{
foreach (int i in Numbers)
{
if (i % 2 == 0)
sumeven += i;
Console.WriteLine("The sum of the Evens is " + sumeven);
}
}
else
{
foreach (int i in Numbers)
{
if (i % 2 != 0)
sumodd += i;
Console.WriteLine("The Sum of the odds is " + sumodd);
}
}``````

Don't see much problems in your code. I just should remove the writeLines on lines 19 and 31 out of their the foreach loops.
Unless it is your intention to follow the progress of the calculation.
Pay a bit more attention to userinput. What would happen if the user typed '3' or 'abc' ?

... or use bitwise operator `if(i&1)` if true even else odd. Its faster because compare one bit only without mathematics

... or use bitwise operator `if(i&1)` if true even else odd.

I suppose if you want readers of your code to think you're clever, or have legitimately profiled the code for performance, run all practical test cases, and determined that this micro-optimization is warranted and not actively harmful.

Its faster because compare one bit only without mathematics

Citation needed. Compilers are rather smart, often smarter than the programmers who merely think they're smart. Optimizing `x % 2` into the suitably fast machine code is rather trivial. If you're applying this optimization with expectations of any noticeable positive impact, I think you'll be disappointed.

In fact, trying to outsmart the compiler can indeed produce slower machine code by bypassing superior optimizations. If it does improve performance for your test cases, there's a good chance the compiler explicitly chose not to optimize it because the results would be flat out wrong.

But let's not quibble over minutia. Instead, how about looking at a far more sinister problem with your suggestion: `x & 1` is not synonymous with `x % 2`. On top of limiting the supported type to integers, the result of the operation varies between the two when working with signed integers. Will it work? Probably...but are you sure? Note that the OP's code is performing this check on values contained in an array. These values will likely eventually be uncontrolled user input. Are you still sure it will work correctly? Can you prove it?

It's unfortunate that these embedded programming tricks have found their way into the general public because they're "nifty". In practice, they raise a lot of concerns that wouldn't exist if programmers followed the KISS approach until there's legitimate reason to deviate from it (like the embedded programmers who pioneered the tricks in the first place).

Good point.

I'm no expert at C++, so can someone else look at this and see if I'm missing something?

zachattack05 : 25 % 2 would return 5

zachattack05 : 25 % 2 would return 5

I'm so tempted to write a proof where this actually happens. :D

According to this site modulus should be the fastest method!
@JamesCherrill: no C++ here, it's C#, looks deceptively a lot like Java. :)

Sorry. C, C++, C#, C no evil... the're all the same to me. But is there ANY language in which 25%2 is 5?