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 03-Sep-2005, 15:14
Elsydeon Elsydeon is offline
Junior Member
 
Join Date: Aug 2005
Posts: 45
Elsydeon is on a distinguished road

Problem with an int array


Well, the second lab is much easier... logically I get it all. I'm just running into a problem with an array I need to get working.

Basically it's a piece of the knight's tour. Not the entire program. I have to write a method that will check if the square the knight is jumping to is a valid move. Meaning it's not off the board, and the knight hasn't visited it before.

Seems very short and simple, and the main is just how I wanted to display it to him in class, not what will be the tour program at all. The only problem is in the method validMove itself. Here's what I've got.

CPP / C++ / C Code:
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>


class knightTour {
	//Handles logic problem of Knight's tour
	//Maximum board size 20x20
public:
	bool validMove(int row, int col);


};//class knightTour

const maxSize=20;
typedef int boardType[maxSize][maxSize];

void main(){
	knightTour check;
	int row, col;
	cout<<"Enter the board location you wish to test:"<<endl;
	cout<<"Board row number: ";
	cin>>row;
	cout<<endl;
	cout<<"Board column number: ";
	cin>>col;
	cout<<endl;
	
	check.validMove(row, col);

}

bool knightTour::validMove(int row, int col){
	if(row>maxSize || col>maxSize || (row>maxSize && col>maxSize) || row<0 || col<0 || (row<0 && col<0)){
		return 1;
	}
//	else{
//		if((boardType[row][col]) == 0){
//			return 0;
//		}
//		else{
//			return 1;
//		}
//		}
}

I commented out where I'm getting my problems. Why am I not able to look at a specific array location and check it? I thought it might be that it's not sent to the method, but any attempts to do that resulted in more compiler errors, and I'm not sure it even needs it. You can remove the comments and see what errors I got from it... Hopefully someone can offer me some sort direction. I'd appreciate it.
  #2  
Old 03-Sep-2005, 15:48
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough
Quote:
typedef int boardType[maxSize][maxSize];
Careful here.
You are saying that you want the string boardType[maxSize][maxSize] to be an alias for int.
For instance:
CPP / C++ / C Code:
typedef int Blah;

int main ()
{
   Blah x = 2;
   std::cout << x << '\n';
   return 0;
}

Prints:
2

remove the typedef and simply say int boardType[max][max];
Or bool boardType[max][max];

[edit]
To better illustrate:
Using the above example
CPP / C++ / C Code:
Blah x = 2;
int y = 7;

std::cout << "Type x " << typeid(x).name() << '\n';
std::cout << "Type y " << typeid(y).name() << '\n';


On my system the output is:
Type x i
Type y i

Output of name() is system dependant however, you should notice that they are the same type.
  #3  
Old 04-Sep-2005, 01:25
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
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
Yeah, what he said.

Also, your if is:
CPP / C++ / C Code:
 if(row>maxSize || col>maxSize || (row>maxSize && col>maxSize) || row<0 || col<0 || (row<0 && col<0))
Now by looking at it again, isn't
CPP / C++ / C Code:
if(row>maxSize || col>maxSize || (row>maxSize && col>maxSize) 
redundant? "If row or col is greater than maxsize" already takes into account row and col greater, therefore all you really need is
CPP / C++ / C Code:
 if (row>maxSize || col>maxSize || row<0 || col<0)

But then again, to be perfectly accurate, you really need
CPP / C++ / C Code:
if (row >= maxSize || 
    col >= maxSize || 
    row <  0       || 
    col <  0)
which also includes another way to reformat the statement for readability. Note the >=
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #4  
Old 05-Sep-2005, 09:24
Elsydeon Elsydeon is offline
Junior Member
 
Join Date: Aug 2005
Posts: 45
Elsydeon is on a distinguished road
One last thing I'm weird on...

CPP / C++ / C Code:
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>


class knightTour {
	//Handles logic problem of Knight's tour
	//Maximum board size 20x20
public:
	bool validMove(int row, int col);


};//class knightTour

const maxSize=20;
int boardType[maxSize][maxSize];

void main(){
	knightTour check;
	int row, col;
	
	boardType[0][0]=1;
	boardType[2][1]=2;
	boardType[4][2]=3;
	boardType[3][4]=4;
	boardType[2][6]=5;
	boardType[4][5]=6;
	boardType[6][6]=7;

	cout<<"For reference, board locations 0,0 2,1 4,2 3,4 2,6 4,5 and 6,6 have been marked as already visited by the knight.";
	cout<<"Enter the board location you wish to test:"<<endl;
	cout<<"Board row number: "<<endl;
	cin>>row;
	cout<<endl;
	cout<<"Board column number: ";
	cin>>col;
	cout<<endl;
	
	check.validMove(row, col);

}

bool knightTour::validMove(int row, int col){
	if(row>=maxSize || col>=maxSize ||  row<0 || col<0){
		return 1;
		cout<<"Space falls off board";
	}
	else{
		if((boardType[row][col]) == 0){
			return 0;
			cout<<"Valid location";
		}
		else{
			return 1;
			cout<<"Space already visited";
		}
		}
}

Having not messed with bool enough, or classes and methods in general, I don't know how to do my last little tweak. I want to get the couts outside of validMove and into main. Something like...

CPP / C++ / C Code:
if(check.validMove==1){
cout<<"Invalid move";
}

Now, obviously I know that doesn't work, I tried it. I just don't know how to do a test on the returned value from the method. Never done it before, and can't find it in the textbook I have.
  #5  
Old 05-Sep-2005, 10:29
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by Elsydeon
One last thing I'm weird on...

CPP / C++ / C Code:
if(check.validMove==1){
cout<<"Invalid move";
}

Now, obviously I know that doesn't work, I tried it. I just don't know how to do a test on the returned value from the method. Never done it before, and can't find it in the textbook I have.

Maybe something like,

CPP / C++ / C Code:
if(check.validMove(int, int) == 1)

would be more appropriate.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
 
 

Recent GIDBlogProgramming ebook direct download available 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
array problem vaha C Programming Language 9 18-May-2005 13:23
problem modifying an array of char in a function ronin C Programming Language 10 28-Mar-2005 19:15
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 22:26
problem reading to a dynamic array noamfrie C Programming Language 9 02-Jan-2005 19:35
Problem in array Kay Chan C Programming Language 2 05-Oct-2004 22:16

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:17.


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