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

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    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;
    
    }
    Attached Images Attached Images Why aren't '1' of &quot;col 1&quot; and &quot;1&quot; aligned?-iomanip2-jpg 
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You should change this line:
    Code:
    num_zeros = num_zeros++;
    to
    Code:
    num_zeros++;

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by rags_to_riches View Post
    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.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What is the blue line supposed to do?
    And what is it currently doing?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Elysia View Post
    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.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  6. #6
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    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. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Inanna View Post
    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.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  8. #8
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    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. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    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...
    Devoted my life to programming...

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    EDIT: Sorry for the extra post, my browser's gone crazy!
    Devoted my life to programming...

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Sipher View Post
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  2. Replies: 46
    Last Post: 08-24-2007, 04:52 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM