How does this C for-loop print text-art pyramids?

后端 未结 10 1908
灰色年华
灰色年华 2021-02-05 04:24

This is my first time posting in here, hopefully I am doing it right.

Basically I need help trying to figure out some code that I wrote for class using C. The purpose o

10条回答
  •  -上瘾入骨i
    2021-02-05 04:53

    The simplest answer to why that is happening is because one loop prints spaces like this represented by -.

    ----------
    ---------
    --------
    -------
    ------
    -----
    

    and so on. The hashes are printed like this

    ------#
    -----##
    ----###
    ---####
    --#####
    -######
    

    since hash printing loop at second stage needs to print equal no of hashes more to complete the pyramid it can be solved by two methods one by copying and pasting hash loop twice or by making the loop run twice next time by following modification

    for ( int hash = 0; hash <= tall*2; hash++ )
            {
                printf ( "#" );
            }
    

    The logic to build such loops is simple the outermost loop prints one line feed to seperate loops,the inner loops are responsible for each lines contents. The space loop puts spaces and hash loop appends hash at end of spaces. [My answer can be redundant because i did not read other answers so much carefully,they were long] Result:

           #
          ###
         #####
        #######
       #########
      ###########
    

提交回复
热议问题