PDA

View Full Version : Code Segment Output for C++


Bamaboy53
Oct 21, 2009, 02:16 PM
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
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
Oct 21, 2009, 03:34 PM
Thanks

Clough
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
Oct 22, 2009, 05:11 AM
Thanks, but I believe I have figure this one out.

Bamaboy53
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
Oct 22, 2009, 05:24 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:)
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.



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

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
Oct 22, 2009, 06:40 AM
Thanks for your feedback... it's most appreciated.

tatetavia1963
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
Oct 27, 2009, 03:56 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:
**********
* *
* *
**********

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!