GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Mar-2004, 09:27
jimmy55 jimmy55 is offline
New Member
 
Join Date: Mar 2004
Posts: 5
jimmy55 is on a distinguished road

Help with c++ program with classes


I'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  
Old 16-Mar-2004, 10:38
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by jimmy55
... one to move the cockroach...
Around here we don't write code with bugs

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  
Old 16-Mar-2004, 17:38
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 17-Mar-2004, 00:06
jimmy55 jimmy55 is offline
New Member
 
Join Date: Mar 2004
Posts: 5
jimmy55 is on a distinguished road
I understand.
Well, here it is so far:
CPP / C++ / C Code:
Roach class{
private:
	int UnvisitSqaures;
	bool board[N][N];
	int i;
	int j;

public:
	void PrintBoard(int, bool);
	int MoveRoach(int, int);
	int UnvisitSquares(int);
	int Initialize(int&, int&);
};



int main(){




	return 0;
}

void Roach::PrintBoard(int N, bool board[N][N]){

	for(int i=0,i<N,i++){
		for(int j=0,j<N,j++){
			if(board[i][j]==true)
				cout<<"T ";
			else
				cout<<"F ";
		}
		cout<<endl;
	}
	return;
}

int Roach::Initialize(int& i, int& j){
	for(int i=0,i<N,i++){
		for(int j=0,j<N,j++){
			board[i][j]=false;
		}
	}
//still need more in this function

}

I know its awful , but i'm terrible at coding and am struggling in this class. Its hard to meet the conditions my teacher offers and i'm not sure how. If you can help me correct these functions i've pieced together and offer me some insight on the other functions, that will be helpful. Its getting late so this is as far as i got.
Last edited by dsmith : 17-Mar-2004 at 08:37. Reason: Please use [c] & [/c] for syntax highlighting
  #5  
Old 17-Mar-2004, 11:42
jimmy55 jimmy55 is offline
New Member
 
Join Date: Mar 2004
Posts: 5
jimmy55 is on a distinguished road
I updated some of the functions and added a new one.

CPP / C++ / C Code:
Roach class{
private:
	int UnvisitSqaures;
	bool board[N][N];
	int i;
	int j;

public:
	void PrintBoard(int, bool);
	int MoveRoach(int, int);
	int UnvisitSquares(int);
	int Initialize(int&, int&);
};

int N=10;

int main(){




	return 0;
}

void Roach::PrintBoard(int N, bool board[N][N]){

	for(int i=0;i<N;i++){
		for(int j=0,j<N,j++){
			if(board[i][j]==true)
				cout<<"T ";
			else
				cout<<"F ";
		}
		cout<<endl;
	}
	return;
}

int Roach::Initialize(int& i, int& j){
	
	for(int a=0,a<N,a++){
		for(int b=0,b<N,b++){
			board[i][j]=false;
		}
	}
	if(i<0 || i>=N || j<0 || j>=N){
		i=0;
		j=0;
		board[i][j]=true;

//more code needed
	}
}



int Roach::UnvisitSquares(int UnvisitSquares);{
	UnvisitSquares=0;
	
	for(int i=0;i<N;i++)
		for(int j=0;j<=N;j++){
			if(board[i][j]==false)
				UnvisitSquares++;
		}
	return UnvisitSquares;
}

int Roach::MoveRoach(int i, int j){
//???
}

  #6  
Old 17-Mar-2004, 12:47
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by jimmy55
I know its awful , but i'm terrible at coding and am struggling in this class. Its hard to meet the conditions my teacher offers and i'm not sure how. If you can help me correct these functions i've pieced together and offer me some insight on the other functions, that will be helpful. Its getting late so this is as far as i got.
We cannot help with what we don't know. You need to give us a clear idea of what you did, what you need to do, etc. Just adding a comment that says "more code needed" isn't quite enough. With this instruction, I could write 250 lines of code for you to add to your function that does absolutely nothing to solve your problem.

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:
Ask specific questions to help us understand what you need help with.
  #7  
Old 17-Mar-2004, 12:52
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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  
Old 17-Mar-2004, 15:01
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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:
class MyClassName
{
  // public and private stuff...
};
You currently have the word class and your class name reversed. I'd fix that quick. There's also a spelling error in your variable declarations... your misspelled variable name is "UnvisitSqaures."
__________________
-Aaron
  #9  
Old 17-Mar-2004, 15:04
jimmy55 jimmy55 is offline
New Member
 
Join Date: Mar 2004
Posts: 5
jimmy55 is on a distinguished road
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:
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;


class Roach{
private:
	int UnvisitedSqaures;
	bool board[10][10];
	int x;
	int y;

public:
	void PrintBoard();
	int MoveRoach(int, int);
	int GetUnvisitedSquares(int);
	int Initialize(int&, int&);
};

int N=10;

int main(){
	int UnvisitedSquares,x,y,Y;
	UnvisitedSquares=0;
	Roach r1;
	
	cout<<"input starting location x: ";
	cin>>x;
	cout<<"input starting location y: ";
	cin>>y;
	r1.Initialize(x,y);
	while(r1.GetUnvisitedSquares(UnvisitedSquares)>0){
		for(int i=1;i<=100;i++)
			r1.MoveRoach(x,y);
		cout<<"Input 'Y' if you would like to see the board: ";
		cin>>Y;
		if(Y=='Y')
			r1.PrintBoard;
	}
			



	return 0;
}

void Roach::PrintBoard(){

	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			if(board[i][j]==true)
				cout<<"T ";
			else
				cout<<"F ";
		}
		cout<<endl;
	}
	return;
}

int Roach::Initialize(int& x, int& y){
	
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			board[i][j]=false;
		}
	}
	if(x<0 || x>=N || y<0 || y>=N){
		x=0;
		y=0;
	}
	board[x][y]=true;
	

	
return 0;
}



int Roach::GetUnvisitedSquares(int Unvisited){
		
	for(int i=0;i<N;i++)
		for(int j=0;j<=N;j++){
			if(board[i][j]==false)
				Unvisited++;
		}
	return Unvisited;
}

int Roach::MoveRoach(int& loc_x,int& loc_y){
	loc_x=loc_x+(rand()%2)-1;
	loc_y=loc_y+(rand()%2)-1;

I'm having trouble with the last function. I'm not sure how to update the board and move the roach.
  #10  
Old 17-Mar-2004, 15:09
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
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 GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 03:21.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.