Find the answer to your Linux question:
Results 1 to 4 of 4
I seem to always post mundane problems in comparison to the more exotic C++ programs on this forum. Not being one to disappoint, here's another problem. I wrote a program ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    38

    generating the triangle numbers


    I seem to always post mundane problems in comparison to the more exotic C++ programs on this forum. Not being one to disappoint, here's another problem.

    I wrote a program that finds all the triangle numbers less than 100:

    #include <iostream>
    using namespace std;

    int main() {

    int a;

    for(a = 1; a * ( a + 1) / 2 <= 100; a++){
    cout << a * ( a + 1) / 2 << "\n";
    }

    return 0;

    }

    However, I would much rather avoid writing a * ( a + 1) / 2 <= 100 each time. I decided to define b = a * ( a + 1) / 2 <= 100.

    #include <iostream>
    using namespace std;

    int main() {

    int a = 1 , b;

    for(a = 1; b <= 100; a++){
    b = a * ( a + 1) / 2;

    cout << b << "\n";
    }

    return 0;

    }

    Unfortunately, this doesn't work. All i've done is define b and replace it in the program that I know works.

    Why is this second program not working, but the first one works perfectly?

  2. #2
    Linux User
    Join Date
    Dec 2009
    Posts
    260
    Hi,
    the basic answer for your question is:
    because the value of b ain't defined in the first run of the for loop.
    anyway if you wanna check a different value than "a" in a loop you should use a while loop.

    i don't know the syntax of c ... so i may suggesting some trash code here:

    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
     int a = 1 , b = 1;
     while (b <= 100) {
       b = a * ( a + 1) / 2;
       cout << b << "\n";
       a++;
     } 
     return 0;
    }

  3. #3
    Just Joined!
    Join Date
    Aug 2011
    Posts
    38
    Ah, I haven't learnt about while loops yet.

    I also see what you mean about not defining b for my first loop. However, I can't take b out of the loop either since if I set a =1, b=1 and would forever stay 1 (since it's inside the loop). This would result in an infinite number of 1's!!

    I don't think what you've done is "trash code", is there any way you can do it with a for loop instead? (if the answer is no, I might skip ahead a chapter to the while loop section).

  4. #4
    Linux User
    Join Date
    Dec 2009
    Posts
    260
    Code:
    #include <iostream>
    using namespace std;
    int main() {                 //definition of the function main
     int a = 1 , b;              //declare some vars memory becomes reserved and assigned to your program
     for(a = 1; b <= 100; a++){ //here starts the loop
      b = a * ( a + 1) / 2    //b becomes set
      cout << b << "\n";
     }                             //here ends the loop
     return 0;                   //return from function with code 0
    }
    //end of func. main
    Hi again,
    some information for you to understand where the loop starts.

    The loop starts in the line where you write for.
    Everything before just runs one time.
    So it's no problem to write:
    int a=1, b=1;
    It it happens, that the for loop ain't ending setting b=1, you have a problem with the nature of the for loop.

    In my personal opinion the for loop is the less usable loop in c that exists.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •