Diamond pattern I almost have it
Apr 27, 2013 at 7:28pm UTC
I'm trying to create a diamond out of stars. I'm close, but clearly I don't quite understand how these for loops work. Can anyone suggest a fix for the bottom half of the diamond??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
cin >> i;
for (i=1; i <=10; i++)
{
for (j = 1; j<=(10-i); j++)
cout << " " ;
for (j=1;j<=i;j++)
cout<< "*" ;
for (j=(i-1); j>=1; j--)
cout << "*" ;
cout << endl;
}
for (i=10; i >=0; i--)
{
for (j=(i-1); j>=1; j--)
cout << "*" ;
for (j=1; j<=i; j++)
cout<< "*" ;
for (j = 10; j<=(10+i); j++)
cout << " " ;
cout<< endl;
}
Apr 27, 2013 at 8:16pm UTC
You want the spaces to come before the *.
For testing I would print a "x" instead of the space so you can see on screen where it appears and if the code is working like you think it is.
for the first line try
1 2
for (int x = 1; x<=10-i; x++)
cout << "x" ;
Apr 27, 2013 at 11:33pm UTC
Thank you so much! Now I have to use that in a function. I'll probably be posting again soon.
Topic archived. No new replies allowed.