1. ## cannibals and missionaries

Can anybody help me with the following problem:

Write a C program for the missionaries and cannibals problem.
The rules are(for those who haven't played the game):
# There are three missionaries and three cannibals on the left bank of a river.
# They wish to cross over to the right bank using a boat that can only carry two at a time.
# The number of cannibals on either bank must never exceed the number of missionaries on the same bank, otherwise the missionaries will become the cannibals' dinner!

I'd be highly grateful if anybody can help me with this.

2. What have you got so far ?

3. to be honest i've just started working on it. have around a week to work on it.
the main problem is that i just started programming n i'm still working to get used to it.

4. well, we're not going to give you the answer right away (if at all). Try to solve the problem on paper, write down how you solved it and then translate the solution into code.

5. I understand that. Honestly, I'm not asking you for answer which i can just copy.
I also want to learn. I solved the problem on paper, have the algorithm but the thing is I'm very new with programming so I just have a faint idea of converting it into codes.
I started like 2 weeks ago only.

6. The way you learn is to write programs yourself very often. The more you do that, the faster you learn. In this day and age, you can learn the syntax of various programmings languages right from the internet for free. It's very easy to pick that up, but what can't really be taught is experience.

7. Here are a few guidelines:

- You somehow need to store where the cannibals/missionaries are (on what bank). Of course you could have 6 different variables but a slightly better approach would be to have 2 arrays, one for each bank (or one for the missionaries and one for the cannibals). The data structure could look like this:

Code:
```#define CANNIBALS 3
#define MISSIONARIES 3

#define CANNIBAL 1
#define MISSIONARY 2

char leftRiverBank[CANNIBALS + MISSIONARIES] = {0};
char rightRiverBank[CANNIBALS + MISSIONARIES] = {0};

/* put x missionaries on the left river bank */
int i;
for (i = 0; i < MISSIONARIES; i++)
{
leftRiverBank[i] = MISSIONARY;
}

/* put y cannibals on the right river bank */
for (i = 0; i < CANNIBALS i++)
{
rightRiverBank[i] = CANNIBAL
}```
You also need several utility functions, like a function that checks if there is a missionary/cannibal on a side of the river or a function that moves them across with the boat etc.

8. Why is that proffessors give newbies to a language some challenging assignments when they have just began the language syntax? Have they not heard from start from the basics and work upwards?

When I was at college learning basic C, my first assignment was to input 10 numbers and print back the highest, lowest, product and average. Simple and straightforward. Then they became progressivly more challenging as I learned new features. Has the learnng systen changed dramaticky?

Then write it down in pseudo code, and arrange it into the logical functions for your program.

I'm thinking functions like "cannibal_aboard(), padre_aboard(), x_river(int direction), might be useful. int's like ls_cannibals, rs_cannibals, ls_padres, rs_padres, might be handy variables.

Maybe we should make the left and right side padre's a float type, just in case the cannibals have eaten part of them, huh? <just kidding>

If you've spent 2 weeks on this, and have no code yet, I'd say get busy working on this! Show us what you've got. Work from your algorithm, into pseudo code, and then "flesh it out" into code. Put off the details, for now. Work with the logic, and the flow of execution.

I've never programmed this puzzle, but have solved it with paper/pencil. Great assignment!

10. Originally Posted by swgh
Has the learnng systen changed dramaticky?
Perhaps. It wouldn't surprise me that there are bad professors, since we definitely know they exist.

At the same time, there's the other side of things. Students can very well be the problem: "Huh, we talked about it? We didn't see this when I didn't attend class for the last two weeks, or when I skipped the first three assignments."

So, could be more than one reason for this.

11. Thanks koni and all you guys. I have started working on the problem.
I'll get back to you guys when I get stuck again(which i surely will pretty soon), till then please feel free to write your opinions and suggestions.

I havent been working on this problem for two weeks... I have started learning C language 2 weeks ago.
Thanks for the suggestions, i'll need to decode them to understand myself first:P

13. Perhaps the intent of the exercise is to try every possible combination of movements, and report those which succeed in getting everyone across within the rules.

In essence, try a move, check the rules, repeat until someone gets eaten or everyone is across.

14. News to me, but it seems there are two sets of rules for this puzzle.

The harder of the two is that when the boat's passenger(s), reaches either side of the river, the passenger(s) DO count toward the tally of missionaries and cannibals on that side of the river.

The easier one is that the boat's passengers who never sets foot on the shore, don't count toward the tally of the shore's missionaries or cannibals, unless they get out of the boat. So a person could stay in the boat, and cross back over the river, and never be included in the tally until /unless he got out of the boat on a shore.

I'll bet the professor wants the harder one to be solved, of course.

Salem's suggestion is great, but I believe that's far beyond a 2 week old beginner's class in C! So it seems like some hints may be forthcoming in this thread.

Like although you need to take a cannibal on the first X of the river, the halfway point will have all 3 missionaries across the river, and all the cannibals back on the original side. THEN the cannibals transport themselves across the river.

Edit: correction - for the harder problem, two cannibals X the river first, in my solution. All the missionaries will be across the river before all the cannibals make it, but it's not halfway through the X's. That will be the 7th crossing out of 11 needed, before all the missionaries get crossed.

15. The Harder problem you suggest is not possible, someone is going to get ate, so he probably meant the easier.