numbers with boxes
-
I have this code and cannot seem to ge the numbers to print out to 20 within the boxes. Any help would be appreciated. Thanks Suppose to be this: +---+---+---+---+---+ | 1 | 2 | 3 | 4 | 5 | +---+---+---+---+---+ | 6 | 7 | 8 | 9 | 10| +---+---+---+---+---+ | 11| 12| 13| 14| 15| +---+---+---+---+---+ | 16| 17| 18| 19| 20| +---+---+---+---+---+ Here is what I get: How many rows of mailboxes will there be?4 How many columns of mailboxes will there be?5 The numbering layout will be: +---+---+---+---+---+ | 1 | 2 | 3 | 4 | 5 | Overall height of mailbox unit: 24 Overall width of mailbox unit: 20
<#define mbxhgt 6
#define mbxwid 4int main (void)
{
int rows,
cols,
r = 1,
c = 1,
boxnum = 1,
unit_height,
unit_width;system("cls");
printf ("Mailbox Layout Program\n\n");
printf ("How many rows of mailboxes will there be?");
scanf ("%d", &rows);printf ("\nHow many columns of mailboxes will there be?");
scanf ("%d", &cols);printf ("\n\nThe numbering layout will be:\n");
printf("+---+---+---+---+---+\n|");while (c <= cols)
{
c = c + 1;
printf (" %d |", boxnum);
boxnum = boxnum + 1;
c = c++;
}while (r <= rows)
{
r = r + 1;
printf ("\n \n");
boxnum = boxnum + 1;
r = r++;
}unit_height = rows * mbxhgt;
unit_width = cols * mbxwid;printf ("\nOverall height of mailbox unit: %d", unit_height);
printf ("\nOverall width of mailbox unit: %d\n", unit_width);
return (0);
/pre>See here. The two
while()
loops are separate. One needs to be nested inside the other, like:int number = 1;
printf("+---+---+---+---+---+\n");
for (int row = 0; ...)
{
for (int col = 0; ...)
{
printf("| %2d", number);
number++:
}printf("|\\n+---+---+---+---+---+\\n");
}
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
See here. The two
while()
loops are separate. One needs to be nested inside the other, like:int number = 1;
printf("+---+---+---+---+---+\n");
for (int row = 0; ...)
{
for (int col = 0; ...)
{
printf("| %2d", number);
number++:
}printf("|\\n+---+---+---+---+---+\\n");
}
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
kbury wrote:
I tried that...
Tried what?
kbury wrote:
...but got error messages.
Which ones?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
kbury wrote:
I tried that...
Tried what?
kbury wrote:
...but got error messages.
Which ones?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
nesting, but maybe i did it wrong, because it came out with this Mailbox Layout Program How many rows of mailboxes will there be?4 How many columns of mailboxes will there be?5 The numbering layout will be: +---+---+---+---+---+ | 1 | Overall height of mailbox unit: 24 Overall width of mailbox unit: 20
-
nesting, but maybe i did it wrong, because it came out with this Mailbox Layout Program How many rows of mailboxes will there be?4 How many columns of mailboxes will there be?5 The numbering layout will be: +---+---+---+---+---+ | 1 | Overall height of mailbox unit: 24 Overall width of mailbox unit: 20
Your
while()
loops are backwards. Think about drawing this figure on paper. You start with the first column of the first row, then the second column of the first row, etc. After the last column, you go to the next row (i.e., first column of the second row). You've not changed the innerprintf()
statement to use a width specifier. Did you not read the link I offered? It should look like:printf("| %2d", number);
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Your
while()
loops are backwards. Think about drawing this figure on paper. You start with the first column of the first row, then the second column of the first row, etc. After the last column, you go to the next row (i.e., first column of the second row). You've not changed the innerprintf()
statement to use a width specifier. Did you not read the link I offered? It should look like:printf("| %2d", number);
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
I just cannot seem to get my head wrapped around this for some reason. here is what I changed.
{
for (int r = 0;r <= rows; r++);{
r = r + 1;
printf (" \n");
boxnum = boxnum + 1;
r=r++;
for (int c=0; c <= cols; c++);
{c = c + 1;
printf (" %2d|", boxnum);
boxnum = boxnum + 1;
c = c++;}
unit_height = rows * mbxhgt;
unit_width = cols * mbxwid;printf ("\nOverall height of mailbox unit: %d", unit_height);
printf ("\nOverall width of mailbox unit: %d\n", unit_width);
return (0);
}}}
-
I just cannot seem to get my head wrapped around this for some reason. here is what I changed.
{
for (int r = 0;r <= rows; r++);{
r = r + 1;
printf (" \n");
boxnum = boxnum + 1;
r=r++;
for (int c=0; c <= cols; c++);
{c = c + 1;
printf (" %2d|", boxnum);
boxnum = boxnum + 1;
c = c++;}
unit_height = rows * mbxhgt;
unit_width = cols * mbxwid;printf ("\nOverall height of mailbox unit: %d", unit_height);
printf ("\nOverall width of mailbox unit: %d\n", unit_width);
return (0);
}}}
kbury wrote:
for (int r = 0;r <= rows; r++); { r = r + 1; ... r=r++;
How many times does
r
(andc
) need to be incremented? Also, that rogue semicolon is sure to cause you grief. There's a problem withboxnum
, but it does not affect the box-drawing. I'll withold the answer until you've studied it more."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
kbury wrote:
for (int r = 0;r <= rows; r++); { r = r + 1; ... r=r++;
How many times does
r
(andc
) need to be incremented? Also, that rogue semicolon is sure to cause you grief. There's a problem withboxnum
, but it does not affect the box-drawing. I'll withold the answer until you've studied it more."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
changed code to this
<#include <stdio.h> /*Include header*/
#include <stdlib.h>
#include <assert.h>#define mbxhgt 6
#define mbxwid 4int main (void)
{
int rows,
cols,
r = 1,
c = 1,
boxnum = 1,
unit_height,
unit_width;system("cls");
printf ("Mailbox Layout Program\n\n");
printf ("How many rows of mailboxes will there be?");
scanf ("%d", &rows);printf ("\nHow many columns of mailboxes will there be?");
scanf ("%d", &cols);printf ("\n\nThe numbering layout will be:\n");
printf("+---+---+---+---+---+\n|");while (c <= cols)
{c = c + 1; printf (" %d |", boxnum); boxnum = boxnum + 1; }
while (r <= rows)
{r = r + 1;printf ("\\n \\n");
if (boxnum <= 1);
printf("| %d | ", boxnum++);}unit_height = rows * mbxhgt;
unit_width = cols * mbxwid;printf ("\nOverall height of mailbox unit: %d", unit_height);
printf ("\nOverall width of mailbox unit: %d\n", unit_width);return (0);
}/pre>now looks like this:
How many rows of mailboxes will there be?4
How many columns of mailboxes will there be?5
The numbering layout will be:
+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 || 6 |
| 7 |
| 8 |
| 9 |
Overall height of mailbox unit: 24
Overall width of mailbox unit: 20 -
changed code to this
<#include <stdio.h> /*Include header*/
#include <stdlib.h>
#include <assert.h>#define mbxhgt 6
#define mbxwid 4int main (void)
{
int rows,
cols,
r = 1,
c = 1,
boxnum = 1,
unit_height,
unit_width;system("cls");
printf ("Mailbox Layout Program\n\n");
printf ("How many rows of mailboxes will there be?");
scanf ("%d", &rows);printf ("\nHow many columns of mailboxes will there be?");
scanf ("%d", &cols);printf ("\n\nThe numbering layout will be:\n");
printf("+---+---+---+---+---+\n|");while (c <= cols)
{c = c + 1; printf (" %d |", boxnum); boxnum = boxnum + 1; }
while (r <= rows)
{r = r + 1;printf ("\\n \\n");
if (boxnum <= 1);
printf("| %d | ", boxnum++);}unit_height = rows * mbxhgt;
unit_width = cols * mbxwid;printf ("\nOverall height of mailbox unit: %d", unit_height);
printf ("\nOverall width of mailbox unit: %d\n", unit_width);return (0);
}/pre>now looks like this:
How many rows of mailboxes will there be?4
How many columns of mailboxes will there be?5
The numbering layout will be:
+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 || 6 |
| 7 |
| 8 |
| 9 |
Overall height of mailbox unit: 24
Overall width of mailbox unit: 20You are just glutton for punishment, aren't you? As I've said, and demonstrated, you cannot have two separate loops.
kbury wrote:
if (boxnum <= 1);
Other than being unnecessary, do you notice anything wrong with this? My compiler warns me with a C4390 warning.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
You are just glutton for punishment, aren't you? As I've said, and demonstrated, you cannot have two separate loops.
kbury wrote:
if (boxnum <= 1);
Other than being unnecessary, do you notice anything wrong with this? My compiler warns me with a C4390 warning.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
I guess a gluton for punishment. I am new at this and am trying to learn from a online class, which seems to make this more difficult than I had expected.
What's difficult about comparing your code to mine? :confused:
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
You are just glutton for punishment, aren't you? As I've said, and demonstrated, you cannot have two separate loops.
kbury wrote:
if (boxnum <= 1);
Other than being unnecessary, do you notice anything wrong with this? My compiler warns me with a C4390 warning.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
I am using the LCC Win 32 compiler and when I remove the semicolon away from if (boxnum <= 1); It does not print out the 6, 7, 8, 9 at all.
anyways I changed the code and now it does this. Do i need to change the if boxnum statement?
printf ("\n\nThe numbering layout will be:\n");
while (r <= rows)
{ r = r + 1; printf ("\\n \\n");
while (c <= cols)
c = c + 1; printf (" %d |", boxnum); boxnum = boxnum + 1; if (boxnum <= 1); printf(" %d | ", boxnum++); } unit\_height = rows \* mbxhgt; unit\_width = cols \* mbxwid; printf ("\\nOverall height of mailbox unit: %d", unit\_height); printf ("\\nOverall width of mailbox unit: %d\\n", unit\_width);
return (0);
}Mailbox Layout Program How many rows of mailboxes will there be?4 How many columns of mailboxes will there be?5 The numbering layout will be: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Overall height of mailbox unit: 24 Overall width of mailbox unit: 20
-
anyways I changed the code and now it does this. Do i need to change the if boxnum statement?
printf ("\n\nThe numbering layout will be:\n");
while (r <= rows)
{ r = r + 1; printf ("\\n \\n");
while (c <= cols)
c = c + 1; printf (" %d |", boxnum); boxnum = boxnum + 1; if (boxnum <= 1); printf(" %d | ", boxnum++); } unit\_height = rows \* mbxhgt; unit\_width = cols \* mbxwid; printf ("\\nOverall height of mailbox unit: %d", unit\_height); printf ("\\nOverall width of mailbox unit: %d\\n", unit\_width);
return (0);
}Mailbox Layout Program How many rows of mailboxes will there be?4 How many columns of mailboxes will there be?5 The numbering layout will be: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Overall height of mailbox unit: 24 Overall width of mailbox unit: 20
kbury wrote:
while (c <= cols)
For all but the first row, when will
c
ever be LEcols
again? Furthermore, without braces for the innerwhile()
loop, what statements do you expect will be executed?"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
kbury wrote:
while (c <= cols)
For all but the first row, when will
c
ever be LEcols
again? Furthermore, without braces for the innerwhile()
loop, what statements do you expect will be executed?"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
I am trying to stick to this flowchart given to me. Maybe I just do not understand it. In the flowchart it is only called where I have it in the code and after boxnum it is suppose to go back to col.
Counting this one, I've posted 8 times to this thread, which was 7 times too many. I showed you the answer in my first post. You continue to use your code, which I do not have a problem with, but you still refuse to make the necessary changes or even compare the two to see why they behave differently. I'm confused as to why a person would do this.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Counting this one, I've posted 8 times to this thread, which was 7 times too many. I showed you the answer in my first post. You continue to use your code, which I do not have a problem with, but you still refuse to make the necessary changes or even compare the two to see why they behave differently. I'm confused as to why a person would do this.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Thanks for your help. I tried to code it just like you had in the 1st answer, but my compiler would not compile it.
kbury wrote:
I tried to code it just like you had in the 1st answer...
Using copy & paste?
kbury wrote:
...but my compiler would not compile it.
It was standard C code. What error(s) did you receive?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons