DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Help to understand the program

  1. #1
    pavel Guest

    Help to understand the program


    2) Write a simple C++ program that inputs a positive integer n and then prints
    2n -1 lines of asterisks with width increased from 1 through 2n -1 asterisks
    then decreased back to 1 in a way to form a parallelogram. For example,
    if the input is 5 then the output should be


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


  2. #2
    Chris McGinlay Guest

    Re: Help to understand the program

    pavel wrote:

    >
    > 2) Write a simple C++ program that inputs a positive integer n and then
    > prints 2n -1 lines of asterisks with width increased from 1 through 2n -1
    > asterisks
    > then decreased back to 1 in a way to form a parallelogram. For example,
    > if the input is 5 then the output should be
    >
    >
    > *
    > ***
    > *****
    > *******
    > *********
    > *******
    > *****
    > ***
    > *


    To get you started:

    int main()
    {
    int n=0;
    std::cout << "Please enter n \n";
    std::cin >> n;

    //wax
    for (int i=1; i <= 2*n-1; i+=2)
    {
    for (int k=1; k <= i; k++) std::cout << "*";
    std::cout << std::endl;
    }

    //wane
    //TODO

    return 0;
    }

    --
    www.ascent.zetnet.co.uk
    Standards Compliant Web Design

  3. #3
    Dennis Guest

    Re: Help to understand the program


    Chris McGinlay <ascent@zetnet.co.uk> wrote:
    >pavel wrote:
    >
    >>
    >> 2) Write a simple C++ program that inputs a positive integer n and

    then
    >> prints 2n -1 lines of asterisks with width increased from 1 through 2n

    -1
    >> asterisks
    >> then decreased back to 1 in a way to form a parallelogram. For example,
    >> if the input is 5 then the output should be
    >>
    >>
    >> *
    >> ***
    >> *****
    >> *******
    >> *********
    >> *******
    >> *****
    >> ***
    >> *

    >
    >To get you started:
    >
    >int main()
    >{
    > int n=0;
    > std::cout << "Please enter n \n";
    > std::cin >> n;
    >
    > //wax
    > for (int i=1; i <= 2*n-1; i+=2)
    > {
    > for (int k=1; k <= i; k++) std::cout << "*";
    > std::cout << std::endl;
    > }
    >
    > //wane
    > //TODO
    >
    > return 0;
    >}
    >
    >--
    >www.ascent.zetnet.co.uk
    >Standards Compliant Web Design



  4. #4
    Dennis Guest

    Re: Help to understand the program


    Chris McGinlay <ascent@zetnet.co.uk> wrote:
    >pavel wrote:
    >
    >>
    >> 2) Write a simple C++ program that inputs a positive integer n and

    then
    >> prints 2n -1 lines of asterisks with width increased from 1 through 2n

    -1
    >> asterisks
    >> then decreased back to 1 in a way to form a parallelogram. For example,
    >> if the input is 5 then the output should be
    >>
    >>
    >> *
    >> ***
    >> *****
    >> *******
    >> *********
    >> *******
    >> *****
    >> ***
    >> *

    >
    >To get you started:
    >
    >int main()
    >{
    > int n=0;
    > std::cout << "Please enter n \n";
    > std::cin >> n;
    >
    > //wax
    > for (int i=1; i <= 2*n-1; i+=2)
    > {
    > for (int k=1; k <= i; k++) std::cout << "*";
    > std::cout << std::endl;
    > }
    >
    > //wane
    > //TODO
    >
    > return 0;
    >}
    >
    >--
    >www.ascent.zetnet.co.uk
    >Standards Compliant Web Design


    The second for loop could look like the following... but try to use your
    own otherwise you will never be what you want to be...

    for (int j = 1; j < n; j++)
    {
    cout << setw(2 * j + 1);
    for (int k = (2 * n - 1) - (2 * j); k > 0; k--)
    cout << "*";
    cout << endl;
    }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center