Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Array Display (Rhyme time)

Reply
Thread Tools

Array Display (Rhyme time)

 
 
fb
Guest
Posts: n/a
 
      12-02-2004
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

 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      12-02-2004
"fb" <(E-Mail Removed)> 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?


 
Reply With Quote
 
 
 
 
josh
Guest
Posts: n/a
 
      12-02-2004
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

 
Reply With Quote
 
Francis Glassborow
Guest
Posts: n/a
 
      12-02-2004
In article <9Bzrd.179024$HA.22520@attbi_s01>, josh
<(E-Mail Removed)> 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
 
Reply With Quote
 
Jack D
Guest
Posts: n/a
 
      12-02-2004
On 2004-12-02 12:22:57 +0100, Francis Glassborow
<(E-Mail Removed)> 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

 
Reply With Quote
 
Jack D
Guest
Posts: n/a
 
      12-02-2004
On 2004-12-02 14:01:39 +0100, Jack D <(E-Mail Removed)> said:

> On 2004-12-02 12:22:57 +0100, Francis Glassborow
> <(E-Mail Removed)> 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){

 
Reply With Quote
 
josh
Guest
Posts: n/a
 
      12-03-2004
Francis Glassborow wrote:
> In article <9Bzrd.179024$HA.22520@attbi_s01>, josh
> <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Doubt in -- Ruby --> Array --> array.display -- method jazzez ravi Ruby 1 02-28-2009 01:08 PM
Display byte array back to pdf and display in IE SG Java 6 01-15-2008 01:02 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM



Advertisments