Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Lorek

C++ Problem

This topic is 5553 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright, so its been about a year now since i''ve actually programmed in C++ and I pulled out my old school book with all the exercises and theres this one problem that''s just completely annoying me off the deep end. Its a nested loop problem. I need to make a program that outputs. * *** ***** ******* ***** *** * using one function. So after messing around with it so far this is what I have but it doesn''t work.
  
int display(int x)
{
for(int i=0; i<x; i++)
	if(i%2==1)
		cout << "*";
cout << endl;


if(x<7 && temp==0)
	display(x+2);
if(x>=7)
{
	temp=1;
	display(x-2);
}
if (x<=0)
	return 0;
return 0;
}
  
Well that was my two and a half hour attempt to make it work. It doesn''t though and my mind''s a little locked so i''m gonna take a break. Hopefully someone can help me.

Share this post


Link to post
Share on other sites
Advertisement
Well I figured it out. I guess I was thinking too hard on how to get out of using the nested loops that i wasn''t thinking clearly.

Recursion can be useful though.
The working code.

  
void display()
{
for (int i=1; i<=7; i++)

if(i%2==1)
{
for(int j=0; j<i; j++)
cout << "*";
cout << endl;
}


for(i=i-2;i>0; i--)

if(i%2==1)
{

for(int j=0; j<i; j++)
cout << "*";
cout << endl;
}
}

Share this post


Link to post
Share on other sites
This is a diamond.


  
#include <iostream.h>
#include <conio.h>

int main()
{
int x=5, y=3;

for(int i=-1; i<12; i++)
{
for(int j=-1; j<12; j++)
{
if(j>y && j<x)
{
cout<<"*";
}
else{
cout<<" ";
}
}
cout<<endl;
if(i<5)
{
x += 1;
y -= 1;
}
else{
x -=1;
y +=1;
}
}

getch();
return 0;
}


[edited by - Screams of Chaos on May 7, 2003 2:37:14 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!