Thread: Need help

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    8

    Need help

    I am writing a program that reads two positive integers, W and X,
    and displays a diamond with width W centered at column X on the
    screen. Each displayed diamond should be preceded by a two row header
    that indicates column numbers. Assume we work
    with a screen of total width 75.

    NOTE: I'm NOT asking you to do my homework I just need some help on to how to tackle this question.

    Example:

    width? 4
    column? 13




    12345678901234567890123456789012345678901234567890 1234567890123456789012345
    ................... 1
    .................. 212
    ........... .... 32123
    .......... ... 4321234
    ........... .... 32123
    ............ ..... 212
    ............. ..... 1 .......( EDIT- the .... don't have to be there)


    Note: you must verify that your diamond can fit within the maximum
    screen width. Diamonds must not be truncated or altered. You must
    report weather the diamond would be truncated on the left, right or on
    both sides of the 75 column screen. You can allow the diamond to
    "touch" one of the sides, like in (width 2 / column 2). A width of 1
    should produce the diamond "1".


    What should I do?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Is the diamond suppose to look like that (filled by palindromes of numbers)? Or could it look like this (just a shape):

    Code:
    ................*
    ...............***
    ..............*****
    ...............***
    ................*
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Determine the left and right columns of the diamond.

    When the user wants a diamond of width W... When the centre of it is on column X, its left extreme would be? How far does the diamond shape extend on either side of its centre? Its right extreme would be? Work it out on paper. Draw a diagram. The calculations you come up with will be used to display the statements that the diamond would be truncated on its left or its right side.

    Whatever the distance from the left edge of the screen to the diamond's left edge is, is the number of spaces you need to display before showing any part of the diamond.
    Last edited by nonoob; 10-05-2008 at 11:53 AM.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    8
    it should say something like that.

    width? 10
    column? 70

    The diamond hits the right side.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Your first example has a width of 8, not 4.

    Pseudo-code
    Code:
    x=75-(width/2+col);
    if (x<0) {gets truncated}
    Your second example won't get truncated, but it might hit the right margin depending on how you determine the middle of an even set.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Here's how I would approach it:

    Throw out the "Notes" section, above. Those requirements are refinements - mere details, and should be thrown out - by which I mean put off, for now.

    Now break it down, with a pencil and a big yellow pad (I don't know why it has to be yellow, however).

    Teach yourself to do the core of this, by hand, and then record the steps you use to do it. You can't get the computer to do something you don't know how to do.

    So let's break it down. First, you have to print out the ledger line. Pretty easy:
    Code:
    pseudo-code
    print("123456789)
    for(i = 2; i < 8; i++)  
       print("0123456789")
    something like that.

    Now for the diamond. Each row of the diamond has a sp or starting point, where the first
    char will be printed. That sp will equal the center point of the diamond (which never changes), minus the "width" (which I call the radius of the diamond), minus one, which is the total width of the diamond line, divided by 2, and does change with every line.

    All the above figures are calculated from the leftmost edge of the display.

    In your diamond, above: sp = 13 - 0, is the sp for the first line.
    sp = 13 - 1 is the sp for the second line of your diamond.

    Your diamond is a simple one that always has just one char difference in any row, so this may be overkill for you.

    The key relationship, besides the above, is that each line or row of the diamond will have a starting point 1 less than the line above it, if it's on the top half of the diamond, or one more space added to it, if it's on the lower half of the diamond.

    That should be enough to get you well on your way. Once you know the sp, and the middle column of the diamond, everything else falls into place.

Popular pages Recent additions subscribe to a feed