C++ Diamond pattern output

Hey you all. Does any of you know how to solve the code below to make a diamond pattern output? I just need help on the 'if' condition.Thanks in advance!


#include<iostream>
#include<cmath>
using namespace std;
int main()
{int x, y;
for (x=1;x<=5;x=x+1)
{
for (y=1;y<=5;y=y+1)
{
if(((abs(x-3)<y)&&(x+3>y)))
{
cout<<"*";
}
else
{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}



Output (without the dashes):
--*
-***
*****
-***
--*
Last edited on
I did this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int x, y, z;
 z=1;
 
 for (x=1;x<=5;x++)
 {
   for (y=1;y<=5;y++)
   {
     if(( (abs(z-3)<y) && (z+3>y) ))
     {
     cout<<"*";
     }
     else
     {
     cout<<" ";
     }
   }
   cout<<endl;
   if(x<3) {
	 z++;
   }
   else {
	 z--;
   }
 }
Thank you very much for that, but Im sorry i forgot mention that i cannot replace or add more than 2 variables. My instructor said the rules are to not change the 'for' operations, to add more variables and to solve only the 'if' condition.
lol. It seems the teacher wants you to solve this in a very complicated way.
You could shove it up the teachers rear by doing this...

 
if((x==1 && y ==3) || (x==2 && y ==2) || (x==2 && y==3) || (x==2 && y==4))


Well, I think you see where I am going with this!!!
Code tags, please.

You almost get there:
1
2
3
4
if ( ( (abs(x-3) < y) && (x+3 > y) ) )
{
  cout << "*";
}

At least the "lower bound" looks reasonable:
1
2
3
4
if ( abs(x-3) < y )
{
  cout << "*";
}

What could be the "upper bound" for each row?
|x-3| : upper
  2   : y < 4
  1   : y < 5
  0   : y < 6
  1   : y < 5
  2   : y < 4

Do you notice a computable relation in that table?
Topic archived. No new replies allowed.