Why is it adding enemies

The following code outputs 1. Shouldn't it output zero since knights[7] is 7. I'm new and probably missing something super obvious.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main() {
	vector<int>knights(8);
	
	for (int i = 0; i < 8; i++)
	{
		knights[i] = i;
	}
int enemies = 0;
if (knights[7] != 3 or knights[7] != 7 or knights[7] != 5 or knights[7] != 4)
			{
				enemies++;
			}
			cout << enemies << endl;
return 0;
}
7 doesn't equal 3, for instance.
You have to change the ors to ands. Say the sentence in your mind.
"If the seventh knight is not 3 or it is not 7 or it is not 5 or it is not 4, then increment enemies." vs. "If the seventh knight is not 3 and it is not 7 and it is not 5 and it is not 4, then increment enemies."
Of course, super obvious. Thanks a lot.
👍 :)
Only thing i can say is if you're still working on the king Arthur problem and this is as far as you've got you're in for a hell of a battle.

The best advice I can give you is save the friend list in a 2d array where the first element represents the knight whose friends are in the list. Like int lan =0; so element list[0][0 through 3] are lan friends. So it can be accessed via,, list[friend][friends] Then write a couple small functions that returns the first friend if the seated (or result) list value ==0 if a first has already been picked then pick next friend based on the previous result. Every time a friend is picked check so see if its already in the list. If it is disregard it and try again. If you get to the end of the list hence the current solution is just bad then it has to backup. Don't save a result in the knights list until its verified good. Also if result[a] is seated and the next is picked via list[result[a]][friends] then you know they're friends already just need to check if already seated. Its also useful to write a function that takes in the current position selection and checks if they're already in the list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// the thought process looks like this

int firstfriend(int p1){return list[p1][0];}

bool back=false;
if(result[start] ==0 && start>0)
int temp = firstfriend(result[start-1]) 
         If(!seated(temp,result)
          result[start]=temp;
          else{
                    for(int i=1; i<4;i++){
                           temp=findnextfriend(temp,result[start-1])

                             if(!seated(temp,result)
                             result[start]=temp;
                             back=false;
                             break;
                             else{  back=true;}
                    }
            }
// and so on 


I've been coding as a hobby for a long time and this was a royal pita.
Last edited on
Topic archived. No new replies allowed.