Array Display (Rhyme time)

Discussion in 'C++' started by fb, Dec 2, 2004.

  1. fb

    fb Guest

    I am tring to print an array of 100 elements in a grid like pattern.
    ex:
    +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
    +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
    +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
    +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000

    The problem is that Memory[0] is printed on a line by itself...
    Let's say I've populated the array (somewhat) with the following values:
    1007, 1008, 2007, 3008, 2109, 1109, 4300

    It get's printed as:

    +1007
    +1008 +2007 +3008 +2109 +1109 +4300 +0004 +0006 +0010
    +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
    +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
    +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 etc...

    The code below is what I am using to do the output. I've fiddled with
    it for so long now, i'm almost sure there is no solution to this
    problem...If you have any ideas, feel free to let me know. Thanks.

    (p.s. everything missing std:: is in global namespace)

    counter = 0;
    while (counter <= 100){
    cout << showpos << setfill('0') << setw(5)
    << std::internal << Memory[counter] << " ";
    if (counter % 10 == 0)
    cout << endl;
    counter++;
    }

    fb
    --
    Having read the tea leaves and disembowelled a newborn
    goat and examined the entrails I can say with confidence
    that you need to refactor the inner loop into a service
    locator pattern and use a observer pattern to count the
    number of cubits.

    I can also say with confidence that you are going to get flamed.

    -Peter Hickman
     
    fb, Dec 2, 2004
    #1
    1. Advertisements

  2. "fb" <> wrote in message
    news:n7yrd.409118$%k.335097@pd7tw2no...
    >I am tring to print an array of 100 elements in a grid like pattern.
    > ex:
    > +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
    > +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
    > +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
    > +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000
    >
    > The problem is that Memory[0] is printed on a line by itself...


    > counter = 0;
    > while (counter <= 100){
    > cout << showpos << setfill('0') << setw(5)
    > << std::internal << Memory[counter] << " ";
    > if (counter % 10 == 0)
    > cout << endl;
    > counter++;
    > }


    > I've fiddled with it for so long now, i'm almost sure there is no solution
    > to this problem...If you have any ideas, feel free to let me know.


    Probably the easiest solution is to reorder the last three lines before
    the }:

    counter++;
    if (counter % 10 == 0)
    cout << endl;

    Now that you've seen a solution, can you give us any insight as to why your
    previous fiddling didn't find it?
     
    Andrew Koenig, Dec 2, 2004
    #2
    1. Advertisements

  3. fb

    josh Guest

    fb wrote:
    > I am tring to print an array of 100 elements in a grid like pattern.
    > ex:
    > +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000 +0000


    [...]

    > It gets printed as:
    >
    > +1007
    > +1008 +2007 +3008 +2109 +1109 +4300 +0004 +0006 +0010


    Change Memory[counter] to (counter % 10) and, ignoring the extra
    formatting, you'd see:
    0
    1 2 3 4 5 6 7 8 9 0
    1 2 3 4 5 6 7 8 9 0

    where you want:
    0 1 2 3 4 5 6 7 8 9
    0 1 2 3 4 5 6 7 8 9

    So I'd think the most obvious solution would be:

    > counter = 0;
    > while (counter <= 100){
    > cout << showpos << setfill('0') << setw(5)
    > << std::internal << Memory[counter] << " ";
    > if (counter % 10 == 0)


    if (counter % 10 == 9)

    > cout << endl;
    > counter++;
    > }


    -josh
     
    josh, Dec 2, 2004
    #3
  4. In article <9Bzrd.179024$HA.22520@attbi_s01>, josh
    <> writes
    >So I'd think the most obvious solution would be:


    Obvious is in the eye of the beholder (or mind of the speaker). If you
    are thinking in terms of going to a new line every time the count
    reaches a denary value ending in a nine, yes. If you are thinking in
    terms of 'every ten' then no.

    >
    >> counter = 0;
    >> while (counter <= 100){
    >> cout << showpos << setfill('0') << setw(5)
    >> << std::internal << Memory[counter] << " ";
    >> if (counter % 10 == 0)

    >
    > if (counter % 10 == 9)
    >
    >> cout << endl;
    >> counter++;
    >> }


    Actually, I do not like that code one bit quite apart from the test for
    a new line. Quick, how many lines of output will there be? That use of
    100 is a really vicious magic number. If there are 110 elements the code
    should say so.

    I would prefer:

    int const number_of_elements(110);
    int const number_per_line(10);
    int const lines(number_of_elements/number_per_line);
    if(number_of_elements%number_per_line != 0) lines++;
    int counter(0);
    while (lines--){
    for(int i(0); i != number_per_line; ++i){
    cout << showpos << setfill('0') << setw(5)
    << internal << Memory[counter++] << " ";
    }
    cout << endl;
    }

    Yes, it is more verbose, but it is self documenting and, I believe,
    clearly correct (assuming that the previous code actually handled the
    element count correctly). Note that, IMO, either manipulators should be
    consistently qualified with std:: (my preference) or not at all. Also,
    number_of_elements is probably already known, by being the dimension of
    'Memory'




    --
    Francis Glassborow ACCU
    Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
    For project ideas and contributions: http://www.spellen.org/youcandoit/projects
     
    Francis Glassborow, Dec 2, 2004
    #4
  5. fb

    Jack D Guest

    On 2004-12-02 12:22:57 +0100, Francis Glassborow
    <> said:

    > I would prefer:
    >
    > int const number_of_elements(110);
    > int const number_per_line(10);
    > int const lines(number_of_elements/number_per_line);
    > if(number_of_elements%number_per_line != 0) lines++;
    > int counter(0);
    > while (lines--){
    > for(int i(0); i != number_per_line; ++i){
    > cout << showpos << setfill('0') << setw(5)
    > << internal << Memory[counter++] << " ";
    > }
    > cout << endl;
    > }


    I'd make that:
    for(int i(0); i != number_per_line && lines; ++i){

    in case number_of_elements%number_per_line != 0
     
    Jack D, Dec 2, 2004
    #5
  6. fb

    Jack D Guest

    On 2004-12-02 14:01:39 +0100, Jack D <> said:

    > On 2004-12-02 12:22:57 +0100, Francis Glassborow
    > <> said:
    >
    >> I would prefer:
    >>
    >> int const number_of_elements(110);
    >> int const number_per_line(10);
    >> int const lines(number_of_elements/number_per_line);
    >> if(number_of_elements%number_per_line != 0) lines++;
    >> int counter(0);
    >> while (lines--){
    >> for(int i(0); i != number_per_line; ++i){
    >> cout << showpos << setfill('0') << setw(5)
    >> << internal << Memory[counter++] << " ";
    >> }
    >> cout << endl;
    >> }

    >
    > I'd make that:
    > for(int i(0); i != number_per_line && lines; ++i){
    >
    > in case number_of_elements%number_per_line != 0


    Sorry, should of course be:

    for(int i(0); i != number_per_line && (counter < number_of_elements); ++i){
     
    Jack D, Dec 2, 2004
    #6
  7. fb

    josh Guest

    Francis Glassborow wrote:
    > In article <9Bzrd.179024$HA.22520@attbi_s01>, josh
    > <> writes
    >
    >>So I'd think the most obvious solution would be:

    >
    > Obvious is in the eye of the beholder (or mind of the speaker). If you


    That's true. But then that's why I said "I'd think" :)

    > are thinking in terms of going to a new line every time the count
    > reaches a denary value ending in a nine, yes. If you are thinking in
    > terms of 'every ten' then no.


    The original already gave a new line every ten. That much of the
    problem is already solved. What's wrong is that the new line is coming
    after the wrong element.

    What is wrong with the code? Something in the condition for starting a
    new line. Examine that condition. (Hence the "diagrams".) What do you
    need to make the picture you get look like the picture you want? Move 9
    items up to the first line. What will do that? Moving the newline from
    after (counter % 10 == 0) to after (counter % 10 == 9), 9 items later.

    Perhaps ((counter + 1) % 10 == 0) would be more obvious, since things
    are right starting with the second element... (or maybe... putting
    counter++; before the condition...)

    >>>counter = 0;
    >>>while (counter <= 100){


    Now that I actually pay attention to it, that condition is wrong. "an
    array of 100 elements" would need (counter < 100).

    >>> cout << showpos << setfill('0') << setw(5)
    >>> << std::internal << Memory[counter] << " ";
    >>> if (counter % 10 == 0)

    >>
    >> if (counter % 10 == 9)
    >>
    >>
    >>> cout << endl;
    >>> counter++;
    >>>}

    >
    > Actually, I do not like that code one bit quite apart from the test for
    > a new line. Quick, how many lines of output will there be? That use of
    > 100 is a really vicious magic number. If there are 110 elements the code
    > should say so.


    I quite like the "counter = 0;" part, myself. ;)

    -josh
     
    josh, Dec 3, 2004
    #7
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SG
    Replies:
    6
    Views:
    31,030
    ricuzzo
    Jan 15, 2008
  2. engsol
    Replies:
    2
    Views:
    1,024
    Dan Bishop
    Jan 26, 2004
  3. Replies:
    8
    Views:
    549
    Magnus Lycka
    Aug 5, 2005
  4. Peter Hansen
    Replies:
    0
    Views:
    753
    Peter Hansen
    Feb 22, 2006
  5. flamesrock
    Replies:
    8
    Views:
    595
    Hendrik van Rooyen
    Nov 24, 2006
Loading...

Share This Page