|
venkatasu wrote: But i don't need any code modifications.
On the contrary, you need to increment i later in the loop. The debugger you are using should have showed you this.
When you exceed an array's boundaries, it messes stuff up elsewhere. Consider your code:
int x[5] = { 1, 2, 3, 4, 5 },
y[5] = { 5, 4, 3, 2, 1 },
result[5] = { 0, 0, 0, 0, 0 }; These are laid out in contiguous memory such that x comes after y comes after result . When you assigned a value to result[5] , which is out of bounds, you actually wrote to y[0] .
venkatasu wrote: I want to know how I got -1 in the first row of the output
By incrementing i at the wrong spot.
"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
|
|
|
|
|
|
How's the forehead? 
|
|
|
|
|
You think!
"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 got that answer because your program is logically flawed because of the way you increment the counter i in the first loop as you've been repeatedly told. Unless you change that, you'll never get the right answer. Most people use for loops in instances like yours.
You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.
|
|
|
|
|
The program in the book is broken. If your instructor wants you to use that exact program, unchanged, then perhaps it's a test of your debugging skills.
Use a debugger and single step through your program, watch for when the -1 shows up.
|
|
|
|
|
Your program should output as:
1 5 0
2 4 6
3 3 6
4 2 6
5 1 6
if you did what DavidCrow said, the outout should be:
1 5 6
2 4 6
3 3 6
4 2 6
5 1 6
|
|
|
|
|
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 4
int 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
|
|
|
|
|
I do not understand. I tried that but got error messages.
|
|
|
|
|
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
<printf ("mailbox="" layout="" program\n\n");
printf="" ("how="" many="" rows="" of="" mailboxes="" will="" there="" be?");
scanf="" ("%d",="" &rows);
printf="" ("\nhow="" columns="" &cols);
printf="" ("\n\nthe="" numbering="" be:\n");
printf("+---+---+---+---+---+\n|");
{
while="" (c="" <="cols)
{
c" =="" c="" +="" 1;
printf="" ("="" %d="" |",="" boxnum);
boxnum="boxnum" 1;
c="c++;
{
" while="" (r="" r="" ("\n="" \n");
boxnum="boxnum" 1;
r="r++;
}
unit_height" *="" mbxhgt;
unit_width="cols" mbxwid;
printf="" ("\noverall="" height="" mailbox="" unit:="" %d",="" unit_height);
="" pre="">
|
|
|
|
|
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 inner printf() 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);
}
}}
|
|
|
|
|
kbury wrote: for (int r = 0;r <= rows; r++);
{
r = r + 1;
...
r=r++;
How many times does r (and c ) need to be incremented? Also, that rogue semicolon is sure to cause you grief.
There's a problem with boxnum , 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 <stdlib.h>
#include <assert.h>
#define mbxhgt 6
#define mbxwid 4
int 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
|
|
|
|
|
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?
"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
|
|
|
|
|
kbury wrote: while (c <= cols)
For all but the first row, when will c ever be LE cols again? Furthermore, without braces for the inner while() 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
|
|
|
|
|
Thanks for your help. I tried to code it just like you had in the 1st answer, but my compiler would not compile it.
|
|
|
|