SlideShare a Scribd company logo
1 of 8
Download to read offline
Please help me find where I do something wrong with my program
I am working on creating a random walk across a grid. I already wrote most of the code, but
every time I run my program, the output is only '.'
this is the requirement :
- using #include , #include , #include
- const int SIZE = 10;
- typedef char Grid[SIZE][SIZE]
- The grid must be set to have all its elements be the character ’.’ before the walk is begun.
The program must contain the following function prototypes, the main function, and the function
definitions for all the other functions:
****Function prototypes
bool can_move_up(int i, int j, Grid walk);
bool can_move_down(int i, int j, Grid walk);
bool can_move_left(int i, int j, Grid walk);
bool can_move_right(int i, int j, Grid walk);
void init_array(Grid walk);
void generate_random_walk(Grid walk);
void print_array(Grid walk);
int main(void)
{
Grid walk; // the grid in which the random walk occurs
srand((unsigned) time(NULL));
init_array(walk);
generate_random_walk(walk);
print_array(walk);
return 0;
}
This is what I got so far:
#include
#include // for srand and rand
#include // for time
using namespace std;
const int SIZE = 10;
typedef char Grid[SIZE][SIZE];
// Function declaration
bool can_move_up(int i, int j, Grid walk);
bool can_move_down(int i, int j, Grid walk);
bool can_move_left(int i, int j, Grid walk);
bool can_move_right(int i, int j, Grid walk);
void init_array(Grid walk);
void generate_random_walk(Grid walk);
void print_array(Grid walk);
int main(void)
{
Grid walk; // the grid in which the random walk occurs
srand((unsigned) time(NULL));
init_array(walk); //calling init_array function
generate_random_walk(walk); //calling generate_random_walk function
print_array(walk); //calling print_array function
return 0;
}
void init_array(Grid walk) // function init_array
{
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
walk[i][j] = '.'; //filling array with '.'
}
}
}
bool can_move_up(int i, int j, Grid walk)
{
if((i < 0) && (walk[i - 1][j] == '.') )
return true;
else
return false;
}
bool can_move_down(int i, int j, Grid walk)
{
if((i < 9) && (walk[i + 1][j] == '.'))
return true;
else
return false;
}
bool can_move_left(int i, int j, Grid walk)
{
if((j > 0) && (walk[i][j - 1] == '.'))
return true;
else
return false;
}
bool can_move_right(int i, int j, Grid walk)
{
if ((j > 9) && (walk[i][j + 1] == '.'))
return true;
else
return false;
}
void generate_random_walk(Grid walk)
{
int i,j;
int random;
char letter;
random = rand() % 4;
for (letter = 'A'; letter <= 'Z'; letter ++)
{
switch (random)
{
case 0:
can_move_up(i, j, walk);
break;
case 1:
can_move_down(i, j, walk);
break;
case 2:
can_move_left(i, j, walk);
break;
case 3:
can_move_right( i, j, walk);
break;
default:
break;
}
}
}
void print_array(Grid walk)
{
for (int i = 0; i < SIZE ; i++)
{
for (int j = 0; j < SIZE ; j++)
{
cout << walk[i][j];
}
cout << endl;
}
}
Solution
#include
#include
#include
using namespace std;
const int SIZE = 10;
typedef char Grid[SIZE][SIZE];
// Function declaration
bool can_move_up(int i, int j, Grid walk);
bool can_move_down(int i, int j, Grid walk);
bool can_move_left(int i, int j, Grid walk);
bool can_move_right(int i, int j, Grid walk);
void init_array(Grid walk);
void generate_random_walk(Grid walk);
void print_array(Grid walk);
int main(void)
{
Grid walk; // the grid in which the random walk occurs
srand((unsigned) time(NULL));
init_array(walk); //calling init_array function
generate_random_walk(walk); //calling generate_random_walk function
print_array(walk); //calling print_array function
return 0;
}
void init_array(Grid walk) // function init_array
{
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
walk[i][j] = '.'; //filling array with '.'
}
}
}
bool can_move_up(int i, int j, Grid walk)
{
if((i < 0) && (walk[i - 1][j] == '.') )
return true;
else
return false;
}
bool can_move_down(int i, int j, Grid walk)
{
if((i < 9) && (walk[i + 1][j] == '.'))
return true;
else
return false;
}
bool can_move_left(int i, int j, Grid walk)
{
if((j > 0) && (walk[i][j - 1] == '.'))
return true;
else
return false;
}
bool can_move_right(int i, int j, Grid walk)
{
if ((j > 9) && (walk[i][j + 1] == '.'))
return true;
else
return false;
}
void generate_random_walk(Grid walk)
{
int i,j;
int random;
char letter;
random = rand() % 4;
for (letter = 'A'; letter <= 'Z'; letter ++)
{
switch (random)
{
case 0:
can_move_up(i, j, walk);
break;
case 1:
can_move_down(i, j, walk);
break;
case 2:
can_move_left(i, j, walk);
break;
case 3:
can_move_right( i, j, walk);
break;
default:
break;
}
}
}
void print_array(Grid walk)
{
for (int i = 0; i < SIZE ; i++)
{
for (int j = 0; j < SIZE ; j++)
{
cout << walk[i][j];
}
cout << endl;
}
}

More Related Content

Similar to Please help me find where I do something wrong with my programI am.pdf

Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
illyasraja7
 
import java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdfimport java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdf
anwarsadath111
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfThis is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdf
anjandavid
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
anjandavid
 
I wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdfI wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdf
feelinggifts
 
Please copy and paste the code and explain why it won't work- It is su.docx
Please copy and paste the code and explain why it won't work- It is su.docxPlease copy and paste the code and explain why it won't work- It is su.docx
Please copy and paste the code and explain why it won't work- It is su.docx
cgraciela1
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
Rahul04August
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
calderoncasto9163
 
include ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfinclude ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdf
contact32
 
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
PiersRCoThomsonw
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfi have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdf
armcomputers
 
The following is my code for a connectn program. When I run my code .pdf
The following is my code for a connectn program. When I run my code .pdfThe following is my code for a connectn program. When I run my code .pdf
The following is my code for a connectn program. When I run my code .pdf
eyelineoptics
 
Test driven game development silly, stupid or inspired?
Test driven game development   silly, stupid or inspired?Test driven game development   silly, stupid or inspired?
Test driven game development silly, stupid or inspired?
Eric Smith
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
poblettesedanoree498
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
fonecomp
 

Similar to Please help me find where I do something wrong with my programI am.pdf (20)

Print Star pattern in java and print triangle of stars in java
Print Star pattern in java and print triangle of stars in javaPrint Star pattern in java and print triangle of stars in java
Print Star pattern in java and print triangle of stars in java
 
week-15x
week-15xweek-15x
week-15x
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
 
import java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdfimport java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdf
 
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdfThis is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdf
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
 
I wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdfI wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdf
 
Please copy and paste the code and explain why it won't work- It is su.docx
Please copy and paste the code and explain why it won't work- It is su.docxPlease copy and paste the code and explain why it won't work- It is su.docx
Please copy and paste the code and explain why it won't work- It is su.docx
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
 
include ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdfinclude ltiostreamgt include ltstringgt include .pdf
include ltiostreamgt include ltstringgt include .pdf
 
Ocr code
Ocr codeOcr code
Ocr code
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
 
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
 
i have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdfi have a runable code below that works with just guessing where the .pdf
i have a runable code below that works with just guessing where the .pdf
 
The following is my code for a connectn program. When I run my code .pdf
The following is my code for a connectn program. When I run my code .pdfThe following is my code for a connectn program. When I run my code .pdf
The following is my code for a connectn program. When I run my code .pdf
 
Test driven game development silly, stupid or inspired?
Test driven game development   silly, stupid or inspired?Test driven game development   silly, stupid or inspired?
Test driven game development silly, stupid or inspired?
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
 
ES6 generators
ES6 generatorsES6 generators
ES6 generators
 

More from mayorothenguyenhob69

How would you define the Wilcoxon Signed Rank TestSolutionThe.pdf
How would you define the Wilcoxon Signed Rank TestSolutionThe.pdfHow would you define the Wilcoxon Signed Rank TestSolutionThe.pdf
How would you define the Wilcoxon Signed Rank TestSolutionThe.pdf
mayorothenguyenhob69
 
How could global climate change affect vector-borne or zoonotic di.pdf
How could global climate change affect vector-borne or zoonotic di.pdfHow could global climate change affect vector-borne or zoonotic di.pdf
How could global climate change affect vector-borne or zoonotic di.pdf
mayorothenguyenhob69
 
E1-1 Reporting Amounts on the Four Basic Financial Statements [LO 1-2.pdf
E1-1 Reporting Amounts on the Four Basic Financial Statements [LO 1-2.pdfE1-1 Reporting Amounts on the Four Basic Financial Statements [LO 1-2.pdf
E1-1 Reporting Amounts on the Four Basic Financial Statements [LO 1-2.pdf
mayorothenguyenhob69
 
Compare different sexual and asexual life cycles noting their adapti.pdf
Compare different sexual and asexual life cycles noting their adapti.pdfCompare different sexual and asexual life cycles noting their adapti.pdf
Compare different sexual and asexual life cycles noting their adapti.pdf
mayorothenguyenhob69
 
BackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdfBackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdf
mayorothenguyenhob69
 
What are the common conditions resulting in thrombocytosis Explain .pdf
What are the common conditions resulting in thrombocytosis Explain .pdfWhat are the common conditions resulting in thrombocytosis Explain .pdf
What are the common conditions resulting in thrombocytosis Explain .pdf
mayorothenguyenhob69
 

More from mayorothenguyenhob69 (20)

If the sample means are different from the population mean, what do .pdf
If the sample means are different from the population mean, what do .pdfIf the sample means are different from the population mean, what do .pdf
If the sample means are different from the population mean, what do .pdf
 
How would you define the Wilcoxon Signed Rank TestSolutionThe.pdf
How would you define the Wilcoxon Signed Rank TestSolutionThe.pdfHow would you define the Wilcoxon Signed Rank TestSolutionThe.pdf
How would you define the Wilcoxon Signed Rank TestSolutionThe.pdf
 
How could global climate change affect vector-borne or zoonotic di.pdf
How could global climate change affect vector-borne or zoonotic di.pdfHow could global climate change affect vector-borne or zoonotic di.pdf
How could global climate change affect vector-borne or zoonotic di.pdf
 
How Can Sequence Data Be Used to Track Flu Virus Evolution During Pan.pdf
How Can Sequence Data Be Used to Track Flu Virus Evolution During Pan.pdfHow Can Sequence Data Be Used to Track Flu Virus Evolution During Pan.pdf
How Can Sequence Data Be Used to Track Flu Virus Evolution During Pan.pdf
 
Far from being primitive, bacteria have evolved until they are i.pdf
Far from being primitive, bacteria have evolved until they are i.pdfFar from being primitive, bacteria have evolved until they are i.pdf
Far from being primitive, bacteria have evolved until they are i.pdf
 
E1-1 Reporting Amounts on the Four Basic Financial Statements [LO 1-2.pdf
E1-1 Reporting Amounts on the Four Basic Financial Statements [LO 1-2.pdfE1-1 Reporting Amounts on the Four Basic Financial Statements [LO 1-2.pdf
E1-1 Reporting Amounts on the Four Basic Financial Statements [LO 1-2.pdf
 
Do differentiated cells transform into other type of cells How Wha.pdf
Do differentiated cells transform into other type of cells How Wha.pdfDo differentiated cells transform into other type of cells How Wha.pdf
Do differentiated cells transform into other type of cells How Wha.pdf
 
Define the terms activity, layout, intent, and AVDSolutionACT.pdf
Define the terms activity, layout, intent, and AVDSolutionACT.pdfDefine the terms activity, layout, intent, and AVDSolutionACT.pdf
Define the terms activity, layout, intent, and AVDSolutionACT.pdf
 
Describe the similarities and differences between IPv4 & IPv6.So.pdf
Describe the similarities and differences between IPv4 & IPv6.So.pdfDescribe the similarities and differences between IPv4 & IPv6.So.pdf
Describe the similarities and differences between IPv4 & IPv6.So.pdf
 
Compare different sexual and asexual life cycles noting their adapti.pdf
Compare different sexual and asexual life cycles noting their adapti.pdfCompare different sexual and asexual life cycles noting their adapti.pdf
Compare different sexual and asexual life cycles noting their adapti.pdf
 
BackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdfBackgroundIn many applications, the composition of a collection o.pdf
BackgroundIn many applications, the composition of a collection o.pdf
 
11.25In 2010, there were 117,538,000 households in the United Stat.pdf
11.25In 2010, there were 117,538,000 households in the United Stat.pdf11.25In 2010, there were 117,538,000 households in the United Stat.pdf
11.25In 2010, there were 117,538,000 households in the United Stat.pdf
 
Which methodology may be used for detection of incorporated BrdUA.pdf
Which methodology may be used for detection of incorporated BrdUA.pdfWhich methodology may be used for detection of incorporated BrdUA.pdf
Which methodology may be used for detection of incorporated BrdUA.pdf
 
Which of the following is a difference between lightweight and heavy.pdf
Which of the following is a difference between lightweight and heavy.pdfWhich of the following is a difference between lightweight and heavy.pdf
Which of the following is a difference between lightweight and heavy.pdf
 
What are the common conditions resulting in thrombocytosis Explain .pdf
What are the common conditions resulting in thrombocytosis Explain .pdfWhat are the common conditions resulting in thrombocytosis Explain .pdf
What are the common conditions resulting in thrombocytosis Explain .pdf
 
What are some differences between the xylem and the phloem Solu.pdf
What are some differences between the xylem and the phloem  Solu.pdfWhat are some differences between the xylem and the phloem  Solu.pdf
What are some differences between the xylem and the phloem Solu.pdf
 
Two families of sloths exist commonly in Central and South America (t.pdf
Two families of sloths exist commonly in Central and South America (t.pdfTwo families of sloths exist commonly in Central and South America (t.pdf
Two families of sloths exist commonly in Central and South America (t.pdf
 
Two populations of rats are discovered on nearby islands. Describe t.pdf
Two populations of rats are discovered on nearby islands. Describe t.pdfTwo populations of rats are discovered on nearby islands. Describe t.pdf
Two populations of rats are discovered on nearby islands. Describe t.pdf
 
30-32 Nuclear pores permit the passage of chromosomes outward. glu.pdf
30-32 Nuclear pores permit the passage of  chromosomes outward.  glu.pdf30-32 Nuclear pores permit the passage of  chromosomes outward.  glu.pdf
30-32 Nuclear pores permit the passage of chromosomes outward. glu.pdf
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdf
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

Please help me find where I do something wrong with my programI am.pdf

  • 1. Please help me find where I do something wrong with my program I am working on creating a random walk across a grid. I already wrote most of the code, but every time I run my program, the output is only '.' this is the requirement : - using #include , #include , #include - const int SIZE = 10; - typedef char Grid[SIZE][SIZE] - The grid must be set to have all its elements be the character ’.’ before the walk is begun. The program must contain the following function prototypes, the main function, and the function definitions for all the other functions: ****Function prototypes bool can_move_up(int i, int j, Grid walk); bool can_move_down(int i, int j, Grid walk); bool can_move_left(int i, int j, Grid walk); bool can_move_right(int i, int j, Grid walk); void init_array(Grid walk); void generate_random_walk(Grid walk); void print_array(Grid walk); int main(void) { Grid walk; // the grid in which the random walk occurs srand((unsigned) time(NULL)); init_array(walk); generate_random_walk(walk); print_array(walk); return 0; } This is what I got so far: #include #include // for srand and rand #include // for time using namespace std; const int SIZE = 10; typedef char Grid[SIZE][SIZE]; // Function declaration
  • 2. bool can_move_up(int i, int j, Grid walk); bool can_move_down(int i, int j, Grid walk); bool can_move_left(int i, int j, Grid walk); bool can_move_right(int i, int j, Grid walk); void init_array(Grid walk); void generate_random_walk(Grid walk); void print_array(Grid walk); int main(void) { Grid walk; // the grid in which the random walk occurs srand((unsigned) time(NULL)); init_array(walk); //calling init_array function generate_random_walk(walk); //calling generate_random_walk function print_array(walk); //calling print_array function return 0; } void init_array(Grid walk) // function init_array { for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { walk[i][j] = '.'; //filling array with '.' } } } bool can_move_up(int i, int j, Grid walk) { if((i < 0) && (walk[i - 1][j] == '.') ) return true; else
  • 3. return false; } bool can_move_down(int i, int j, Grid walk) { if((i < 9) && (walk[i + 1][j] == '.')) return true; else return false; } bool can_move_left(int i, int j, Grid walk) { if((j > 0) && (walk[i][j - 1] == '.')) return true; else return false; } bool can_move_right(int i, int j, Grid walk) { if ((j > 9) && (walk[i][j + 1] == '.')) return true; else return false; } void generate_random_walk(Grid walk)
  • 4. { int i,j; int random; char letter; random = rand() % 4; for (letter = 'A'; letter <= 'Z'; letter ++) { switch (random) { case 0: can_move_up(i, j, walk); break; case 1: can_move_down(i, j, walk); break; case 2: can_move_left(i, j, walk); break; case 3: can_move_right( i, j, walk); break; default: break; } } } void print_array(Grid walk)
  • 5. { for (int i = 0; i < SIZE ; i++) { for (int j = 0; j < SIZE ; j++) { cout << walk[i][j]; } cout << endl; } } Solution #include #include #include using namespace std; const int SIZE = 10; typedef char Grid[SIZE][SIZE]; // Function declaration bool can_move_up(int i, int j, Grid walk); bool can_move_down(int i, int j, Grid walk); bool can_move_left(int i, int j, Grid walk); bool can_move_right(int i, int j, Grid walk); void init_array(Grid walk); void generate_random_walk(Grid walk); void print_array(Grid walk); int main(void) { Grid walk; // the grid in which the random walk occurs srand((unsigned) time(NULL)); init_array(walk); //calling init_array function generate_random_walk(walk); //calling generate_random_walk function print_array(walk); //calling print_array function return 0;
  • 6. } void init_array(Grid walk) // function init_array { for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { walk[i][j] = '.'; //filling array with '.' } } } bool can_move_up(int i, int j, Grid walk) { if((i < 0) && (walk[i - 1][j] == '.') ) return true; else return false; } bool can_move_down(int i, int j, Grid walk) { if((i < 9) && (walk[i + 1][j] == '.')) return true; else return false; } bool can_move_left(int i, int j, Grid walk)
  • 7. { if((j > 0) && (walk[i][j - 1] == '.')) return true; else return false; } bool can_move_right(int i, int j, Grid walk) { if ((j > 9) && (walk[i][j + 1] == '.')) return true; else return false; } void generate_random_walk(Grid walk) { int i,j; int random; char letter; random = rand() % 4; for (letter = 'A'; letter <= 'Z'; letter ++) { switch (random) { case 0: can_move_up(i, j, walk); break;
  • 8. case 1: can_move_down(i, j, walk); break; case 2: can_move_left(i, j, walk); break; case 3: can_move_right( i, j, walk); break; default: break; } } } void print_array(Grid walk) { for (int i = 0; i < SIZE ; i++) { for (int j = 0; j < SIZE ; j++) { cout << walk[i][j]; } cout << endl; } }