trace

Oct 27, 2014 at 8:30pm
Can someone help me understand how we got the result with this code?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;

void main()
{
	int a[4] = { 10, 20, 30, 40 };
	int i = 0;
	int j = 4;
	while (i++ < 4)
	{
		j = i;
		cout << a[i] << endl;
		while (j-->0)
			cout << i << " ";
	}


     system("pause");
}
Oct 27, 2014 at 9:24pm
since you increment i in the while( i++ < 4 ) line, you never output element 0.
Oct 27, 2014 at 9:53pm
I get that part. i dont get line 16 and 17.
Oct 27, 2014 at 10:23pm
What is the original problem? What is the objective of the program?
Oct 27, 2014 at 10:25pm
no problem and no objective.
im just trying to find out how we get the numbers that the program outputs.
Oct 28, 2014 at 12:03am
in line 16: j-- is the postfix decrement operator. which means it won't be decremented until after the comparison is made. try it with the prefix decrement while( --j > 0 ) and see if that's what you were expecting.
Topic archived. No new replies allowed.