It looks like your logic is reversed a bit. You want to sum both the odds and evens, but in your current logic, it's either one or the other depending on the initial number.
Since the range of odd numbers is wider than even numbers, you can include the test and sum of even numbers in your loop for the odd numbers. Just do the sum for even numbers conditionally:
for ( int i = 0; i < 3579; i++ ) {
if ( i % 2 != 0 )
totalodd += i;
if ( i >= 522 && i < 2222 && i % 2 == 0 )
totaleven += i;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>I am new to C++
Then I'll encourage you not to use that horrid style of formatting. Ideally you should put braces on a separate line. Look for code posted by members here with high post counts. Chances are good that they use a highly readable style.
>but then I tried making some changes to it
In other words, you took the example I gave you (which worked perfectly according to your requirements) and butchered it.
>but now cant get back to where I was
This is where versioning helps. Unfortunately, I can't help you get back to where you were because I don't know where you were unless where you were was the code I posted, in which case you can just cut and paste it again.
>disregard last code this is the one I have so far
Grrr. You're right back where you started. The if statement guarantees that you calculate evens, or odds, but never ever ever both. I explained this to you in post #2.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>I seem to be upsetting you
I doubt you could manage to upset me.
>I wanted to ask you about the "braces on a separate line part, I'm not sure what you mean by that.
I mean that anywhere you use { or }, they should be alone on the line and everything between them should be indented. Like this:
void function()
{
// statement
if ( something )
{
// statement
// statement
// statement
}
// statement
while ( something )
{
// statement
}
// statement
}
This guarantees that it's easy to see where in the nesting each statement is, and you save yourself a lot of trouble trying to match braces because a missing brace is painfully obvious.
>I'm just trying to understand this stuff that seems to be extremely hard for me.
Okay, let's keep it as simple as possible. Treat the odd and even calculations as completely separate. If nothing is related, the relationships won't confuse you. Then it boils down to two separate loops:
int oddsum = 0;
for ( int i = 1; i < 3579; <em>next number</em> ) {
// add the odd numbers to oddsum
}
int evensum = 0;
for ( int i = 522; i < 2222; <em>next number</em> ) {
// add the even numbers to evensum
}
Now, we can do this two ways. We can look at every number and pick out the evens or the odds, but that's slightly more complicated because you have to determine if the number is even or odd. An easier way is to just add 2 in the part that saysnext number. That way you skip over the numbers you don't want:
int oddsum = 0;
for ( int i = 1; i < 3579; i += 2 ) {
// add the odd numbers to oddsum
}
int evensum = 0;
for ( int i = 522; i < 2222; i += 2 ) {
// add the even numbers to evensum
}
Adding to the sum is easy. Just do sum = sum + i , or the shorter form of sum += i .
That's all there is to it. Look through the range by adding 2, and add the current number in the range to the sum. Do this twice: once for odd numbers and once for even numbers.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I guess that his professor was real adamant in him using while loops for the program instead of the good 'ol for loops...
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26
i was just wondering (apart from the program itself) if there would be anyway to verify if what the program did is correct mathematically?
zandiago
Nearly a Posting Maven
2,480 posts since Jun 2007
Reputation Points: 129
Solved Threads: 26