Ask Experts Questions for FREE Help !
Ask
    Bamaboy53's Avatar
    Bamaboy53 Posts: 21, Reputation: -1
    New Member
     
    #1

    Oct 21, 2009, 02:16 PM
    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);
    Clough's Avatar
    Clough Posts: 26,677, Reputation: 1649
    Uber Member
     
    #2

    Oct 21, 2009, 03:21 PM
    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!
    Bamaboy53's Avatar
    Bamaboy53 Posts: 21, Reputation: -1
    New Member
     
    #3

    Oct 21, 2009, 03:34 PM

    Thanks
    Clough's Avatar
    Clough Posts: 26,677, Reputation: 1649
    Uber Member
     
    #4

    Oct 22, 2009, 01:11 AM
    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!
    Bamaboy53's Avatar
    Bamaboy53 Posts: 21, Reputation: -1
    New Member
     
    #5

    Oct 22, 2009, 05:11 AM

    Thanks, but I believe I have figure this one out.
    Bamaboy53's Avatar
    Bamaboy53 Posts: 21, Reputation: -1
    New Member
     
    #6

    Oct 22, 2009, 05:16 AM
    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:)
    slapshot_oi's Avatar
    slapshot_oi Posts: 1,537, Reputation: 589
    Ultra Member
     
    #7

    Oct 22, 2009, 05:24 AM
    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.
    Bamaboy53's Avatar
    Bamaboy53 Posts: 21, Reputation: -1
    New Member
     
    #8

    Oct 22, 2009, 06:40 AM

    Thanks for your feedback... it's most appreciated.
    tatetavia1963's Avatar
    tatetavia1963 Posts: 1, Reputation: 1
    New Member
     
    #9

    Oct 27, 2009, 03:29 PM

    I am trying to
    Create a C++ console application that uses cout to display a box of asterisks. The output should look similar to:
    **********
    * *
    * *
    **********
    Clough's Avatar
    Clough Posts: 26,677, Reputation: 1649
    Uber Member
     
    #10

    Oct 27, 2009, 03:56 PM
    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!

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

How do you take off a segment of pcv pipe that has been glued. [ 2 Answers ]

I have a leak in one of the joints on my pcv pipe that is been installed between two elbows.. How do I take off?

Perimeter of a segment [ 2 Answers ]

Find the perimeter of a segment whose angle is 45 and radius is 6 cm


View more questions Search