way to check 3 elements of array and set 4th

This is a discussion on way to check 3 elements of array and set 4th within the C++ Programming forums, part of the General Programming Boards category; I'm trying to think of how I can check 3 elements of a 4 element array and set the 4th. ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    203

    way to check 3 elements of array and set 4th

    I'm trying to think of how I can check 3 elements of a 4 element array and set the 4th. I have a bool array of 4 elements that all start off false. If any 3 of the elements are true, the other element needs to be set to true also. If only 1 or 2 elements are true, nothing is changed.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    That would be rather simple, methinks. What code have you written so far in your attempt to solve your problem?

    EDIT:
    If any 3 of the elements are true, the other element needs to be set to true also. If only 1 or 2 elements are true, nothing is changed.
    Surely you mean "if all 3 of the other elements are true", otherwise you have conflicting requirements, or?
    Last edited by laserlight; 01-09-2006 at 09:57 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
       bool arr[4];
       int nrs = 0, ns;
       for ( int idx = 0; idx < 4; idx ++ ) {
           if ( arr[idx] ) nrs ++;
           else ns = idx;
       }
       if ( nrs == 3 ) arr[ns]=true;
    Don't know if that is what you want.
    this checks if any 3 of the 4 are set if yes sets the 4th as well
    Kurt

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    Thanks Zuk, that looks like exactly what i was wanting. I was over thinking the simplicity of it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vertical Scroller laser cannon problem
    By Swarvy in forum Game Programming
    Replies: 5
    Last Post: 05-02-2009, 06:30 PM
  2. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  3. C help for network animator
    By fastshadow in forum Tech Board
    Replies: 7
    Last Post: 03-17-2006, 02:44 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21