![]() |
|
#1
|
|||
|
|||
Help with c++ program with classesI'm a beginner programmer and need some help with this program.
I have trouble putting it into code. Here is a link to what i have to do: http://www.cs.wmich.edu/~nelson/CS111SP04/lab06/lab06.htm I'm not sure what to include in the class or how to code the functions, but I know i need 3 functions. One to initialize the board one to move the cockroach, and one to return the number of unvisited squares. There is also a function to print the board. If i could get any help, it would be much appreciated. Thanks. |
|
#2
|
||||
|
||||
|
Quote:
Hey, I found a use for the banana! Serially... You need to give it a try first. Post what you have done, and ask specific questions to help us understand what you need help with. "Everything" is not specific. |
|
#3
|
||||
|
||||
|
I wrote a program similar to the one described by your assignment, however, you must attempt to do this yourself before we actually can help you. Otherwise, you're just asking us to do it for you
__________________
-Aaron |
|
#4
|
|||
|
|||
|
I understand.
Well, here it is so far: CPP / C++ / C Code:
I know its awful Last edited by dsmith : 17-Mar-2004 at 08:37.
Reason: Please use [c] & [/c] for syntax highlighting
|
|
#5
|
|||
|
|||
|
I updated some of the functions and added a new one.
CPP / C++ / C Code:
|
|
#6
|
||||
|
||||
|
Quote:
In essense you've said: "Here's the flour, here's the sugar. Make the stuff." Cookies? Cake? Onion Ring batter? Banana Bread? From my previous post: Quote:
|
|
#7
|
||||
|
||||
|
Alright well you're off to a great start. First thing I'd do is change the variables i and j to x and y, because that makes more sense as far as plotting points, and won't be confusing to anyone who reads your source code.
int Initialize(int&, int&) Well, I read through the code, and at first glance it looked okay, but then something raised an eyebrow. I noticed that you used your board variable without actually passing it to the function. Your function header should now look more like this: int Initialize(int &x, int &y, bool board[][N]);. You must always specify the size of the 2nd dimension when using a two-dimensional array with a regular or named constant, which means your variable N should either be a const int or should be defined with a #define statement.. The second thing I noticed is that you have your function set to return an integral type, but all of your variables are passed by reference. You can and should change the return type to void. int UnvisitSquares(int) First of all, what the heck does UnvisitSquares mean? Unvisit isn't a word. You should be more careful about how you name your functions. My preferred naming convention for functions is to name them after what they do. So if this function counts how many squares are left to be visited, then you should name it something like TallyUnvisitedSquares. The main point of this rant is that UnvisitSquares is really vague. First thing I noticed about this function is that you declared UnvisitSquares in the header, and you also declared UnvisitSquares in your class definition. This will produce an error, because you're still in the scope of your class. You do not have to create a new variable, and you can simply reference the UnvisitSquares that you declared in your class definition without passing it to the function. It is local, so you can change it without having to do anything with pointers or reference operators. This also means you should change the return value to void. Next, I don't understand why you initialize UnvisitSquares to zero at the beginning of your UnvisitSquares function. Since you are counting how many squares haven't been visited, you should start UnvisitSquares at the total number of elements in your array, and subtract one from that amount every time the roach moves to a square that hasn't been visited, then update the board to show that the current location of the roach has been visited. If I were you, I'd delete that whole function, think about what it needs to do, then rewrite it to do that. Always do the thinking BEFORE you code, not AS you code. Also, get rid of that semi-colon between the right parenthesis and the left brace. Finally, your MoveRoach function Moving the roach around the board is a lot simpler than you'd think. The assignment specifies that you must use a random integer between -1 and 1 to decide which direction to move... since this is a total of 3 possible directions (-1, 0, 1), I'm assuming he doesn't care if the roach doesn't know how to move downward (2). Anyways, to get the random number, you will need to use the rand() function from the stdlib.h library. In order to get a random number between -1 and 1, I would use the modulus operator to mod the random number by 2 (to get a number between 0 and 2), then subtract 1 (if we got 0, we now have -1, if we got 1, we now have 0, if we got 2, we now have 1). Then you can decide that if you encounter -1, you move left, 0 for right, 1 for up, and since there's no fourth number, you're going to have to live with not moving down, or mention it to your instructor. Your MoveRoach function should update the Roach's position, and either alter the state of the position it reaches, or leave that to another function, like UnvisitSquares. Sorry for all the reading... hope this helps __________________
-Aaron |
|
#8
|
||||
|
||||
|
Woops! I spotted another problem much earlier when I posted that hunk of crap up there, but in my scramble to pick up all the pieces you dropped, I managed to forget about it. Your class definition should follow THIS format:
CPP / C++ / C Code:
__________________
-Aaron |
|
#9
|
|||
|
|||
|
I was thinking more along the lines of moving the cockroach by generating a random x and a random y so he can move up, down, left, right, or diagonal. 8 directions in all. I am also updating names of the function so they aren't as confusing. I also fixed some errors. I have also pretty much finished the main function. Right now i am working on the move function. Here is what the rest looks like:
CPP / C++ / C Code:
I'm having trouble with the last function. I'm not sure how to update the board and move the roach. |
|
#10
|
||||
|
||||
|
Ah, ha. I made some mistakes in my assessment! Okay, well if you have a specific question for me, just ask, and I'll see what I can do. Good job so far!
__________________
-Aaron |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Modify a C program | ayoub | C Programming Language | 3 | 15-Mar-2004 12:34 |
| compiling a program within another (C++) | Siphiro | C++ Forum | 5 | 06-Feb-2004 16:35 |
| error during program | rjd72285 | C++ Forum | 0 | 11-Nov-2003 19:49 |
| one program access another? | dgoulston | C++ Forum | 1 | 07-Oct-2003 12:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The