C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: Nested For Loop Query

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Francis Glassborow
Guest





PostPosted: Sun Aug 17, 2003 4:38 pm    Post subject: Re: Nested For Loop Query Reply with quote



In article <417ujv4sk9nsnnpaka9r2b0r1tcl5lrs0s (AT) 4ax (DOT) com>,
[email]Anonymous (AT) mulgara (DOT) westnet.com.au[/email] writes
Quote:
#include #include
using namespace std;

void main(void)

check the required signature for main(); it is defined to return an int.

Quote:
{
const int rows(10);
const char caret('^');
const int offset(2);

for(int row = 0; row < rows; ++row)
{
cout << setw(rows * offset - row * offset - 1);

Instead of messing with manipulators, just out put the required number
of spaces. You are vulnerable to a change from pre-standard C++ if you
use setw and then output a char.

Quote:

for(int carets = 1; carets < row * offset + offset; ++carets)
cout << caret;

cout << 'n';
}


}


What do you suggest?

--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv
Guest





PostPosted: Sun Aug 17, 2003 4:41 pm    Post subject: Re: Nested For Loop Query Reply with quote



On Sun, 17 Aug 2003 07:44:08 -0400, Anonymou wrote:

Quote:
{This looks like homework but the code includes elements of more general
interest. Please address those but be careful not to do his homework:-)
-mod}

I am trying to display the following pyramid using nested for loops:
[snip]......


Quote:

What do you suggest?

I would suggest using recursion for such type of problems instead of the
nested for loop approach, which means that you have to do all the
calculation yourself. Using recursion, you can make the compiler do all
the dirty work, and code for only an atomic case.

Regards,
-Dhruv.







[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Roger Willcocks
Guest





PostPosted: Mon Aug 18, 2003 10:52 am    Post subject: Re: Nested For Loop Query Reply with quote



<Anonymous (AT) mulgara (DOT) westnet.com.au> wrote

Quote:
{This looks like homework but the code includes elements of more general
interest. Please address those but be careful not to do his homework:-)
-mod}

I am trying to display the following pyramid using nested for loops:
....
What do you suggest?


I would note that each row contains the same number of characters; the first
row is

9 0 [1] 0 9 (spaces carets [caret] carets spaces)

the second: 8 1 [1] 1 8
the third: 7 2 [1] 2 7
etc.

until everything is carats.

--
Roger



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Michiel Salters
Guest





PostPosted: Mon Aug 18, 2003 4:40 pm    Post subject: Re: Nested For Loop Query Reply with quote

[email]Anonymous (AT) mulgara (DOT) westnet.com.au[/email] wrote in message news:<417ujv4sk9nsnnpaka9r2b0r1tcl5lrs0s (AT) 4ax (DOT) com>...
Quote:
{This looks like homework but the code includes elements of more general
interest. Please address those but be careful not to do his homework:-)
-mod}

I am trying to display the following pyramid using nested for loops:

^
^^^
^^^^^
^^^^^^^
^^^^^^^^^
^^^^^^^^^^^

What do you suggest?

I would use a single loop, and within the loop I'd use the std::string
constructor that takes a character count and a fill character to create
a string of spaces and another string of carets.

HTH,
--
Michiel Salters

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Mon Aug 18, 2003 5:18 pm    Post subject: Re: Nested For Loop Query Reply with quote

In article <pan.2003.08.17.14.37.45.709236 (AT) gmx (DOT) net>, Dhruv
<dhruvbird (AT) gmx (DOT) net> writes
Quote:
On Sun, 17 Aug 2003 07:44:08 -0400, Anonymou wrote:

{This looks like homework but the code includes elements of more general
interest. Please address those but be careful not to do his homework:-)
-mod}

I am trying to display the following pyramid using nested for loops:
[snip]......


What do you suggest?

I would suggest using recursion for such type of problems instead of the
nested for loop approach, which means that you have to do all the
calculation yourself. Using recursion, you can make the compiler do all
the dirty work, and code for only an atomic case.

I would definitely NOT encourage the use of recursion for such tasks.
Good compilers can often convert a tail recursive function to iteration
but that is not an excuse for a programmer using recursion.



--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Mon Aug 18, 2003 5:19 pm    Post subject: Re: Nested For Loop Query Reply with quote

Dhruv <dhruvbird (AT) gmx (DOT) net> wrote:

Quote:
On Sun, 17 Aug 2003 07:44:08 -0400, Anonymou wrote:

{This looks like homework but the code includes elements of more general
interest. Please address those but be careful not to do his homework:-)
-mod}

I am trying to display the following pyramid using nested for loops:
[snip]......


What do you suggest?

I would suggest using recursion for such type of problems instead of the
nested for loop approach, which means that you have to do all the
calculation yourself. Using recursion, you can make the compiler do all
the dirty work, and code for only an atomic case.

Regards,
-Dhruv.

Recursion? it is easy to do iteratively creating a side in a '3d' look

of a pyramid. Simple analysis will determine how many of each character
to write on each line so you can get a '3d' pyramid, provided the # of
sides meets a simple criterion [ 10 satisfies this as does 13 and 28 and
may others.] Just think it through and you can get results with two
outer loops and three inner loops in each outer loop, all loops use
++,-- arithmetic.




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Mon Aug 18, 2003 5:19 pm    Post subject: Re: Nested For Loop Query Reply with quote

<Anonymous (AT) mulgara (DOT) westnet.com.au> wrote:

Quote:
{This looks like homework but the code includes elements of more general
interest. Please address those but be careful not to do his homework:-)
-mod}

I am trying to display the following pyramid using nested for loops:

[snip]


What do you suggest?

is this like what you want?

^
^^^*
^^^^^**
^^^^^^^***
^^^^^^^^^****
^^^^^^^^^^^*****
^^^^^^^^^^^^^******
^^^^^^^^^^^^^^^****
^^^^^^^^^^^^^^^^^**
^^^^^^^^^^^^^^^^^^^

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Tue Aug 19, 2003 12:16 am    Post subject: Re: Nested For Loop Query Reply with quote

On 17 Aug 2003 07:44:08 -0400, [email]Anonymous (AT) mulgara (DOT) westnet.com.au[/email] wrote:

Quote:
{This looks like homework but the code includes elements of more general
interest. Please address those but be careful not to do his homework:-)
-mod}

Sorry, I see nothing but a simple math error.

Quote:
I am trying to display the following pyramid using nested for loops:

^
^^^
^^^^^
^^^^^^^
^^^^^^^^^
^^^^^^^^^^^
^^^^^^^^^^^^^
^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^

Use a fixed pitch when creating these pictures. The above desired
output looks almost identical to your actual output on my screen.
It is a triangle with the right symbol of the last line one space
left of the others.

Quote:
So far, I have the following code, but cannot seem to create the
pyramid - only a triangle shape occurs:

#include #include
using namespace std;

void main(void)

Say int main () when posting to C++ groups to avoid unhelpful
replies. Main is required to return int and this code will
fail to compile on a very strict compiler. It obviously has
nothing to do with your problem.

Quote:
{
const int rows(10);
const char caret('^');
const int offset(2);

I appreciate the desire for named constants; however, I can't follow
the meaning of offset in this case. Sometimes the literal is more
meaningful than any name you could give it. It does mean 2 and is
used as a multiplier.

Quote:
for(int row = 0; row < rows; ++row)
{
cout << setw(rows * offset - row * offset - 1);

I know that your assignment requires use of nested for loops and setw.

Your code is structured as required. The math error is in the setw.
The number of carets is 2r+1, but the number of leading spaces is
10-r. The doubling only applies to the carets.

Quote:
for(int carets = 1; carets < row * offset + offset; ++carets)
cout << caret;

cout << 'n';
}

}

What do you suggest?

Look at what is happening. You are getting twice as many spaces as you
want. There is a times two in the setw. Remove that and see what
happens. Only off by one now. Remove the minus one and get what you
want. Much faster than posting to this group. Now go back to the
problem and discover what error in thinking produced the original code.
The last part is very important. Not because it will prevent you from
making it again, but because you will recognize it when it happens. The
major difference between experienced programmers and new ones is that
the former have made most of the errors already. The first time is
painful for everyone.

As a general suggestion for homework problems, state up front that it
is a homework problem and the requirements such as nested loops using
setw in the outer as in this example. That will stop replies from
stating that you have a dumb solution, but will not stop them from
stating that you have a dumb instructor. Instructional goals often
have nothing to do with the professional goals of most people here in
this group. Given that you do that and post your attempt which does
not work, it is likely to be rejected as trivial here. A better place
would be alt.comp.lang.learn.c-c++. You may still get a bunch of
responses that are of little or no help, but at least they will get
to you quicker as will the good ones.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
AngleWyrm
Guest





PostPosted: Tue Aug 19, 2003 12:32 am    Post subject: Re: Nested For Loop Query Reply with quote

<Anonymous (AT) mulgara (DOT) westnet.com.au> wrote

Quote:
{This looks like homework but the code includes elements of more general
interest. Please address those but be careful not to do his homework:-)
-mod}

I am trying to display the following pyramid using nested for loops:

To debug this code, add a cout statement like so:

for(int row = 0; row < rows; ++row)
{
cout << setw(rows * offset - row * offset - 1);
cout << rows * offset - row*offset - 1; // THIS SHOWS THE PROBLEM

for(int carets = 1; carets < row * offset + offset; ++carets)
cout << caret;

The formula you are using for your setw() function is producing values 19,17,15,...,1 for your
leading white space. Try rewriting your formula to something that will produce values starting from
the middle-1 and going down, instead of the end.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Tue Aug 19, 2003 12:32 am    Post subject: Re: Nested For Loop Query Reply with quote

On 18 Aug 2003 13:19:17 -0400, [email]cbarron3 (AT) ix (DOT) netcom.com[/email] (Carl Barron)
wrote:

Quote:
Recursion? it is easy to do iteratively creating a side in a '3d' look
of a pyramid. Simple analysis will determine how many of each character
to write on each line so you can get a '3d' pyramid, provided the # of
sides meets a simple criterion [ 10 satisfies this as does 13 and 28 and
may others.] Just think it through and you can get results with two
outer loops and three inner loops in each outer loop, all loops use
++,-- arithmetic.

What does that have to do with anything? The poor student needs to
print a picture of an isosceles triangle using two nested for loops
and setw, but his program prints a right triangle. Fixed pitch

Desired:
^
^^^
^^^^^
^^^^^^^

Program:
^
^^^
^^^^^
^^^^^^^

Asking simple questions on this group may be impossible useless.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.