| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"fb" |
| Date: |
01 Dec 2004 11:55:31 PM |
| Object: |
Array Display (Rhyme time) |
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
.
|
|
| User: "Andrew Koenig" |
|
| Title: Re: Array Display (Rhyme time) |
02 Dec 2004 12:10:08 AM |
|
|
"fb" <fb@goaway.net> 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?
.
|
|
|
|
| User: "josh" |
|
| Title: Re: Array Display (Rhyme time) |
02 Dec 2004 01:35:33 AM |
|
|
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
.
|
|
|
| User: "Francis Glassborow" |
|
| Title: Re: Array Display (Rhyme time) |
02 Dec 2004 05:22:57 AM |
|
|
In article <9Bzrd.179024$HA.22520@attbi_s01>, josh
<smileyfaceswillruletheworld@yahoo.com.NOSPAM> 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
.
|
|
|
| User: "josh" |
|
| Title: Re: Array Display (Rhyme time) |
03 Dec 2004 12:21:08 AM |
|
|
Francis Glassborow wrote:
In article <9Bzrd.179024$HA.22520@attbi_s01>, josh
<smileyfaceswillruletheworld@yahoo.com.NOSPAM> 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
.
|
|
|
|
| User: "Jack D" |
|
| Title: Re: Array Display (Rhyme time) |
02 Dec 2004 07:01:39 AM |
|
|
On 2004-12-02 12:22:57 +0100, Francis Glassborow
<francis@robinton.demon.co.uk> 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
.
|
|
|
| User: "Jack D" |
|
| Title: Re: Array Display (Rhyme time) |
02 Dec 2004 07:15:54 AM |
|
|
On 2004-12-02 14:01:39 +0100, Jack D <jack_221NOSP@Mhotmail.com> said:
On 2004-12-02 12:22:57 +0100, Francis Glassborow
<francis@robinton.demon.co.uk> 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){
.
|
|
|
|
|
|
|

|
Related Articles |
|
|