CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2010
    Posts
    17

    Need Help With This Code !!!!

    hi
    i write this code but i phase a problem :::

    her the code

    Code:
    #include<iostream.h>
    #include<conio.h>
    int main()
    {
    
    int i,j,k,l=1,num;
    cout<<"Enter the no:"<<endl;
    cin>>num;
    for(k=num;k>=1;k--)
    { for(i=k;i>=1;i--)
    cout<<" ";
    if(l<num)
    { for(j=1;j<=l;j++)
    cout<<"*"<<" ";
    }
    l++;
    cout<<endl;
    
    }
    for(k=num; k>=1; k--)
    {
    for(j=1; j<=l; j++)
    {
    cout<<" ";
    }
    if( l <= num)
    {
    for(i=k; i>=1; i--)
    cout << "*" << " ";
    }
    l++;
    cout<<endl;
    }
    getch();
    return 0;
    
    }



    the out Put is

    Code:
            *
           * *
          * * *
         * * * *
        * * * * *
       * * * * * *
      * * * * * * *



    the Output Should be

    Code:
             *
            * *
           * * *
          * * * *
         * * * * *
        * * * * * *
       * * * * * * *
      * * * * * * * *
       * * * * * * *
        * * * * * *
         * * * * *
          * * * *
           * * *
            * *
             *

    where is the problem ....Please Help


    Re grads

  2. #2
    Join Date
    Aug 2008
    Posts
    373

    Re: Need Help With This Code !!!!

    Try this one
    Code:
    int main()
    {
    
    int i,j,k,l=1,num;
    cout<<"Enter the no:"<<endl;
    cin>>num;
    for(k=num;k>=1;k--)
    { for(i=k;i>=1;i--)
    cout<<" ";
    if(l<num)
    { for(j=1;j<=l;j++)
    cout<<"*"<<" ";
    }
    l++;
    if(num==l)
    {
    }
    else
    {
    	cout<<endl;
    }
    
    }
    int kp=1;
    for(k=num; k>=1; k--)
    {
    	kp++;
    for(j=1; j<=kp; j++)
    {
    cout<<" ";
    }
    if( l >= num)
    {
    for(i=k; i-1>=1; i--)
    cout << "*" << " ";
    }
    l++;
    cout<<endl;
    }
    getch();
    return 0;
    
    }

  3. #3
    Join Date
    Oct 2010
    Posts
    11

    Re: Need Help With This Code !!!!

    Msm: Unless I missed my guess your code I think it will actually print

    Code:
         *
        * *
       * * *
      * * * *
      * * * *
       * * *
        * *
         *
    when what he wants is
    Code:
         *
        * *
       * * *
      * * * *
     * * * * *
      * * * *
       * * *
        * *
         *
    mot1639: Try this
    Code:
    int main(int argc, char* argv[])
    {
       int i,j,k,l=1,num;
       cout<<"Enter the no:"<<endl;
       cin>>num;
       for(k=num;k>=1;k--)
       { 
          for(i=k;i>=1;i--)
             cout<<" ";
          if(l<num)
          { 
             for(j=1;j<=l;j++)
                cout<<"*"<<" ";
          }
          l++;
          if (k > 1) // skip new line on last iteration
             cout<<endl;
       }
       l = 1; // mot1639 forgot to init l which is why there were all blanks
       for(k=num; k>=1; k--)
       {                          
          if(k!=num)  // <--- don't want to add a space on the widest row which
             for(j=1; j<=l; j++)  // occurs the first time through this loop
                cout<<" ";
          if( l <= num)
          {
             for(i=k; i>=1; i--)
                cout << "*" << " ";
          }
          l++;
          cout<<endl;
       }
    getch();
    return 0;
    
    }

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Need Help With This Code !!!!

    How about having the OP do his/her own homework?

    The problem is easy enough for the OP to fix for themself without giving away answers. Since many teachers do look around the Web for copied answers, your posts could get the OP in a lot of trouble.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 20th, 2010 at 06:40 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured