Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   C++ (https://www.askmehelpdesk.com/forumdisplay.php?f=439)
-   -   Code Segment Output for C++ (https://www.askmehelpdesk.com/showthread.php?t=408406)

  • Oct 21, 2009, 02:16 PM
    Bamaboy53
    Code Segment Output for C++
    To anyone who can help. I am in the beginning stages of C++ and I need some help with the following question. Don't just give me the answer if you can, please explained how to determine out. Thanks.

    What is the output of the following code segment?

    for(int m=1; m <= 4; m++)
    cout << (m*2);
  • Oct 21, 2009, 03:21 PM
    Clough
    Greetings and WELCOME to the site, Bamaboy53!

    I moved your question out of Introductions to this forum topic area so that it will be more likely to be noticed and addressed by those who are best able to answer it. Your question will get noticed much more here.

    Introductions is for people to introduce themselves only, and we try to not ask questions there. We would appreciate it if you would return to Introductions sometime to tell us a little bit about yourself, if you would be willing to do that.

    Thanks!
  • Oct 21, 2009, 03:34 PM
    Bamaboy53

    Thanks
  • Oct 22, 2009, 01:11 AM
    Clough
    Hi again, Bamaboy53!

    Eventually, someone should come along to address your question. If it doesn't get noticed, then I'll ask those who I think might be good with addressing it to come to your thread.

    Thanks!
  • Oct 22, 2009, 05:11 AM
    Bamaboy53

    Thanks, but I believe I have figure this one out.
  • Oct 22, 2009, 05:16 AM
    Bamaboy53
    Here's the answer I came up with:

    What is the output of the following code segment?

    for(int m=1; m <= 4; m++)
    cout << (m*2);

    In the above for loop m is initialize to 1
    1st time m = 1 and it is less than equal to 4 so cout prints at the screen (1 * 2) = 2
    2nd time m = 2 and it is less than equal to 4 so cout prints at the screen (2 * 2) = 4
    3rd time m = 3 and it is less than equal to 4 so cout prints at the screen (3 * 2) = 6
    4th time m = 4 and it is less than equal to 4 so cout prints at the screen (4 * 2) = 8
    5th time m = 5 and it is NOT less than equal to 4 so for loop exited

    Output screen will show all the above numbers together
    2468

    I hope that I got it right:)
  • Oct 22, 2009, 05:24 AM
    slapshot_oi
    Quote:

    Originally Posted by Bamaboy53 View Post
    Here's the answer I came up with:

    What is the output of the following code segment?

    for(int m=1; m <= 4; m++)
    cout << (m*2);

    In the above for loop m is initialize to 1
    1st time m = 1 and it is less than equal to 4 so cout prints at the screen (1 * 2) = 2
    2nd time m = 2 and it is less than equal to 4 so cout prints at the screen (2 * 2) = 4
    3rd time m = 3 and it is less than equal to 4 so cout prints at the screen (3 * 2) = 6
    4th time m = 4 and it is less than equal to 4 so cout prints at the screen (4 * 2) = 8
    5th time m = 5 and it is NOT less than equal to 4 so for loop exited

    Output screen will show all the above numbers together
    2468

    I hope that I got it right:)

    You sure did.

    On a side-note, in C++ they tried to make it standard to use the pre-increment operator and the != operator instead of <= operator because that logic is more readable to humans because we think that way.

    ++3 evaluates to 4, where as 3++ evaluates to 3. In the case of your for-loop, variable m isn't incremented until the line cout << (m*2); is executed.

    It's not essential, just style.

    [code="C++"]
    for(int m = 1; m != 5; ++ m)
    cout << (m*2);
    [/code]
    I'm programming in Objective-C now, and that is quite different from C++ in some respects. If you're interested in programming and this isn't just an assignment you want to get over with, I suggest you check it out.
  • Oct 22, 2009, 06:40 AM
    Bamaboy53

    Thanks for your feedback... it's most appreciated.
  • Oct 27, 2009, 03:29 PM
    tatetavia1963

    I am trying to
    Create a C++ console application that uses cout to display a box of asterisks. The output should look similar to:
    **********
    * *
    * *
    **********
  • Oct 27, 2009, 03:56 PM
    Clough
    Quote:

    Originally Posted by tatetavia1963 View Post
    I am trying to
    Create a C++ console application that uses cout to display a box of asterisks. The output should look similar to:
    **********
    * *
    * *
    **********

    Hi, tatetavia1963!

    You've posted your question on the thread of someone else. This place doesn't work the same way that a chat room does.

    If you want your question to be recognized as being your own, then it would be best for you to start a new thread with your question.

    Thanks!

  • All times are GMT -7. The time now is 05:14 PM.