Thread: Loop problem

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22

    Loop problem

    Hi, first of all i have to say Im a little embarrassed of making this question because Ive been reading a lot of posts out there, and they are really advanced topics. This is really simple.
    I'm sure that the solution has to be also very simple, but I've been thinking about it and i just cant find it.
    ok here it goes:

    I have to add the numbers between 1 and 10 using a loop. I know how to do the loop, what i dont know is how add the numbers between 1 and 10.

    I repeat that I know this has to be really stupid but i would appreciate some help.

    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <iostream>
    
    int main()
    {
        int num1, num2, num3;
    
        num2 = 5;
        num3 = 2;
        num1 = num2 + num3;
    
        std::cout << num3 << std::endl;
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    Thanks. but ive tried your code, and this isnt what im looking for. What i want to do is to add ALL the numbers between 1 and 10 using loop.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by robwhit View Post
    Code:
    #include <iostream>
    
    int main()
    {
        int num1, num2, num3;
    
        num2 = 5;
        num3 = 2;
        num1 = num2 + num3;
    
        std::cout << num3 << std::endl;
    
        return 0;
    }
    Just add num4, num5, etc.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    what code do you have so far? you said that you knew how to do the loop part.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    I dont see any loop there. Imagine a want to do it with a while loop. How would I do it?

  7. #7
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    ok this is what i got:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
    int x;
    
    x = 0;
    
    while ( x <= 10)
    {
        ?????
    }

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Tesnik View Post
    How would I do it?
    I'd do it by using a counter variable and then using another variable and add successively to the variable the value of the counter each time the loop repeats.

    the code should be indented and in between braces like this:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x;
    
        x = 0;
    
        while ( x <= 10)
        {
            ?????
        }
        return 0;
    }
    have you done for loops yet? they would usually be used in this situation, but you can use a while loop too.

    here's the general layout in case it isn't clear:
    Code:
        while ( x <= 10)
        {
            // add y + x to y
            // add 1 to x
        }
    http://www.cprogramming.com/tutorial/lesson3.html
    Last edited by robwhit; 08-22-2007 at 08:00 PM.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    How's this?
    Code:
    #include <iostream>
    using namespace std;
    
    int AddNums( int begin, int end )
    {
       if ( begin == end )
       {
          return begin;
       }
       else
       {
          return end + AddNums( begin, end - 1 );
       }
    }
    
    int main()
    {
       cout << AddNums( 1, 10 );
       return 0;
    }

  10. #10
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    ok umm, from your post ive tried this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       int x;
       int y;
       
       x = 1; 
       y = 1;/*counter*/
       
       while( x <= 10 )
       {
              cout<< x <<"\n";
              y++;
              x = x + y;
              
       }
       
       system("pause");
       return 0;
        
    }
    but... the output is this:

    1
    3
    6
    10

    and I dont want that

  11. #11
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    oops i wrote to slow

    cpjust THATS what i wanted!!

    thanks both of you

    Ive been studying C++ from this page (which is great) for about two weeks, and Ive done simple programs a little bit more complicated than this, thats why im so surprised that i couldnt find a solution for this, i was beggining to think i was stupid. Actually I havent found a solution, somebody else found it, so i think that makes me stupid

    thanks again
    Last edited by Tesnik; 08-22-2007 at 08:14 PM.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    the counter is the one that goes 1 2 3 4 5 6 7 8 9 10. When you write this line:

    x = x + y;

    then it doesn't do that. you want:

    y = x + y;

    and switch the lines

    y++;
    y = x + y;

    to

    y = x + y;
    y++;

  13. #13
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    ok thanks very much

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You can also try this:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int n = 0;
       int i = 10;
    
    loop:
       n += i;
    
       if ( --i > 0 )
    	   goto loop;
    
       cout << n << endl;
    
       return 0;
    }

  15. #15
    Registered User
    Join Date
    Aug 2007
    Location
    Madrid (Spain)
    Posts
    22
    wow haha i think thats a little more advanced. I understood the other code better

    I haven't seen the loop: thing.
    and the --i... dont know what it is

    but i will learn.

    thanks again

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