Print star ('*') diamond in C with nested loops?

前端 未结 10 1643
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 14:59

I want to be able to print a diamond like this when the user enters 5 for the diamond. But also will work for any value that is odd and greater than 0.

10条回答
  •  被撕碎了的回忆
    2021-01-07 15:28

    I struggled with this a bit. Also sorry, my final code is C++, not strict C, as I haven't used C in 10 years. Here's a more wordy explanation for those trying to understand how to work this out for yourself. Before I begin, one thing you can do to help yourself out is start with a declaration like:

    int numStars = 1, numSpaces = 10;

    numSpaces doesn't really matter as long as it's small enough to fit on your screen... you can plug in any number and test it later. Then, work out your algorithm, and later on you can work on handling input.

    Also, the number of spaces AFTER the stars is irrelevant for this problem.

    Then, solve the problem of how to output the top portion of the diamond. I used a counter loop to print spaces until I'd printed the number of spaces for the first row, then a second loop (not nested) to print the number of stars needed for that row. We know that in row 1, we need to print a space 10 times and a star one time.

    Once the row has the number of spaces and stars required, you go to the newline ("\n" or endl). But the next row you need to print 2 more stars and 1 less spaces. So you must increment/decrement appropriately.

    When you get to the row before the middle row, you'll stop. In other words, the top loop needs to continue while numSpaces is greater than 0. At the end of thefinal pass in the loop, numSpaces is decremented to 0, but it won't print the line with 0 spaces and n stars (2 stars for every space you started with). That happens in your next loop...

    Then, you can run the second loop that reverses your increment/decrement at the end, but is otherwise almost the same. Only this time, it should print 0 spaces and 10n stars. Then the newline, and finally decrease the numStars by 2 and increase numSpaces by 1.

    If you're not using functions yet or not allowed to in implementation, that's just about it. You could easily call a function, though, passing it numSpaces/numStars, for each of the loops to eliminate duplicate code.

    Here's my code in C++:

    #include 
    
    using namespace std;
    
    int main()
    {
        int     numStars = 1,
                numSpaces = 10;
    
        while (numSpaces > 0)
        {
            for (int i = 0; i < numSpaces; i++)
            {
                cout << " ";
            }
            for (int i = 0; i < numStars; i++)
            {
                cout << "*";
            }
            cout << "\n";
    
            numStars += 2;
            numSpaces--;
        }
        while (numStars > 0)
        {
            for (int i = 0; i < numSpaces; i++)
            {
                cout << " ";
            }
            for (int i = 0; i < numStars; i++)
            {
                cout << "*";
            }
            cout << "\n";
            numStars -= 2;
            numSpaces++;
        }
    
        return 0;
    }
    

提交回复
热议问题