For loops to create four right triangle histograms...HELP!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dannyyoder1978
    New Member
    • Feb 2008
    • 2

    #16
    I know this thread is outdated, but you know, 2 cents.

    I am not sure on the code right now, but what you need to do is realize the difference between printing just one of those triangles out, stacked on top of each other, and having them all lined up. You need a starting loop that is going to define how many total LINES there are, and then a loop that is going to define how many characters, including the spaces, that are going on each line.

    next:
    You have up facing triangles @ 1 char each. You have 2 triangles that have their bases up @ 10 chars each. Then you have 3 spaces. So the first line is going to look like this:

    * ********** ********** * = 25 chars
    You need to find a way to adjust where the " " is going to be for lines 2 -10. The middle space will always be the same, the first space will need to be added to each time the loop comes around so that it moves right, the third space needs to be subtracted from so that it moves left.

    I am working and learning with C# and just finished a similar exercise where we just made the same shapes, just stacked on top of each other. I will see if I can come up with a solution to post here for you and others.

    Comment

    • dannyyoder1978
      New Member
      • Feb 2008
      • 2

      #17
      I'm back, and sooner than I thought :)

      Here is the code in C#, but it can be converted easily to any language.

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

      int space1 = 2; //variable that will move the first spacer right
      int space2 = 24; //variable that will move the third space left

      for (int L = 1; L <= 10; L++) //determines number of lines
      { //start first loop

      for (int C = 1; C <= 25; C++) // determines chars per line

      { //start second loop

      if (C == space1) // if the char location = space1, print a space

      {

      Console.Write(" ");

      }

      else if (C == 13) // if the char location = 13 (the middle) print space

      {

      Console.Write(" ");

      }

      else if (C == space2) //if the char location = space2, print space

      {

      Console.Write(" ");

      }

      else

      {

      Console.Write(" *"); //all else fails, print *

      }

      }//end second loop

      space1++; //incr. space1 by one
      space2--; // decr. space2 by one

      Console.WriteLi ne(" "); // new line

      } //end first loop

      --------------------------------------------------------------
      Hope this helps, but I encourage you to study it and understand exactly how it is working instead of just copying it, or else you haven't learned anything, really.

      Comment

      Working...