how to add space to the following star pattern?











up vote
-2
down vote

favorite












How is it possible to print the following pattern?



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


How can we add the spaces during the first half of the pattern?
I only managed to get the second half of the pattern right:



#include <iostream>

using namespace std;

void printpattern(int n)
{
for (int r = 0; r <= n; r++)
{
for (int z = 0; z <= r; z++) {
cout << "*";
}

cout << endl;
}
}

int main()
{
int n = 5;
printpattern(n);
}









share|improve this question
























  • Ad a new loop before the nested loop to print the spaces?
    – Some programmer dude
    Nov 21 at 13:47










  • what 'bout cout << " ";
    – Sigismondo
    Nov 21 at 13:47






  • 4




    A picture of a screen that is rendering plain text? Really?
    – Quentin
    Nov 21 at 13:50










  • Homework? Show more work please. What have you tried? What are the results?
    – Tim
    Nov 21 at 13:51






  • 1




    @Quentin At least it's not as bad as Web 0.1 (sorry for repost, accidentally remove first comment).
    – Some programmer dude
    Nov 21 at 14:02

















up vote
-2
down vote

favorite












How is it possible to print the following pattern?



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


How can we add the spaces during the first half of the pattern?
I only managed to get the second half of the pattern right:



#include <iostream>

using namespace std;

void printpattern(int n)
{
for (int r = 0; r <= n; r++)
{
for (int z = 0; z <= r; z++) {
cout << "*";
}

cout << endl;
}
}

int main()
{
int n = 5;
printpattern(n);
}









share|improve this question
























  • Ad a new loop before the nested loop to print the spaces?
    – Some programmer dude
    Nov 21 at 13:47










  • what 'bout cout << " ";
    – Sigismondo
    Nov 21 at 13:47






  • 4




    A picture of a screen that is rendering plain text? Really?
    – Quentin
    Nov 21 at 13:50










  • Homework? Show more work please. What have you tried? What are the results?
    – Tim
    Nov 21 at 13:51






  • 1




    @Quentin At least it's not as bad as Web 0.1 (sorry for repost, accidentally remove first comment).
    – Some programmer dude
    Nov 21 at 14:02















up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











How is it possible to print the following pattern?



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


How can we add the spaces during the first half of the pattern?
I only managed to get the second half of the pattern right:



#include <iostream>

using namespace std;

void printpattern(int n)
{
for (int r = 0; r <= n; r++)
{
for (int z = 0; z <= r; z++) {
cout << "*";
}

cout << endl;
}
}

int main()
{
int n = 5;
printpattern(n);
}









share|improve this question















How is it possible to print the following pattern?



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


How can we add the spaces during the first half of the pattern?
I only managed to get the second half of the pattern right:



#include <iostream>

using namespace std;

void printpattern(int n)
{
for (int r = 0; r <= n; r++)
{
for (int z = 0; z <= r; z++) {
cout << "*";
}

cout << endl;
}
}

int main()
{
int n = 5;
printpattern(n);
}






c++ loops for-loop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 14:08









Swordfish

8,49611234




8,49611234










asked Nov 21 at 13:45









Sarah_Xx

616




616












  • Ad a new loop before the nested loop to print the spaces?
    – Some programmer dude
    Nov 21 at 13:47










  • what 'bout cout << " ";
    – Sigismondo
    Nov 21 at 13:47






  • 4




    A picture of a screen that is rendering plain text? Really?
    – Quentin
    Nov 21 at 13:50










  • Homework? Show more work please. What have you tried? What are the results?
    – Tim
    Nov 21 at 13:51






  • 1




    @Quentin At least it's not as bad as Web 0.1 (sorry for repost, accidentally remove first comment).
    – Some programmer dude
    Nov 21 at 14:02




















  • Ad a new loop before the nested loop to print the spaces?
    – Some programmer dude
    Nov 21 at 13:47










  • what 'bout cout << " ";
    – Sigismondo
    Nov 21 at 13:47






  • 4




    A picture of a screen that is rendering plain text? Really?
    – Quentin
    Nov 21 at 13:50










  • Homework? Show more work please. What have you tried? What are the results?
    – Tim
    Nov 21 at 13:51






  • 1




    @Quentin At least it's not as bad as Web 0.1 (sorry for repost, accidentally remove first comment).
    – Some programmer dude
    Nov 21 at 14:02


















Ad a new loop before the nested loop to print the spaces?
– Some programmer dude
Nov 21 at 13:47




Ad a new loop before the nested loop to print the spaces?
– Some programmer dude
Nov 21 at 13:47












what 'bout cout << " ";
– Sigismondo
Nov 21 at 13:47




what 'bout cout << " ";
– Sigismondo
Nov 21 at 13:47




4




4




A picture of a screen that is rendering plain text? Really?
– Quentin
Nov 21 at 13:50




A picture of a screen that is rendering plain text? Really?
– Quentin
Nov 21 at 13:50












Homework? Show more work please. What have you tried? What are the results?
– Tim
Nov 21 at 13:51




Homework? Show more work please. What have you tried? What are the results?
– Tim
Nov 21 at 13:51




1




1




@Quentin At least it's not as bad as Web 0.1 (sorry for repost, accidentally remove first comment).
– Some programmer dude
Nov 21 at 14:02






@Quentin At least it's not as bad as Web 0.1 (sorry for repost, accidentally remove first comment).
– Some programmer dude
Nov 21 at 14:02














3 Answers
3






active

oldest

votes

















up vote
1
down vote



accepted










Although comments suggest using std::string, I believe this was intended to be written using only loops. This works:



void printpattern(int n)
{
// print first half.
for (int i = 0; i < n; ++i) {
// print spaces
for (int r = n - i; r > 0; --r)
std::cout << ' ';

// print stars.
for (int j = i; j > 0; --j)
std::cout << '*';

std::cout << 'n';
}

/// print second half. No need to print spaces here.
for (int i = 1; i <= n; ++i) {
for (int r = n - i; r >= 0; --r)
std::cout << '*';

std::cout << 'n';
}
}





share|improve this answer























  • wow that is fascinating how did you analyze it? i really tried but couldn't is it because i am still a beginner or what
    – Sarah_Xx
    Nov 22 at 20:17


















up vote
2
down vote













If one is afraid of using loops such thingies can of course always be solved with recursion and a little math:



#include <iostream>

void pattern(int n, int p = 0)
{
if (!n) return;
if (!p) { pattern(2 * n * n - n, n); return; }
int k = --n / p, o = n % p + 1, t = o - (p - k);
std::cout.put(" *"[k >= p && t < p || k < p && t >= 0]);
--o || std::cout.put('n');
pattern(n, p);
}





share|improve this answer



















  • 1




    I want to be able to upvote this more than once. It's beautiful!
    – Retired Ninja
    Nov 21 at 15:59


















up vote
1
down vote













It's easier to do this by composing a longer string and using a sliding view.



That's pretty straightforward in C++:



#include <iostream>
#include <string>
#include <string_view>

void printpattern(std::size_t n)
{
const auto s = std::string(n, ' ') + std::string(n, '*') + std::string(n, ' ');
for (std::size_t i = 1; i < n*2; ++i)
std::cout << std::string_view(s.data()+i, n) << 'n';
}

int main()
{
printpattern(5);
}


You could of course make the space padding be of length n-1 on both sides, and use a more conventional loop starting i at zero:



    const auto s = std::string(n - 1, ' ') + std::string(n, '*')
+ std::string(n-1, ' ');
for (std::size_t i = 0; i < n * 2 - 1; ++i)


It's up to you whether saving two characters of temporary string is worthwhile.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413464%2fhow-to-add-space-to-the-following-star-pattern%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Although comments suggest using std::string, I believe this was intended to be written using only loops. This works:



    void printpattern(int n)
    {
    // print first half.
    for (int i = 0; i < n; ++i) {
    // print spaces
    for (int r = n - i; r > 0; --r)
    std::cout << ' ';

    // print stars.
    for (int j = i; j > 0; --j)
    std::cout << '*';

    std::cout << 'n';
    }

    /// print second half. No need to print spaces here.
    for (int i = 1; i <= n; ++i) {
    for (int r = n - i; r >= 0; --r)
    std::cout << '*';

    std::cout << 'n';
    }
    }





    share|improve this answer























    • wow that is fascinating how did you analyze it? i really tried but couldn't is it because i am still a beginner or what
      – Sarah_Xx
      Nov 22 at 20:17















    up vote
    1
    down vote



    accepted










    Although comments suggest using std::string, I believe this was intended to be written using only loops. This works:



    void printpattern(int n)
    {
    // print first half.
    for (int i = 0; i < n; ++i) {
    // print spaces
    for (int r = n - i; r > 0; --r)
    std::cout << ' ';

    // print stars.
    for (int j = i; j > 0; --j)
    std::cout << '*';

    std::cout << 'n';
    }

    /// print second half. No need to print spaces here.
    for (int i = 1; i <= n; ++i) {
    for (int r = n - i; r >= 0; --r)
    std::cout << '*';

    std::cout << 'n';
    }
    }





    share|improve this answer























    • wow that is fascinating how did you analyze it? i really tried but couldn't is it because i am still a beginner or what
      – Sarah_Xx
      Nov 22 at 20:17













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    Although comments suggest using std::string, I believe this was intended to be written using only loops. This works:



    void printpattern(int n)
    {
    // print first half.
    for (int i = 0; i < n; ++i) {
    // print spaces
    for (int r = n - i; r > 0; --r)
    std::cout << ' ';

    // print stars.
    for (int j = i; j > 0; --j)
    std::cout << '*';

    std::cout << 'n';
    }

    /// print second half. No need to print spaces here.
    for (int i = 1; i <= n; ++i) {
    for (int r = n - i; r >= 0; --r)
    std::cout << '*';

    std::cout << 'n';
    }
    }





    share|improve this answer














    Although comments suggest using std::string, I believe this was intended to be written using only loops. This works:



    void printpattern(int n)
    {
    // print first half.
    for (int i = 0; i < n; ++i) {
    // print spaces
    for (int r = n - i; r > 0; --r)
    std::cout << ' ';

    // print stars.
    for (int j = i; j > 0; --j)
    std::cout << '*';

    std::cout << 'n';
    }

    /// print second half. No need to print spaces here.
    for (int i = 1; i <= n; ++i) {
    for (int r = n - i; r >= 0; --r)
    std::cout << '*';

    std::cout << 'n';
    }
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 at 14:31

























    answered Nov 21 at 14:20









    sstefan

    312211




    312211












    • wow that is fascinating how did you analyze it? i really tried but couldn't is it because i am still a beginner or what
      – Sarah_Xx
      Nov 22 at 20:17


















    • wow that is fascinating how did you analyze it? i really tried but couldn't is it because i am still a beginner or what
      – Sarah_Xx
      Nov 22 at 20:17
















    wow that is fascinating how did you analyze it? i really tried but couldn't is it because i am still a beginner or what
    – Sarah_Xx
    Nov 22 at 20:17




    wow that is fascinating how did you analyze it? i really tried but couldn't is it because i am still a beginner or what
    – Sarah_Xx
    Nov 22 at 20:17












    up vote
    2
    down vote













    If one is afraid of using loops such thingies can of course always be solved with recursion and a little math:



    #include <iostream>

    void pattern(int n, int p = 0)
    {
    if (!n) return;
    if (!p) { pattern(2 * n * n - n, n); return; }
    int k = --n / p, o = n % p + 1, t = o - (p - k);
    std::cout.put(" *"[k >= p && t < p || k < p && t >= 0]);
    --o || std::cout.put('n');
    pattern(n, p);
    }





    share|improve this answer



















    • 1




      I want to be able to upvote this more than once. It's beautiful!
      – Retired Ninja
      Nov 21 at 15:59















    up vote
    2
    down vote













    If one is afraid of using loops such thingies can of course always be solved with recursion and a little math:



    #include <iostream>

    void pattern(int n, int p = 0)
    {
    if (!n) return;
    if (!p) { pattern(2 * n * n - n, n); return; }
    int k = --n / p, o = n % p + 1, t = o - (p - k);
    std::cout.put(" *"[k >= p && t < p || k < p && t >= 0]);
    --o || std::cout.put('n');
    pattern(n, p);
    }





    share|improve this answer



















    • 1




      I want to be able to upvote this more than once. It's beautiful!
      – Retired Ninja
      Nov 21 at 15:59













    up vote
    2
    down vote










    up vote
    2
    down vote









    If one is afraid of using loops such thingies can of course always be solved with recursion and a little math:



    #include <iostream>

    void pattern(int n, int p = 0)
    {
    if (!n) return;
    if (!p) { pattern(2 * n * n - n, n); return; }
    int k = --n / p, o = n % p + 1, t = o - (p - k);
    std::cout.put(" *"[k >= p && t < p || k < p && t >= 0]);
    --o || std::cout.put('n');
    pattern(n, p);
    }





    share|improve this answer














    If one is afraid of using loops such thingies can of course always be solved with recursion and a little math:



    #include <iostream>

    void pattern(int n, int p = 0)
    {
    if (!n) return;
    if (!p) { pattern(2 * n * n - n, n); return; }
    int k = --n / p, o = n % p + 1, t = o - (p - k);
    std::cout.put(" *"[k >= p && t < p || k < p && t >= 0]);
    --o || std::cout.put('n');
    pattern(n, p);
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 at 16:25

























    answered Nov 21 at 15:41









    Swordfish

    8,49611234




    8,49611234








    • 1




      I want to be able to upvote this more than once. It's beautiful!
      – Retired Ninja
      Nov 21 at 15:59














    • 1




      I want to be able to upvote this more than once. It's beautiful!
      – Retired Ninja
      Nov 21 at 15:59








    1




    1




    I want to be able to upvote this more than once. It's beautiful!
    – Retired Ninja
    Nov 21 at 15:59




    I want to be able to upvote this more than once. It's beautiful!
    – Retired Ninja
    Nov 21 at 15:59










    up vote
    1
    down vote













    It's easier to do this by composing a longer string and using a sliding view.



    That's pretty straightforward in C++:



    #include <iostream>
    #include <string>
    #include <string_view>

    void printpattern(std::size_t n)
    {
    const auto s = std::string(n, ' ') + std::string(n, '*') + std::string(n, ' ');
    for (std::size_t i = 1; i < n*2; ++i)
    std::cout << std::string_view(s.data()+i, n) << 'n';
    }

    int main()
    {
    printpattern(5);
    }


    You could of course make the space padding be of length n-1 on both sides, and use a more conventional loop starting i at zero:



        const auto s = std::string(n - 1, ' ') + std::string(n, '*')
    + std::string(n-1, ' ');
    for (std::size_t i = 0; i < n * 2 - 1; ++i)


    It's up to you whether saving two characters of temporary string is worthwhile.






    share|improve this answer



























      up vote
      1
      down vote













      It's easier to do this by composing a longer string and using a sliding view.



      That's pretty straightforward in C++:



      #include <iostream>
      #include <string>
      #include <string_view>

      void printpattern(std::size_t n)
      {
      const auto s = std::string(n, ' ') + std::string(n, '*') + std::string(n, ' ');
      for (std::size_t i = 1; i < n*2; ++i)
      std::cout << std::string_view(s.data()+i, n) << 'n';
      }

      int main()
      {
      printpattern(5);
      }


      You could of course make the space padding be of length n-1 on both sides, and use a more conventional loop starting i at zero:



          const auto s = std::string(n - 1, ' ') + std::string(n, '*')
      + std::string(n-1, ' ');
      for (std::size_t i = 0; i < n * 2 - 1; ++i)


      It's up to you whether saving two characters of temporary string is worthwhile.






      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        It's easier to do this by composing a longer string and using a sliding view.



        That's pretty straightforward in C++:



        #include <iostream>
        #include <string>
        #include <string_view>

        void printpattern(std::size_t n)
        {
        const auto s = std::string(n, ' ') + std::string(n, '*') + std::string(n, ' ');
        for (std::size_t i = 1; i < n*2; ++i)
        std::cout << std::string_view(s.data()+i, n) << 'n';
        }

        int main()
        {
        printpattern(5);
        }


        You could of course make the space padding be of length n-1 on both sides, and use a more conventional loop starting i at zero:



            const auto s = std::string(n - 1, ' ') + std::string(n, '*')
        + std::string(n-1, ' ');
        for (std::size_t i = 0; i < n * 2 - 1; ++i)


        It's up to you whether saving two characters of temporary string is worthwhile.






        share|improve this answer














        It's easier to do this by composing a longer string and using a sliding view.



        That's pretty straightforward in C++:



        #include <iostream>
        #include <string>
        #include <string_view>

        void printpattern(std::size_t n)
        {
        const auto s = std::string(n, ' ') + std::string(n, '*') + std::string(n, ' ');
        for (std::size_t i = 1; i < n*2; ++i)
        std::cout << std::string_view(s.data()+i, n) << 'n';
        }

        int main()
        {
        printpattern(5);
        }


        You could of course make the space padding be of length n-1 on both sides, and use a more conventional loop starting i at zero:



            const auto s = std::string(n - 1, ' ') + std::string(n, '*')
        + std::string(n-1, ' ');
        for (std::size_t i = 0; i < n * 2 - 1; ++i)


        It's up to you whether saving two characters of temporary string is worthwhile.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 16:36

























        answered Nov 21 at 15:59









        Toby Speight

        16k133965




        16k133965






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413464%2fhow-to-add-space-to-the-following-star-pattern%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)