-
For Loops
Hi i have a question if anybody can help me. I need to take a positive input, n, and and produce an output that is the sum of the alternating series.
i.e. n=5 the nthe output would be 3 because 1 - 2 + 3 - 4 + 5 = 3
i need to do this using a for loop and it has been boggling my mind.
Any suggestions on how i can get this done? Just the for loop i dont understand, like, how to turn the even numbers negative.
-
Code:
int sum = 0;
for ( int i=1; i<5+1; i++ )
{
sum += (i%2==0) ? -i : i; // Or something
}
std::cout<< sum;
-
cool, thanks. so what does the question mark do? what function does it serve i mean
-
The ? and the : work like a "if this do that, if not do this" in one line, very nice and handy.
-
if (something) {do this}
else {do this instead }
is equivalent to
(something) ? do this : do this instead;
Change it if you don't like it.
-
In general, is equivalent to
Code:
if (b) { a = c; } else { a = d; }
It's a convenient way of writing if-then-else expressions where you're just using the result in an expression. So, for example, the line in the for loop there could be written,
Code:
if (i % 2 == 0) {
sum += -i;
}
else {
sum += i;
}
-
Thanks a lot. My prof was getting on with me having to add in another variable, like j for instance. and that messed me up. I guess i still would for the output though.
use like, three variables? n, i, and j?
-
>> use like, three variables? n, i, and j?
Those variable names don't tell you anything about what information they hold! It's very useful (for debugging purposes), if your variable names describe, concisely, what their function is, which is why I called my sum, 'sum'. You could input a variable called 'UpperBound' or something, which defined the upper bound of the for loop, then do something like -
for ( int i=0; i<UpperBound; i++ ) [Rest as before]
-
I was wondering, is there a for each construct in C++?
-
-
Well, 1-2+3-4+... = (1-2) + (3-4) + ... with n added at the end if and only if n is odd. So you multiply -1 by the truncated value of half of n (the number of terms in parentheses), and then if n is odd, add n.
Code:
int sum = -(n >> 1);
if (n&1) sum += n;
Taking the bitwise & with 1 is the most efficient way to check whether a number is odd - it just reads off the lowest order bit. Similarly, the right shift >> by 1 is the most efficient way to divide by two - it just moves all the bits over by one and throws away the lowest-order one.
-
These operations only work as expected on unsigned variables. CPU power is cheap nowadays, unless you happen to work on a smart card reader, in which case this program shouldn't be inside one. You want program efficiency?
Code:
shr eax, 1
neg eax
...
But in other cases, try going more for programmer efficiency, which comes at a slightly higher premium.
-
You're right, I forgot about that. But only n has to be unsigned, and it should be anyway, since the definition of the sum only makes sense if n>0. The only problem might be if one enters n at the keyboard and wants to check that it's positive, in which case there wouldn't be any way to protect against an unintended cast. And that could be avoided by entered a signed int sn, checking it, and then assigning to an unsigned n (or even better, entering an arbitrary string and then doing all the checks before assigning to n). Anyway, there's no such thing as enough CPU, if (unlike this toy example) the operation is used enough times.