Computing.Net > Forums > Programming > Display numbers with C++ for loops

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Display numbers with C++ for loops

Reply to Message Icon

Name: Bertis Emerson
Date: August 8, 2003 at 05:36:36 Pacific
OS: XP Pro
CPU/Ram: AMD Athlon/512
Comment:

I'm having a problem of understanding the logic of getting the following code to work the way I want it to in the beginning comment block:

/*
This project is to display
a set of numbers in the
following manner:

123456789
12345678
1234567
123456
12345
1234
123
12
1

*/

#include

int main()
{
// declare variables
int count;
int num;

/*
make two for loops;
the outer loop is
to count and display
the rows for the inner
loop calculations
*/

for (count = 1; count = 9; count++) // start of outer loop
{
cout endl;

for (num = 1; num = 9; num++) // start of inner loop

cout num ""; // end of inner loop

} // end of outer counter loop

cout endl endl;

return 0;

} // end of main



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: August 8, 2003 at 09:52:44 Pacific
Reply:

I'm guessing that you put signs where appropriate and the site stripped them. It does that. So, assuming that you used = in the test of the for loops and had the appropriate symbols in the 'cout' statments:


One problem is that you need the test in the inner loop to change according to the value of 'count' from the outer loop.

for (num = 1; num = count; ++num)


I think that the second problem will become obvious when you implement the above change.


Post back if you have more questions. Also, please let us now when you get it working.


0

Response Number 2
Name: Don Arnett
Date: August 8, 2003 at 09:55:30 Pacific
Reply:

*&$#*&$@#$

I'm guessing that you put < and > signs where appropriate and the site stripped them. It does that. So, assuming that you used <= in the test of the for loops and had the appropriate symbols in the 'cout' statments:

One problem is that you need the test in the inner loop to change according to the value of 'count' from the outer loop.

for (num = 1; num <= count; ++num)


I think that the second problem will become obvious when you implement the above change.


Post back if you have more questions. Also, please let us now when you get it working.


0

Response Number 3
Name: Don Arnett
Date: August 8, 2003 at 10:00:59 Pacific
Reply:

To get the lt and gt signs to display, I do the following:

- when typing the original version of my post, I type 'lt' or 'gt' where I want a symbol.

- when you press the Submit button, you are taken to a confirmation page. On this page, I put a & in front of the 'lt' and 'gt'.

- my problem above was that I told the confirmation page to redisplay so that I could see the symbols. But when I pressed Submit, they were stripped. So remember, if you see the symbol on the confirmation page, you won't see it in the posted version. You should see the code "& lt" or "& gt" in the conf page.



0

Response Number 4
Name: cheezeWhiz
Date: August 8, 2003 at 14:52:35 Pacific
Reply:


hi~ it looks like using 9 will send you out
of the array bounds? i'm thinking 8 is a
better call.

right?




0

Response Number 5
Name: Don Arnett
Date: August 8, 2003 at 15:41:47 Pacific
Reply:

cheese - you need to re-read. There was no mention of an array anywhere.


0

Related Posts

See More



Response Number 6
Name: Rolos
Date: August 8, 2003 at 15:46:56 Pacific
Reply:

Hi. There is no need for an array variable in this case. However, if it is required in your assignment you should implement one. But I did not see any sort of specifications for it in your instructions. I can help you a little with your conditions and so forth.

I will give you a little hint with the counter variables in your for loops. Your 'count' variable should be initialized to 9 and decremented. Your 'num' variable should be initialized to 1 and incremented. Also be sure to make adjustments to your condition statements which compares 'count' and 'num' so you dont run into an infinite loop or worse.

Also you should make sure that adjustments are made to BOTH of your for loops. This way, it will produce the output the way you desire in the example provided in your code. I have already wrote the code and ran it, so if you are in extremely dire need then post back a comment. I will be glad to give you little snippets, but not the entire assignment.

I swore an oath not to do assignments for passer-byers in this forum. But I and the rest of the regulars here can provide sufficient help to get you on your way. I hope this helps you.

- Rolos



0

Response Number 7
Name: cheezeWhiz
Date: August 8, 2003 at 17:51:10 Pacific
Reply:

ahh. i see. i don't know where i got that
from? i worked it out with an array, and
now, i've worked it without, the for loops
do all of the work without the array, so i
guess that makes more sense. lol.


0

Response Number 8
Name: Rolos
Date: August 8, 2003 at 22:32:43 Pacific
Reply:

Haha you're right. I mean you could do it with an array, but I mean like you had said, it does make more sense to not use an array. It would be good practice if the objective is to implement arrays. But since you don't have to store the data, why use it right?

Anyhow, good luck with your assignment. If you need more help then just post back here.

- Rolos


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Display numbers with C++ for loops

java for loops www.computing.net/answers/programming/java-for-loops/12866.html

FOR loop syntax www.computing.net/answers/programming/for-loop-syntax/19286.html

Need help with C++ program www.computing.net/answers/programming/need-help-with-c-program/19441.html