Drunk walker

hello everyone, I had problem also with my drunk walker program, it compile ok but the result is wrong . will any one help me with that cause i must submit it next week.


/***************************************************************
*
* program name: Drunkwalker
* author: Luay Almamury
* date due: 4/20/09
* remarks: a drunken walk
*
*
****************************************************************/

/**********************************
* libraries
**********************************/
#include <iostream> // needed for cin and cout
using namespace std;

/**********************************
* proto types
***********************************/
void initializeArray(); //enters a period in all entries
void initializeAlphabet(); //enters the alphabet
void printArray(); //prints array containing the drunken walk
void randDirection(); //generates a random direction
int canmove(int row, int col);


/***********************************
* global variables
************************************/
char Array[10][10];
int direction;
char Alphabet[26];
int letter=65;
int main ()
{
/*****************************
* local variables
******************************/
int row;
int col;
int alphaIndex = 0; //letters of the alphabet
int stopCode=0;
int letter=65;

initializeArray();
initializeAlphabet();
printArray();
cout<<" pleaase enter your row ";
cin>>row;
cout<<" please enter your col";
cin>>col;


Array[row][col]=(char)letter++; // put the charecter on the array


while(stopCode != 1)
{
randDirection(); // random number between 0-3
switch(direction)
{
case 0: if ((row-1,col)==1)
{
Array[row-1][col]=(char)letter++;
row=row-1;
}
else if ( canmove(row,col+1)==1)
{
Array[row][col+1]=(char)letter++;
col++;
}
else if ( canmove(row+1,col)==1)
{
Array[row+1][col]=(char)letter++;
col++;
}
else if ( canmove(row,col-1)==1)
{
Array[row][col-1]=(char)letter++;
col--;
}

break;


case 1: if ((row+1,col)==1)
{
Array[row+1][col]=(char)letter++;
row=row+1;
}
else if ( canmove(row,col+1)==1)
{
Array[row][col+1]=(char)letter++;
col++;
}
else if ( canmove(row+1,col)==1)
{
Array[row+1][col]=(char)letter++;
col++;
}
else if ( canmove(row,col-1)==1)
{
Array[row][col-1]=(char)letter++;
col--;
}
break;

case 2: if ((row,col+1)==1)
{
Array[row][col+1]=(char)letter++;
row=row-1;
}
else if ( canmove(row,col+1)==1)
{
Array[row][col+1]=(char)letter++;
col++;
}
else if ( canmove(row+1,col)==1)
{
Array[row+1][col]=(char)letter++;
col++;
}
else if ( canmove(row,col-1)==1)
{
Array[row][col-1]=(char)letter++;
col--;
}
break;

case 3: if ((row,col+1)==1)
{
Array[row][col+1]=(char)letter++;
row=row-1;
}
else if ( canmove(row,col+1)==1)
{
Array[row][col+1]=(char)letter++;
col++;
}
else if ( canmove(row+1,col)==1)
{
Array[row+1][col]=(char)letter++;
col++;
}
else if ( canmove(row,col-1)==1)
{
Array[row][col-1]=(char)letter++;
col--;
}
break;
default: break;
// end switch

if(alphaIndex == 26)
{
stopCode = 1;
}
} // end while loop

printArray();

system("pause");

return 0;
} //end of main()
}
/********************************************
* set all elements in the array to a '.'
********************************************/
void initializeArray()
{
int row;
int col;
for(row=0; row<10; row++)
{
for(col=0; col<10; col++)
{
Array[row][col] = '.';
}
}
}// end of initializeArray()

/*************************************************
* initialize the alphabet array from 'A' to 'Z'
**************************************************/
void initializeAlphabet()
{
int row=0;

for(int n = 65; n<91; n++)
{
Alphabet[row]=n;
row++;
}
/*for(int x=0; x<27; x++)
{
cout << Alphabet[x];
}*/

}//end of initializeAlphabet()

/********************************************
* print out the 10X10 array
********************************************/
void printArray()
{
int row;
int col;
for(row=0; row<10; row++)
{
cout << "\n";
for(col=0; col<10; col++)
{
cout << Array[row][col];
}
}
cout << "\n\n";
}// end of printArray()

/********************************************
* creata a random number from 0~4 and
* save the number in a GLOBAL variable
* named direction
********************************************/
void randDirection()
{
int n = rand() % 100;
direction = n % 4; // random number between 0-3
}//end of randDirection();


/*******************************************
* creat move function that check the
* border of the array
********************************************/

int canmove(int row, int col)
{
int move=0;
if( row>=0&row<=9 & col>=0 &col<=9) // check the boundries of the array
{
if (Array[row][col]='.') // check row*col =to '.'

{
Array[row][col]=(char)letter;
}
}
return move;
}









Last edited on
In float CircleArea(float radius) you are redefining float Area. You don't need the float keyword before it. Also, cout << "Enter a radius"; and cin >> radius; Are commented out. Also, when returning just 1 variable, you don't need parenthesis.
Ok. I spent some time on looking through your program. I have some things for you:

1. As ModShop said, you don't need parentheses to return a single variable.
2. Your program didn't work because you commented out the part where the program actually gets the value of your variables.
3. This is the one that is probably the most important - spelling and capitalization. I caught quite a few bugs regarding things like "raidus" instead of radius. Good spelling is crucial for programming. As is capitalization. Computers (especially C++ and programming languages!) almost always recognize lowercase letters as different characters than their uppercase types. An example is, when you declared (EDIT: not declared, used as a parameter) volume, you said, "Volume," but when you returned it, you said, "volume." This is key.

Now that I have finished my rant, hopefully you are a better and more efficient programmer! :)

Now, here is my edited version of your program. Look over it carefully, and find the changes and mistakes.

By the way some of your mathematical equations were wrong. I corrected them for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;
#include <math.h>
//this define thing is easier than doing the const variable(in my opinion)
#define PI 3.14159265

//If you want to get really specific and accurate, you would probably use double instead of float
float height; 
float radius; 
float Volume ;
float Area ;

float CircleArea(float radius)
{
cout<<" please enter the raduis \n";
cin>>radius;
float Area = PI*radius*radius;


return Area;
}// end circle function


float CylinderVolume(float height, float radius) //define volume function
{

cout<<"Enter the height:";
cin>>height;
cout<<"Enter the radius:";
cin>>radius;

float Volume = height * (PI*radius*radius);

return Volume;
}



int main()
{

cout<<CircleArea(radius)<<"\n";

cout<<CylinderVolume(height,radius);

return 0;
}//end main function 
Last edited on
thank you very much
Topic archived. No new replies allowed.