c++ output explanation

Jan 23, 2018 at 5:44pm
#include<iostream>
using namespace std;
int main()
{
int n,k;
int j = 8;
for (int i = 0; i < 3; ++i)
{
switch (i)
{
case 0 :
k = 2;
n = (( ( k )%( j-- ) ) == 1 ) ? j/k : k++;
break;
case 1 :
j--;
k += 3;
n = ( k%j ) ? ( ++k ) : n+2;
case 2 :
k = ( k == n || k == j ) ? ( n-k ) : n+k;
n *= ( i+1 )-k/2;
break;
}
cout << "n = " << n << " k = " << k << endl;
}
}


outputs
n = 2 k = 3
n = 12 k = -2
n = -24 k = 10

can anyone please explain how these output come
Jan 23, 2018 at 6:44pm
this is intentionally gibberish code of no value to examine the hard way.
just do it the lazy way instead:
add a print statement every time n is modified, and another every time k is modified. You can then watch it change values until the final result is computed.

If your IDE supports step-by-step debugging with watches, that is the same thing as adding print statements.
Last edited on Jan 23, 2018 at 6:44pm
Jan 23, 2018 at 7:03pm
but i think my IDE dont supports setp-by-step debugging
Jan 23, 2018 at 8:41pm
so add the print statements.

int main()
{
int n,k;
int j = 8;
for (int i = 0; i < 3; ++i)
{
cout <<" 01 n is: " <<n << endl;
switch (i)
{
case 0 :
k = 2;
cout <<" 02 k is: " << k << endl;
n = (( ( k )%( j-- ) ) == 1 ) ? j/k : k++;
cout <<" 03 n is: " << n << endl;
cout <<" 04 k is: " << k << endl;
...

dos and unix both support file output redirection.
run the program as programname > filename and you can see all the output at once, and search it, or if you want add a , after the print identification numbers and open as csv in excel so you can manipulate it or filter it etc.
Last edited on Jan 23, 2018 at 8:43pm
Jan 23, 2018 at 8:57pm
thank bro for the help
Topic archived. No new replies allowed.