Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested for loops in Borland C++ 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I'm trying to produce this pattern with uaing just nested for loops,

*****
****
***
**
*


Please help!!! I've been trying for like a week now, if you can help me with the code it would be GREAT & MUCH APPRECIATED!!!
 
for(int i=0;i<5;i++)
{
for(int j=0;j<(5-i);j++)
{
for(int c=0;c<i;c++)cout<<&quot; &quot;;
cout<<&quot;*&quot;
};
cout<<endl;
};

I havn't tested it,but I think It is ok :) Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
The above works but isn't what I'm looking for. The above displays...


_*_*_*_*
_*_*_*_*_*_*
_*_*_*_*_*_*
_*_*_*_*


The &quot;_&quot; are spaces.

The pattern I'm trying to get is...

*****
_****
__***
___**
____*

Sorry bout' that, please help me if you can. I've try everything I know. Waiting for your reply :)
 
and if you move the cout<<&quot; &quot; out of the loop ?
just before the second loop.
(cause thats a fault)
try that,I'll make the example. :)


for(int i=0;i<5;i++)
{
for(int c=0;c<i;c++)cout<<&quot; &quot;;
//or just leave this line away

for(int j=0;j<(5-i);j++)
{
cout<<&quot;*&quot;
};
cout<<endl;
};

just a second :)
Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Yeps,moving it out of the loop works for me

so:

#include <iostream.h>

void main()
{
for(int i=0;i<5;i++)
{
for(int c=0;c<i;c++)cout<<&quot; &quot;;//draw the spaces
for(int j=0;j<(5-i);j++)cout<<&quot;*&quot;;//draw the *
cout<<endl;//goto next line

}
} Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Just a thought... i know you have the answer.

Code:
char strs[5][5+1];
memset(strs,'*',sizeof(char)*5*6);


for(int i = 1;i<5;i++)
{
   memcpy(strs[i],' ',i);
}

for(int j = 0;j<5;j++)
{
   strs[j][5]=0;
   cout<<strs[j]<<endl;
}[code]

Just curious if it works.  Just thought it up and thought it was a neat approach.  The strs[j][5] puts a null terminator at the so we can avoid strange outputs.  

Just go's to show, there is more then one way to skin a cat :-P

Matt
 
That works. I can't believe it works. Oh my... (well you know) It works.

Thanks heaps for that! EVen my teacher had to think about it! That's great.

Thanks heaps muppeteer!!! :)
 
Well,teachers most of the time arn't programmers,
When I was 15 years old,and I had my first lessons of programming in Turbo Pascal (I studied chemistry back in the days...), I went to the library for a book about it and
I came to the word 'pointer' wich I did not understand. So I went to my teacher and asked her &quot;what is a pointer?&quot; she said:&quot;I have already heard about it,but I don't know exactely what it is...,but don't mess with that,it is probably to difficult&quot;. :)

Anyway,Zyrenthian I like your solution a lot. I did not know the command memset and memcpy but I understand it and I like it.Keep skinning cats X-)
Greetz,

The Muppeteer.

themuppeteer@hotmail.com

Don't eat yellow snow...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top