Notices
Results 1 to 6 of 6

Thread: An if statement that checks all the elements of an array

  1. #1 An if statement that checks all the elements of an array 
    Forum Freshman
    Join Date
    Jun 2009
    Posts
    53
    I have been trying to find a general method to typing the following code.

    if ( colb[1][mb] == '*' && colb[2][mb] == '*' && colb[3][mb] == '*' && colb[x][mb] == '*' )

    This is ok but by about x = 10 its just too painful.
    I have thought of a solution whereby you make a for loop outside that basically breaks if (and return 0 to some int- check_colb) any of the cob[n][mb] != '*'
    and the if loop can just check if check_colb is 1 or 0.

    However I am wonder is there a specific syntax or method that is typical in such situations? (I am thinking there must be...)

    This is the first time I am in the computer science forum, nice to meet you all.


    Reply With Quote  
     

  2.  
     

  3. #2  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    Some languages have an "one of" or "all" type command, where you supply a test function and it returns whether that applies to one of, or all, the elements of a collection. Not all languages support this though, but I think Java and C++ both have something like this in their standard libraries (at least, with the new C++ standard).

    BTW, your loop is basically the standard way of doing it, and basically what any library call would end up doing anyway.


    Reply With Quote  
     

  4. #3  
    Forum Freshman
    Join Date
    Jun 2009
    Posts
    53
    Sorry, I didn't specify the language....

    Does C have this function by any chance? I guess I can easily make it.
    Reply With Quote  
     

  5. #4  
    Forum Freshman
    Join Date
    May 2011
    Posts
    5
    Quote Originally Posted by tikai
    Sorry, I didn't specify the language....

    Does C have this function by any chance? I guess I can easily make it.
    Well, as far as I know there aren't any. But never mind those, the general method is small and efficient enough so you won't need any libraries. But, if you still want some then I suggest you make a library yourself and add all the functions that you commonly use. In fact, that's what all big programmers do.
    Reply With Quote  
     

  6. #5  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    The STL is one of several reasons I prefer C++ over C.

    As far as I know it would be impossible to do this in a generic way in C, since each type would need its own function, so I wouldn't expect to find this in any of the standard headers.
    Reply With Quote  
     

  7. #6  
    Forum Freshman
    Join Date
    Oct 2010
    Posts
    98
    Code:
    {
        int result = 1;
        int i;
    
        ...
    
        for ( i = 1; i <= x && result; i++ )
        {
            result &&= colb[i][mb] == '*';
        }
        
        if ( result )
        {
            /* whatever you want */
        }
    }
    This also imitates laziness of &&.

    If you are coding in C you may also use goto:

    Code:
        for ( i = 1; i <= x; i++ )
        {
            if ( colb[i][mb] != '*' )
            {
                goto avoid;
            }
        }
    
        /* if true */
    
        avoid:
    C is a pretty old programming language. It was created in early seventies. So it has no modern syntax constructs.
    Reply With Quote  
     

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