How Many Edges Will Herman Visit Before Reaching the Poison?

  • Thread starter Thread starter Demon117
  • Start date Start date
  • Tags Tags
    Coding
AI Thread Summary
Herman the Fly's problem involves simulating his movement on a cube's edges, starting from a random corner while trying to avoid poison placed at another corner. The initial code provided has logical errors, particularly in the function that checks if Herman is dead, as it uses an assignment operator instead of a comparison. Contributors emphasize the importance of developing a clear algorithm before coding and discourage looking for examples to copy. They suggest that learning comes from creating original solutions rather than imitating others. The discussion highlights the need for proper function usage and avoiding global variables in the code.
Demon117
Messages
162
Reaction score
1

Homework Statement



Consider Herman the Fly. Herman must exist on the edges of a cube. At the start of a trial, Herman is randomly placed on one of the eight corners of a cube and poison is placed on one of the eight corners. Herman starts moving along one of the three edges. When he comes to a corner, he chooses one of the two other edges and continues. When he reaches the corner with the poison, he dies. How many edges will Herman visit before he hits the poison? Note that Herman may possibly visit zero to an infinite number of edges in a given trial.

The cube corners are labeled from 0 to 7 with 0 starting at the upper left hand corner of the front fact proceeding clockwise with corners 0, 1, 2, and 3 on the front face, and corner 4 being the upper left hand corner of the rear face and proceeding clockwise with corners 4, 5, 6, and 7 on the rear face.

Develop a simulation of Herman's travels around the cube. For each trial, print out the initial corners of Herman and the poison and all the corners Herman visits until reaching the corner with the poison. Then display the total number of corners visited by Herman in this trial and a running average of the number of corners Herman reached in all trials since the program with initiated. Then ask the user if another trial is desired.

Insufficiently commented code, poorly chosen variable or function names,. not using functions sufficiently or appropriately, using global variables when not required, nor not passing the correct information into a function, etc. will reduce one's grade. Each function requires a block comment stating your name, a single sentence (sans "ands" or "ors") stating the purpose of that function, as well as a description of the parameters (if the names are insufficient). Don't use global variables to avoid passing parameters. An acceptable solution will have at least 10 functions.



The Attempt at a Solution



This is all that I have:
Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cmath>
using namespace std;

bool isHermanDead(int pos1,int pos2)//
{
     if (pos1=pos2)
        cout<<"Herman is Dead"<<endl;
     else
         cout<<"Herman is alive"<<endl;
}

int main()
{
    srand( time(NULL) );
    int poisonPos = rand()%10;
    int hermanPos = rand()%10;
    
    bool heDead = isHermanDead(hermanPos, poisonPos);
    cout<<"The result is that "<<heDead<<endl;
    
    system("pause");
    
    return 0;
}

This is all that I have for the program so far. But apparently I have something amiss here. Does anyone know of examples out there that are similar in nature to this problem?
 
Physics news on Phys.org
matumich26 said:
But apparently I have something amiss here.
Uh, yeah. "Something amiss" is a bit of an understatement.

Does anyone know of examples out there that are similar in nature to this problem?
You don't learn by copying. You learn by doing. Start learning.Show some work on your own. What you have done so far does not qualify as such.
 
You don't have much to go on, yet.

One comment: your isHermanDead function will always return true, no matter what pos1 and pos2 are.

Do you have an algorithm that you're going to implement in code? One instructor I had years ago said, "The sooner you sit down to the keyboard, the longer your program will take to write." It looks like you have sat down to the keyboard too soon, without having a clear idea of what your program needs to do.

Instead of looking for examples that may or may not be similar, it would be better to figure out what you need to do in this problem.
 
D H said:
Uh, yeah. "Something amiss" is a bit of an understatement.


You don't learn by copying. You learn by doing. Start learning.


Show some work on your own. What you have done so far does not qualify as such.

That was never my intent sir. Just because I ask for examples does not assume I am going to copy said examples. Please do not undermine my intelligence by assuming such things. I did not want to include anything that was irrelevant to the problem at hand. That being said, no one learns by ignorance either.
 
matumich26 said:
I did not want to include anything that was irrelevant to the problem at hand.
But any parts that are relevant get used, right?

I'm with D H on this. Start by devising your own algorithm, and then writing functions that implement it. You will learn a lot more by coming up with your own solution than by looking at someone else's. When you get further along (a lot further along), and have run into problems, let us know, and we'll give you a hand.
 

Similar threads

Back
Top