#include<iostream.h>
#include<conio.h>
void main()
{
int x[5]={1,2,3,4,5}, y[5]={5,4,3,2,1}, result[5]={0,0,0,0,0};
int i=0;
while(i++<5)
result[i]=x[i]-y[i];
clrscr();
cout<<"\n The contents of the aray are:\n";
i=0;
do
{
cout<<"\t"<<x[i]<<"\t"<<y[i]<<"\t"<<result[i]<<"\n";
i++;
}
while(i<5);
getch();
}
The above program is executed the following output are displayed. How this executed plz explain.
1 -1 0 --> Explain this step only
The contents of the array are:
1 -1 0
2 4 -2
3 3 0
4 2 2
5 1 4
why the compiler the assign the value in first line of output?
On the last iteration of the first loop ( while(i++<5) ), this program writes into memory past the end of the array result[], invoking undefined behavior.
(it also appears to be written in some 20+ year old non-standard dialect of C++: in standard C++, iostream.h does not exist and main must return int)
I don't think you do understand the concept. People have given you numerous hints as to what the problem is but you don't seem to be taking any notice.
Perhaps add this to your loops.
1 2 3 4 5
while(i++<5)
{
std::cout << "i is " << i << "\n"; // Add this
result[i]=x[i]-y[i];
}
. . .
1 2 3 4 5 6
do
{
std::cout << "i is " << i << "\n"; // Add this
cout<<"\t"<<x[i]<<"\t"<<y[i]<<"\t"<<result[i]<<"\n";
i++;
} while(i<5);
Take the time to look at what you're doing and you'll see the problem.
If i remember correctly i++ will use the value of i and then increment, whereas ++i will increment the value and then use it.
So with i++, the order would go
loop tests i = 0, edits i = 1
loop tests i = 1, edits i = 2
loop tests i = 2, edits i = 3
loop tests i = 3, edits i = 4
loop tests i = 4, edits i = 5
loop tests i = 5, edits none
whereas with ++i, the order would go
loop tests i = 1, edits i = 1
loop tests i = 2, edits i = 2
loop tests i = 3, edits i = 3
loop tests i = 4, edits i = 4
loop tests i = 5, edits none
doing it as ++i prevents your code from doing what @Cubbi talked about.
but both cause your program to misbehave; it would be better to change your while loops to a single for loop as @ajh32 suggests, or move the i++ to its own line at the end of the loop.
If i remember correctly i++ will use the value of i and then increment, whereas ++i will increment the value and then use it.
Whether you use the post or pre increment by the time the code reaches the next line the variable will have been incremented anyway.
1 2 3
int n(10);
int a = n++;
int b = ++n;
Using both pre- and post increment operator as in above code, you will find that a holds the value 10 and n now holds the value of 11. But the pre-increment operator as in:
int b = ++n;
will result in b holding the value of 12 as well as n holding the value of 12. Because the operator is used on the variable before its value is then assigned.
I know the concept of preincrement and post increment while looping. But I want to know the why the compiler is assigned the value of first line output.
1 -1 0 --> if while(i++<5) and
1 5 0 --> if while(++i<5) .
I have already modify the program. increment segment is move to body the loop, so output is get properly.
but I have little confuse the particular line. Explain this line only.
It is because of the way the pre/post increments were causing your loop to modify the data. With the post-increment, you were writing past the end of the memory allocated because it did not exit the loop as planned, which was causing undefined behavior; as @Cubbi said in their post, 3rd one from top.
With the pre-increment, it exits the loop as expected, but since it had already incremented the value of i, it never found the result of (1-5) thus did not store it in the result array; so it prints what is already there, 0.
this program writes into memory past the end of the array result[], invoking undefined behavior.
- Cubbi
Unless someone else believes otherwise; this is the best answer as to why it prints the -1. The code was simply acting up, I have no better explanation than what I and others have already given.