Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help with C

Help with C

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionlearning
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Faith Burnett
    wrote on last edited by
    #1

    I have been taking the free CS50x course from Harvard and just completed the Mario more comfortable problem. This is my code and I wanted to know how the right side of hashes are left aligned even though the code is the same for both left and right hashes.? To be honest I’m new to this and thought it would simply print out two right aligned pyramids with a gap in between. // Prints bricks, sized as specified by user #include #include int main(void) { // Prompt user for a positive number to use as height of pyramid int h; do { h = get_int("Height: "); } while (h < 0 || h > 23); // Print out this many rows for (int i = 0; i < h; i++) { // Print out this many spaces for (int j = 0; j < h - i; j++) { printf(" "); } // Print left hashes for (int j = 0; j < i + 2; j++) { printf("#"); } // Print gap printf(" "); // Print right hashes for (int j = 0; j < i + 2; j++) { printf("#"); } printf("\n"); } }

    D 1 Reply Last reply
    0
    • F Faith Burnett

      I have been taking the free CS50x course from Harvard and just completed the Mario more comfortable problem. This is my code and I wanted to know how the right side of hashes are left aligned even though the code is the same for both left and right hashes.? To be honest I’m new to this and thought it would simply print out two right aligned pyramids with a gap in between. // Prints bricks, sized as specified by user #include #include int main(void) { // Prompt user for a positive number to use as height of pyramid int h; do { h = get_int("Height: "); } while (h < 0 || h > 23); // Print out this many rows for (int i = 0; i < h; i++) { // Print out this many spaces for (int j = 0; j < h - i; j++) { printf(" "); } // Print left hashes for (int j = 0; j < i + 2; j++) { printf("#"); } // Print gap printf(" "); // Print right hashes for (int j = 0; j < i + 2; j++) { printf("#"); } printf("\n"); } }

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Faith Burnett wrote:

      To be honest I’m new to this and thought it would simply print out two right aligned pyramids with a gap in between.

      Those two for() loops are printing the same NUMBER of hashes from where the stdout stream happens to be at the point the for() loop starts printing. Since the stdout stream is at "column" 0 before the first for() loop, the first set of hashes is right justified (preceded with spaces), and since the stdout stream is at "column" H+2 after the first two inner for() loops run, the second set of hashes always starts in the same "column." You might want to consider changing your for() loop boundaries so that the pyramid has a "pointed" top at level 1, and no leading/trailing spaces at level h:

      for (int i = 0; i < h; i++)
      {
      ...
      // Print out this many spaces
      for (int j = 0; j < h - i - 1; j++)
      ...
      // Print left hashes
      for (int j = 0; j < i + 1; j++)
      ...
      // Print right hashes
      for (int j = 0; j < i + 1; j++)
      ...
      }

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      F 1 Reply Last reply
      0
      • D David Crow

        Faith Burnett wrote:

        To be honest I’m new to this and thought it would simply print out two right aligned pyramids with a gap in between.

        Those two for() loops are printing the same NUMBER of hashes from where the stdout stream happens to be at the point the for() loop starts printing. Since the stdout stream is at "column" 0 before the first for() loop, the first set of hashes is right justified (preceded with spaces), and since the stdout stream is at "column" H+2 after the first two inner for() loops run, the second set of hashes always starts in the same "column." You might want to consider changing your for() loop boundaries so that the pyramid has a "pointed" top at level 1, and no leading/trailing spaces at level h:

        for (int i = 0; i < h; i++)
        {
        ...
        // Print out this many spaces
        for (int j = 0; j < h - i - 1; j++)
        ...
        // Print left hashes
        for (int j = 0; j < i + 1; j++)
        ...
        // Print right hashes
        for (int j = 0; j < i + 1; j++)
        ...
        }

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        F Offline
        F Offline
        Faith Burnett
        wrote on last edited by
        #3

        Thanks for the help David!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups