how this output displayed??

#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?

1 -1 0
Well you compile the program and run it. I don't get y[0] == -1 I get 5, as per the first element in the following array assigned to y:

{5,4,3,2,1}
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)
You would have been better writing it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio.h>

using namespace std;

 int main()
 {
     const int ARRAY_SIZE = 5;

     int x[]={1,2,3,4,5},
         y[]={5,4,3,2,1},
    result[]={0,0,0,0,0};

     for (int i=0; i<ARRAY_SIZE; ++i)
     {
         if (i==0)
            cout<<"\n The contents of the aray are:\n";
         result[i]=x[i]-y[i];
         cout<<"\t"<<x[i]<<"\t"<<y[i]<<"\t"<<result[i]<<"\n";
     }

     getch();
     return 0;
 }
Last edited on
No frnd

I want to know same program

Explain this output step only

1 -1 0

why this compiler assigned value of first line...
It doesn't it displays:

1 5 0

Because you've incremented the variable i to 1 and hence x[0] and y[0] aren't used. Bad logic as pointed out.
Ok

but when change the increment section in while loop that is

while(++i<5)


the output is

1 5 0


how is this executed??

The following line:

while(i++<5)

increments i from 0 to 1

then the next line:

result[i]=x[i]-y[i];

is:

result[1] = x[1] - y[1] i.e.: 2 - 4

The array values at index 0 aren't used or changed. See initial arrays
I know this concept of while loop how did worked. But what i am asking


While(i++<5) this line is display the result is

1 -1 0


but while (++i<5) this line is display the result is

1 5 0


I am asking that particular line only.. remaining concepts are i know..
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.
Last edited on
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.
Last edited on
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.

HTH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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];
            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();
}
In c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<conio.h>
using namespace std;
int 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];
            i++;
        }
    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();
    return 0;
}
Thanz all

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.
BranL

why should compiler assign the this value

1 -1 0 explain this step only...

remaining concepts are i know very well.
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.
Topic archived. No new replies allowed.