CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2006
    Posts
    1

    Count elements within bounds in 2D array

    Hi,

    I'm trying to count the number of elements that contain numbers that lie within two boundry conditions in a 2D array.

    So, if I have my data:

    i j
    10 100
    20 200
    30 300
    40 400

    I want to know how many rows (20<=i<=40 && 100<=j<=300). In this case 2. The arrays are also many lines long.

    I thought that this would be really easy to do in a language that seems as powerfula nd flexible as C++ but......

    Any ideas. Cheers.
    Will

  2. #2
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: Count elements within bounds in 2D array

    And what's the problem doing what you said?
    Code:
    //for each 2D number
    if(20<=i<=40 && 100<=j<=300)
    	//value is within
    else
    	//it's not
    I think I don't undestand your problem!! Sorry!

    Albert.

  3. #3
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Count elements within bounds in 2D array

    Quote Originally Posted by AlbertGM
    And what's the problem doing what you said?
    Code:
    //for each 2D number
    if(20<=i<=40 && 100<=j<=300)
    	//value is within
    else
    	//it's not
    I think I don't undestand your problem!! Sorry!

    Albert.
    20<=i<=40 yields true whatever i is. It is interpreted as (20<=i)<=40
    Same thing for 100<=j<=300

    The correct condition is:
    Code:
    20<=i && i<=40 && 100<=j && j<=300
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  4. #4
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: Count elements within bounds in 2D array

    SuperKoko, you are always correcting me. I think I won't do any any other post, because all of them have errors :-)

    Thanks for your answers and for correct me. It could be because today I've lunch with wine :-)

    Albert.

  5. #5
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Count elements within bounds in 2D array

    Quote Originally Posted by AlbertGM
    SuperKoko, you are always correcting me. I think I won't do any any other post, because all of them have errors :-)
    Please, continue posting.

    Doing errors in untested code is very usual. Personally, I can't write much code without any problem if I don't try to compile it.

    With time, you'll do fewer errors on untested code.


    Quote Originally Posted by AlbertGM

    Thanks for your answers and for correct me. It could be because today I've lunch with wine :-)
    It may be a factor.
    Personally, I do many more errors when I'm sleepy.

    PS: I tried to send this message as a PM but AlbertGM blocked PMs in his account.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  6. #6
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: Count elements within bounds in 2D array

    Quote Originally Posted by SuperKoko
    Please, continue posting.
    Of course I'll do. I'm learning a lot :-)
    Quote Originally Posted by SuperKoko
    PS: I tried to send this message as a PM but AlbertGM blocked PMs in his account.
    What's that? How I blocked that? How Can I unlock it?

    Albert.

  7. #7
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Count elements within bounds in 2D array

    Quote Originally Posted by AlbertGM
    How I blocked that?
    I guess it is the default behavior.
    Quote Originally Posted by AlbertGM
    How Can I unlock it?
    Go to the User CP page, click the User Options link, and then, activate "Enable Private Messaging", and finally, click "Save Changes".

    This link should go to your user options page:
    http://www.codeguru.com/forum/profil...do=editoptions
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured