Thread: Loop problem

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Tesnik View Post
    by the way, how long do i have to study to start making more complicated programs. I mean serious programs??
    you are already. programming is serious buisness.

    no, seriously, it can take a while. but it really depends on what you say is serious.

    You could make it really complicated with threading, networking, advanced data structures, graphics, shared libraries, STL, etc, or you could make a useful console program. but each additional facet of programming besides (well, including) standard C++ takes a while to learn to use effectively, but they all build upon C++, so you should have a solid understanding of the core language first. Each of these things can take months, or even years, to learn how to do well.

    Here's some stuff you might find useful:

    << !! Posting Code? Read this First !! >>
    Cprogramming.com: How to Get Started with C++
    Cprogramming.com - Programming Tutorials: C++ Made Easy and C Made Easy
    Cprogramming.com: FAQ
    C Book Recommendations
    C++ Book Recommendations - C Board
    cplusplus.com - The C++ Resources Network

  2. #17
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    thanks a lot. You know, this is my first really serious attempt to study a programming language.
    Ive seen some Pascal, some Python and also some Delphi, but i got tired of it. But C, i dont know... it has something that keeps me studying it
    Its good to know that it may take months or even years, because ive always thought i was going too slow haha, that i wasn&#180;t advancing.

    By the way I deleted that question because I considered it too newbieish so thanks for taking the time

    you will be seeing me alot around here, because im having trouble with pointers, and classes
    Last edited by Tesnik; 08-22-2007 at 08:46 PM.

  3. #18
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    or there's the classic:
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <functional>
    
    using namespace std;
    
    int num = 0;
    
    void Add( int i )
    {
       num += i;
    }
    
    int main()
    {
       vector<int> vec;
    
       for ( int i = 1; i <= 10; ++i )
       {
          vec.push_back( i );
       }
    
       for_each( vec.begin(), vec.end(), Add );
    
       cout << num << endl;
    
       return 0;
    }
    or good old:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       int n = 0;
       for ( int i = 1; i <= 10; n += i++ );
    
       cout << n << endl;
    
       return 0;
    }

  4. #19
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    haha thanks cpjust i appreciate it

    robwhit i have answered you on top of cpjust, we must have press the post button at the same time

    cpjust what does n+= mean?

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    don't worry about being a newbie, there are a lot of beginners around here, you'll blend right in.
    Quote Originally Posted by Tesnik View Post
    you will be seeing me alot around here, because im having trouble with pointers, and classes
    check out the tutorials on this site.
    cpjust what does n+= mean?
    n += 5; is the same thing as n = n + 5;
    Last edited by robwhit; 08-22-2007 at 09:07 PM.

  6. #21
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    I sure will

  7. #22
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    so... n+= i++ means.... n=n+(i+1)???

    i dont get this part of code:

    Code:
     for ( int i = 1; i <= 10; n += i++ );
    
     cout << n << endl;
    why doesn't the cout go inside {} in the for loop?
    Last edited by Tesnik; 08-22-2007 at 10:00 PM.

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you realize that all of the answers that cpjust has given are much more complicated than you need? If you're trying to learn, you should keep looking for the right answer yourself.

    If you know a for loop, try and use it. Otherwise, keep working on your while loop.

  9. #24
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    umm yeah, but its good to know what n += i++ means, or why doesn't the cout go inside the for loop.

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The cout is outside the for loop, meaning it is run only once. The cout outputs the end result, so you want it to run only once. If you put it inside the for loop, it would run each time through the loop, and you would get 1, 3, 6, 10, ...

    n += i++ has two parts. The first part is the i++. i++ means add 1 to i, but it also returns a value. It reutrns the value of i before you add one. So lets say i is 0, then i++ returns 0 and changes the value of i to 1.

    The second part is the n += part. That means that whatever i++ returns is added to n. Since we just learned that i++ returns the value of i before 1 is added, then n += i++ adds the value of i to n, then adds 1 to i.

    In the normal for loop, those two parts would be separated for clarity and readability.

  11. #26
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    very clear, thanks.

  12. #27
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    lol @ cpjust being a showoff.

  13. #28
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    lol @ cpjust being a showoff.
    Yeah, I saw everybody giving Tesnik half of the answer at a time, so I figured I might as well try to completely confuse him and give him every answer except the simple one. Sometimes I get evil when I'm bored.

  14. #29
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    and i thought you were being a good, kind person

  15. #30
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Without any loop at all:
    Code:
    int sum(int min, int max)
    {
    	int sum = (min+max)*((max-min+1)/2);
    
    	if( !((min+max)&#37;2) ) sum += (min+max)/2;
    
    	return sum;
    }
    ... just for grins.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. Problem with for loop calling external function
    By lordbubonicus in forum C Programming
    Replies: 2
    Last Post: 10-13-2007, 10:54 AM
  4. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM