Thread: Need some help with a simple program

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    65

    Need some help with a simple program

    Well...I am not sure if this is the mistake the author makes before the book is published or I just misinterpret it. Anyway, here is the example of the very simple program:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
            int i, n;
    
    //  Get a number from the keyboard and initialize i.
    
            cout << "Enter a number and press Enter: ";
            cin >> n;
            i = 1;
            while (i <= 1){                    // While i less than or equal to n,
                  cout << i << " ";           //   Print i
                  i = i + 1;                        //   Add 1 to i.
            }
    
            return 0;
    }
    And then here comes the question for the exercise
    2.2.2 Alter the example so it prints all the numbers from n to 1 in reverse order. For example: 5 4 3 2 1. (Hint: to decrement a value inside the loop use the statement i = i - 1.

    I get stuck here. I was unable to print n to 1 with the use of i = i - 1. Maybe because of my low level math skills. This is my code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        // Declare two variables as n and i
        int n, i;
    
        // Assign 5 to i and 1 to n and do the calculation
        n = 1;
        i = 5;
        while(i >= n){
            cout << i << " ";
            i = i - 1;
        }
    
        return 0;
    }
    I can't have the cout print n to 1, only can I have cout print i. But if I wanna print n, I cannot use i = i - 1 expression. Is it a mistake of the exercise or I misinterpret it? I dunno how I can print i with the use of i = i - 1. It seems impossible to me.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Um, your code actually looks correct if you want to print "5 4 3 2 1 ".

    EDIT:
    Okay, I think I understand: you are confused because you want to start from n, but this appears to start from i. The trick is that n is just user input. You know that starting from i = 5 is correct, so now you just need to substitute it with n:
    Code:
    i = n;
    Since you know that you are printing from n to 1, use 1 in the loop condition:
    Code:
    while(i >= 1){
    Last edited by laserlight; 12-13-2008 at 11:11 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    That sounds so damn stupid an exercise...There is no point in doing that...I don't know what the author really want the reader or the user to do with the program...

    Anyway, that's the trick has really helped me...but this damn exercise has really put me in a fix...I have been bothered by this for a while and didn't really realize that to replace i = n and use 1 => i

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        // Declare two variables as n and i
        int n, i;
    
        // Assign 5 to i and 1 to n and do the calculation
        i = 5;
    
        while(i >= 1){
            n = i;
            cout << n << " ";
            i = i - 1;
        }
    
        return 0;
    }
    Is it what you meant?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, I mean something like your book's original example:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
        int i, n;
    
        //  Get a number from the keyboard and initialize n.
        cout << "Enter a number and press Enter: ";
        cin >> n;
    
        i = n;
        while (i >= 1) {      // While i greater than or equal to 1
            cout << i << " "; // Print i
            i = i - 1;        // Subtract 1 from i.
        }
    
        return 0;
    }
    That said, note that the comments are unnecessary in this case: I merely altered the original comments so as to explain what happens step by step, but any programmer past the novice stage should be able to understand that code without comments.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Got cha. Yeah I think I have misinterpreted the question...And now I understand.
    Last edited by kenryuakuma; 12-14-2008 at 01:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM