-3

Hi there I am new to c++ and i need your help please. here is my pattern:

*********
 *******
  *****
   ***
    *       
   ***
  *****
 *******
*********

I tried writing code for this, but it does not give the desired result. Can anyone help me please? here is my code:

#include<bits/stdc++.h>;
using namespace std;
int main()
{
    int n=5,j,k;
    bool o=false,t=false;
    for(int i=((2*n) - 1); i >= -((2*n)-1); i -=2)
    {
        for(j=10-abs(i); j >0;j-=2)
        {
            cout << " ";
        }
        for(k=abs(i);k>0;k--)
        {
            cout << "*";
        }
        cout << endl;
    }
    return 0;
}

my output is:

*********
 *******
  *****
   ***
    * 
    *      
   ***
  *****
 *******
*********

Note: there are two lines with one * whereas the desired output has only one line with one *.

Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
Hikikomori
  • 13
  • 2
  • What specifically are you aiming for? And post the output here directly as text. – Carcigenicate Nov 03 '16 at 14:20
  • @Danh No, the output has two one `*` lines instead of just one. – Eli Sadoff Nov 03 '16 at 14:22
  • You are right i have edit it – Hikikomori Nov 03 '16 at 14:22
  • Your use of `abs` is a good idea, but you're failing to capture the symmetry correctly by not including 0 as a possible value for `i`. The fact that `i` is -1 then +1 is giving you gyp. – Bathsheba Nov 03 '16 at 14:23
  • @bathsheba yes sir. I know I just want your help to fix it. – Hikikomori Nov 03 '16 at 14:25
  • 3
    Forgive me for being patronising, but that's all the help you're gonna get. Else you will not learn. – Bathsheba Nov 03 '16 at 14:26
  • Not related to your problem, but you should turn up your warning level. It shows some untidiness in your code. I recommend `-Wall -Wextra`. Also avoid [`#include `](http://stackoverflow.com/q/31816095/10077) and [`using namespace std;`](http://stackoverflow.com/q/1452721/10077). – Fred Larson Nov 03 '16 at 14:36

2 Answers2

1

Whenever i reaches 1, skip that iteration so that nothing will be printed.

In other words, add the if (i == 1) continue; statement to the outer for loop.

You can find the modified code right here: http://ideone.com/t96XO6

marcelovca90
  • 2,673
  • 3
  • 27
  • 34
0

This is because you decrement your stars with 2 each time and then abs it. i mean. take this example:

***  abs(3) = 3 stars
 *   abs(3-2) = abs(1) = 1 star

then you subtract 2 again, and the result is -1 so..

 *   abs(3-2-2) = abs(3-4) = abs(-1) = 1 star
***  abs(3-2-2-2) = 3 stars
jesse dis
  • 143
  • 6