Thread: For Loops

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    Exclamation 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.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    	int sum = 0;
    	for ( int i=1; i<5+1; i++ ) 
    	{
    		sum += (i%2==0) ? -i : i; // Or something
    	}
    	std::cout<< sum;

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    Thumbs up

    cool, thanks. so what does the question mark do? what function does it serve i mean

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    The ? and the : work like a "if this do that, if not do this" in one line, very nice and handy.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    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.

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    In general,
    Code:
    a = b ? c : d;
    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;
    }
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    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?

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> 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]

  9. #9
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I was wondering, is there a for each construct in C++?

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Yes. It's in <algorithm>

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.

Popular pages Recent additions subscribe to a feed