473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Array Display (Rhyme time)

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

Jul 22 '05 #1
6 1343
"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?
Jul 22 '05 #2
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

Jul 22 '05 #3
In article <9Bzrd.179024$HA.22520@attbi_s01>, josh
<sm*************************@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
Jul 22 '05 #4
On 2004-12-02 12:22:57 +0100, Francis Glassborow
<fr*****@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

Jul 22 '05 #5
On 2004-12-02 14:01:39 +0100, Jack D <ja**********@Mhotmail.com> said:
On 2004-12-02 12:22:57 +0100, Francis Glassborow
<fr*****@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){

Jul 22 '05 #6
Francis Glassborow wrote:
In article <9Bzrd.179024$HA.22520@attbi_s01>, josh
<sm*************************@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

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Roy J | last post by:
Hello everyone:) My name is Roy, I am new to Java and I have problem regarding to arrays. if you have time and like to help, please help. I am trying to make a program to deal with prime...
9
by: Ken | last post by:
I have a small program , wi tha menu (only option 1,2 and 6 are working for now) Something weird is happening, my array works to input all the data the user puts in, but whebn I choose option 2,...
1
by: aemazing | last post by:
i've been tryin to do the following - -Add a new flight number to the end of the queue (got it done) -LAnd the plane at the front of the queue - problems wit it- -display the queue - got it done...
1
by: Chris | last post by:
How do I stop displaying "undefined" when using the code below. The problem occurs as I want to display 5 items per page using setInterval but this causes 'mycars' to reach a value that doesn't...
3
by: kohvirus | last post by:
Hello everyone. I'm currently trying to write a program with an array but for the life of me I can't seem to get the right syntax for the array. Here is the program as follows: #include...
6
by: J | last post by:
Kind of new at programming/vb.net. I'm doing this junky die roller program. Heres's what is supposed to happen: Roll 2 6-sided dies. Add rolls together put total in rolls(d6total). Display...
8
by: Sam | last post by:
I have a situation occuring in my code and I just can't see to figure out why I have an structure called employee that will put all of the employee id's into a char array set to 10 struct...
4
by: sjohnson1984 | last post by:
Hello all, I have a form which is generated using a database query - the recordset is filled with agent details, login time and the like, and there are as many rows in the table as records in the...
2
Claus Mygind
by: Claus Mygind | last post by:
I have an array of objects (a hash table). My section of code, the "for in" loop (lines 18 to 41) does not execute at full speed. But if I put in an alert box (line 13) to stop program execution...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.