Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *

this is the trial.. but wrong output is found.. please explain...

What I have tried:

C++
#include<iostream.h>
#include<conio.h>

void main()
{
  int i,j,n,count=0,k;
  clrscr();
  cin>>n; //input rows
  for(i=1;i<=n;++i)
  {
    for(j=n;j>i;j--)
    {
      cout<<" ";
    }
    for(k=0;k<2*i-1;k++)
    {
      cout<<"*";
      if(i==n)
      {
        ++count;
      }
    }
    cout<<endl;
  }

  for(i=1;i<=n-1;++i)
  {
    for(j=n;j>n-i;++j)
    {
      cout<<" ";
    }
    for(k=0;k<count-2;++k)>
    {
      cout<<"*";
    }
    cout<<endl;
  }
  getch();
}
Posted
Updated 13-Jun-16 9:27am
v4
Comments
CHill60 13-Jun-16 8:49am    
Have you tried debugging to see if you can work out what is wrong?
Richard MacCutchan 13-Jun-16 10:18am    
1-3-5-7-9-7-5-3-1
How can you achieve that sequence using a for loop, or two for loops?
[no name] 14-Jun-16 5:53am    
bro code is right.. just wanna know the bug in the code..

This is homework, so I'll give you no code!
But...it's not that difficult if you think about it, and break it down into chunks.
Start by writing a function to print a line: two parameters, number of spaces, and number of stars. That's almost trivial!

Then look at your required output: it's in two halves:
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
Increasing stars, then
  * * * * * * *
    * * * * *
      * * *
        *
Decreasing stars.
So treat it as two halves!
Look at the "increasing stars" section and it's really three sections:
. . . . * . . . . 
. . . * * * . . .
. . * * * * * . . 
. * * * * * * * .
* * * * * * * * *
4 spaces, 1 star, 4 spaces.
3 spaces, three stars, 3 spaces.
2 spaces, five stars, 2 spaces.
1 space, seven stars, 1 space.
0 spaces, nine stars, 0 spaces.
Looked at like that, each line has nine characters, so the stars count is (line number * 2) + 1 : 0*2+1 = 1, 1*2+1 = 3, and so on.
And the total spaces count is 9 - the stars count.
So to draw a line, take the line number (starting from zero), double it and add one - that's the number to pass to the "draw a line" function as the "number of stars". And the "number of spaces" is (9 - number of stars) / 2
Decreasing the number of stars is the same process with slightly different calculations.

Try it! It's really not complicated if you think about it.
 
Share this answer
 
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Advice: take a sheet of paper
Note the number you input
for each line in the drawing
- note line number, number of spaces, number of stars
try to find the relation between all those numbers

If it can help, split the drawing in 2 half.
Otherwise the function absolute value may help
    X =-4 -3 -2 -1  0  1  2  3  4
abs(X)= 4  3  2  1  0  1  2  3  4
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Jun-16 21:55pm    
5ed.

I just wanted to say thank you for fixing my mistake in my solution. I removed it.
You are right. My bad. I foolishly assumed that the picture needs right padding as well. No, it doesn't.

—SA
Patrice T 13-Jun-16 22:21pm    
thank you

you welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900