|
Thankyou sir but pls explain how I got the first row output especially the -1 in the middle. I need the same array dimension because its given that in a text book.
|
|
|
|
|
You exceeded the bounds of the array definition, what data you fetch or corrupt when you do that is up to the "memory alignment gods". Fix the program as suggested so that you do not exceed the array boundaries and you will stop getting wierd output.
|
|
|
|
|
Thank you sir,
How to fix the problem where I need to make changes?
|
|
|
|
|
David Crow gave you the answer, have you tried it.
|
|
|
|
|
Thank you sir,
You told me to fix the size. How to change because its been initialized. Mr crow told to increment later in the loop. But its should be there in the while part itself. If so how to rectify by changing the array size.
|
|
|
|
|
venkatasu wrote: You told me to fix the size.
Nothing was mentioned about changing the size of the array. Arrays are 0 -based.
venkatasu wrote: Mr crow told to increment later in the loop. But its should be there in the while part itself.
I did not say to move the increment statement outside of the while() loop. What part of
while (i < 5)
{
cout << i << endl;
result[i] = x[i] + y[i];
i++;
} troubles you?
"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
|
|
|
|
|
Thank you sir, I understood you told me to increment inside the loop. But they want me to retain the i++ at the begining of the while loop if so what to do.
|
|
|
|
|
venkatasu wrote: But they want me to retain the i++ at the begining of the while loop...
I will refrain from saying (to "they") what is really on my mind. If you insist on keeping it, put a pillow on your desk else your forehead will soon hurt.
Why is it okay for the second loop to increment its control variable correctly?
"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
|
|
|
|
|
As long as you insist that you don't need any code modifications, you will get nowhere with changing the array dimensions.
You are computing and filling in array entries 1 through 5
You are printing out array entries 0 through 4
That is *bad code*, changing array dimensions will not affect this basic flaw.
David Crow has given you two excellent hints (nobody is going to write the program for you). I gave you hints on the fundamental problem. Now it is your turn to demonstrate that you have read, understood, and found the problem yourself. David and I have already passed this course.
I'm done now.
|
|
|
|
|
Oh I am sorry if I have troubled you as I am a newbie please excuse me and guide me as I am confused and newbie.
Thanks a lot sir
|
|
|
|
|
It's one thing to be new and willing to change/explore, it's altogether different to be new and stubborn. By insisting on the latter, you're not going to advance very far.
"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
|
|
|
|
|
venkatasu wrote: while (i++ < 5)
Increment i at the bottom of the while() loop.
"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
|
|
|
|
|
Sir
I need the same program. Its given in a text book. I want to know how I got the -1 in middle of the first row.
|
|
|
|
|
venkatasu wrote: Its given in a text book. I want to know how I got the -1 in middle of the first row.
I've given you the answer. Implement it and you're code will work as expected.
"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
|
|
|
|
|
Thank you sir,
But i don't need any code modifications. It should be the same i++ there in the while itself. I want to know how I got -1 in the first row of the output
|
|
|
|
|
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.
|
|
|
|