Array Display (Rhyme time)

F

fb

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
 
A

Andrew Koenig

fb said:
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?
 
J

josh

fb said:
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
 
F

Francis Glassborow

josh said:
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'
 
J

Jack D

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
 
J

Jack D

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){
 
J

josh

Francis said:
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...)

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top