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;
}
C++ Problem
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.
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.
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.
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; }}
This is a diamond.
[edited by - Screams of Chaos on May 7, 2003 2:37:14 PM]
#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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement