# Thread: Why aren't '1' of "col 1" and "1" aligned?

1. ## Why aren't '1' of "col 1" and "1" aligned?

Hi

I attached a screenshot from the output of the following code. Why aren't '1' of "col 1" and "1" (matrix entry) aligned? Could you please tell me the reason for this?

Code:
```// read 3x4 matrix. print it, find the largest value,
// count number of zeroes;

#include <iostream>
#include <cstdlib>
#include <iomanip>

using namespace std;

int main()
{

const int R = 3;
const int C = 4;
float m[R][C];
float largest = 0;
float smallest = 10000;
int num_zeros = 0;
int r, c;
int entered_largest_r = 0, entered_largest_c = 0;
int entered_smallest_r = 0, entered_smallest_c = 0;

cout << "enter the matrix below\n\n";

for ( r=0; r<R; r++ )
{
for ( c=0; c<C; c++ )
{
cout << "enter entry for row #" << (r+1)
<< " and column #" << (c+1) << ": ";
cin >> m[r][c];

if ( m[r][c] > largest)
{
largest = m[r][c];
entered_largest_r = r+1;
entered_largest_c = c+1;
}

if ( m[r][c] < smallest )
{
smallest = m[r][c];
entered_smallest_r = r+1;
entered_smallest_c = c+1;
}

if ( m[r][c] == 0)
{
num_zeros = num_zeros++;
}
}

}

cout << "\n\n";

cout << "entered matrix is given below\n\n";
cout << setw(10) << "" << setw(10) << "col 1" << setw(10) << "col 2"
<< setw(10) << "col 3" << setw(10) << "col 4\n\n";

for( r=0; r<R; r++ )
{

cout << "\n" << setw(10) << "row " << (r+1);

for( c=0; c<C; c++)
{
cout << setw(10) << m[r][c];

}

}

cout << "\n\n\n";

cout << "largest matric element is: " << largest << ", entered at row #" << entered_largest_r
<< " and column #" << entered_largest_c << "\n\n";
cout << "smallest matrix element is: " << smallest << ", entered at row #" << entered_smallest_r
<< " and column #" << entered_smallest_c << "\n\n";
cout << "number of '0' matrix element(s): " << num_zeros << endl << endl;

system("pause");
return 0;

}```

2. You should change this line:
Code:
`num_zeros = num_zeros++;`
to
Code:
`num_zeros++;`

3. Originally Posted by rags_to_riches
You should change this line:
Code:
`num_zeros = num_zeros++;`
to
Code:
`num_zeros++;`
Hi

Is my blue code line incorrect? It works perfectly fine as far as I can confirm. Please let me know. Thanks.

4. What is the blue line supposed to do?
And what is it currently doing?

5. Originally Posted by Elysia
What is the blue line supposed to do?
And what is it currently doing?
Hi Elysia

It is supposed to count number of entries in the matrix which are zero.

6. Why aren't '1' of "col 1" and "1" (matrix entry) aligned? Could you please tell me the reason for this?
setw() only applies for the next thing you print. For the rows, the width is set to 10 for "row ". The row number starts on the 11th spot.

Is my blue code line incorrect? It works perfectly fine as far as I can confirm.
The blue line is not correct because it is undefined behavior.

7. Originally Posted by Inanna
setw() only applies for the next thing you print. For the rows, the width is set to 10 for "row ". The row number starts on the 11th spot.

The blue line is not correct because it is undefined behavior.
Hi Inanna

But on my end it's working fine.

Apart from that I don't see any issue with it.

Code:
```int a = 0;
a = a++;```
What's the problem with it? "a" would be assigned a new value which is "1" in this case. Please let me know the reason. Thank you.

8. But on my end it's working fine.
Working fine is one possible effect of undefined behavior. That does not mean it will work anywhere else, or even on your end if you try again. Undefined behavior means 100% unpredictable.

What's the problem with it?
Did you read the link I gave and the related FAQs? The problem is explained there.

"a" would be assigned a new value which is "1" in this case.
Another obvious result is a being given the old value of 0. But if both options are just as good and just as obvious, which one does the compiler pick?

9. Thinking as a C++ programmer:
If i had a class Integer and the post-increment operator implemented, it would save the previous value of my Integer, increment its real value and then return the saved value. Therefore, for "i = 0", after "i = i++" "i" would be 0.

Thinking as a C programmer:
"a = b++" extends to "a = b; b = b + 1;", therefore fr "i = 0" after "i = i++" => "i = i; i = i + 1", "i" would be 1.

Both options are equally obvious, depending on how you look at it...

10. EDIT: Sorry for the extra post, my browser's gone crazy!

11. Originally Posted by Sipher
Thinking as a C++ programmer:
If i had a class Integer and the post-increment operator implemented, it would save the previous value of my Integer, increment its real value and then return the saved value. Therefore, for "i = 0", after "i = i++" "i" would be 0.

Thinking as a C programmer:
"a = b++" extends to "a = b; b = b + 1;", therefore fr "i = 0" after "i = i++" => "i = i; i = i + 1", "i" would be 1.

Both options are equally obvious, depending on how you look at it...
Integer classes are slightly different, because then i++ would be a function call, and which forces i to be changed before the function returns. The likely result is equivalent to i=i, as you point out, though a hypothetical compiler could end up doing i=i+1 after in-lining and optimization, as if it were just a regular integer.

But "a = b++" does not necessarily extend to "a = b; b = b + 1;". A compiler might equivalently extend that line to "c=b; b = c + 1; a = c;".

Bottom line, never modify the same memory location twice in the same line. What qualifies as a line in this context is explained here.