English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

the code for drawing diamond is:
int i,j,k,x;
clrscr();
cout<<"Enter Odd Number(1-19):";
cin>>x;
for(i=1;i<=x/2+1;i++)
{
cout< for(j=0;j<=x/2-i;j++)
cout<<" ";
for(k=0;k<=2*(i-1);k++)
cout<<"*";
}
for(i=x/2;i>0;i--)
{
cout< for(j=0;j<=x/2-i;j++)
cout<<" ";
for(k=0;k<=2*(i-1);k++)
cout<<"*";
} how can I change it inorder to have hallow diamond like this:
*
* *
* *
* *
*

2006-09-12 22:06:48 · 1 answers · asked by simona 1 in Computers & Internet Programming & Design

*
* *
* *
* *
*

2006-09-12 22:08:13 · update #1

I can't draw the diamond here

2006-09-12 22:09:21 · update #2

1 answers

Could this be how you want the diamond. I have included '_'s as spaces. I will assume the input is 5 to get this diamond.

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

I have not tested it, but here are some changes. First you will need 2 new functions / methods:

void newFunction01(int i, int x);
void newFunction02(int i, int y);

The two parameters for newFunction01 being int i, the counter, and int x, the input from your original function.
The two parameters for newFunction02 being int i, the counter, and int y, equal to x / 2

// The start of your code, placed in brackets to you can see the end of the function.
{
__int i,j,k,x;
__clrscr();
__cout<<"Enter Odd Number(1-19):";
__cin>>x;
// changes start here
__i = 0;
__newFunction01(i, x);
}

void newFunction01(int i, int x)
{
__int y = x / 2;
__if(i > y)
____return;
__newFunction02(i, y);
__newFunction01(i + 1, x);
__newFunction02(i, y);
}

void newFunction02(int i, int y)
{
__for(int j = i; j < y; j++)
____cout << ' ';
__cout << '*';
__if(i > 0)
__{
____int j = i * 2 - 1;
____for(int k=0; k ______cout<<' ';
____cout << '*';
__}
}

2006-09-14 10:31:06 · answer #1 · answered by Mark aka jack573 7 · 0 0

fedest.com, questions and answers