Question

C++ Homework on Loop Structure A treasure is hidden someplace – the treasures coordinates are (x1,...

C++

Homework on Loop Structure

A treasure is hidden someplace – the treasures coordinates are (x1, y1)! The coordinates (x1, y1) are determined randomly, using the code that is listed at the end of this exercise. The purpose of the game is for the Explorer to find the Treasure!
The explorer is allowed to go North, South, West or East by one step each time. The Explorer is first positioned at location (15,15). If the explorer goes North by one step, the Explorer’s y coordinate is increased by 1. If the explorer goes South by one step, the y coordinate is decreased by 1. In the same fashion the x coordinate is increased by 1 if the explorer goes East by one step and the x coordinate is decreased by 1 if the explorer goes West by one step.
Each time the Explorer ‘moves’, the distance between the Explorer and the treasure is computed. The formula for the distance between (x, y) and (x1, y1) is
distance = sqrt(static_cast<double>((x-x1) *(x-x1) + (y-y1)
* (y-y1)));
//static_cast<double> in this statement is for data type
//conversion from int to double.
When the Explorer’s position is the same as the Treasure’s position, the Explorer found the Treasure!
Procedures
1. Ask the user to move: Please enter direction (n,s,e,w).
2. Update the Explorer’s coordinates.
3. Calculate and display the distance from the Explorer to the Treasure (this
information will clue the Explorer to either keep going in the same direction or
switch directions).
4. At the end of each loop display the Explorer’s coordinates.
5. Make sure that you print out how many moves it took (number of times the loop
ran) to reach the treasure.
6. Use the starter code below, and see hints below the starter code to help with
developing code
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
  
using namespace std;
int main () {
int x=15, y=15;
int x1, y1;
char direction;
double distance;
bool treasure=false;
srand(time(0));
function !
x1=rand( ) % 30 + 1;
and 30
y1=rand( ) % 30 + 1;
and 30
// Explorer’s coordinates
// Treasure’s coordinates
//write loop to find the treasure
HINT: When developing the code, print out the coordinates of the treasure right after the rand() function is called. This is to help you test your program. Once the code works well, comment or remove those "peeking" statements.
// secretly seed the rand
// set X1 to random between 1
// set y1 to random between 1

Homework Answers

Answer #1

The code snippet for the the problem is given below with all the functions properly commented.

The code uses a do-while loop when the variable treasure = false, the control enters the loop and calculates the distance given by the formula. Now in nested if-else statements user input is evaluated and the coordinates are printed after every input. Finally if the distance is zero than the treasure is found.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;

int main()
{
        int x = 15, y = 15;             // Explorer’s coordinates
        int x1, y1;             // Treasure’s coordinates
        int counter = 0; //<-- added counter
        char direction;
        double distance;
        bool treasure = false;

        srand(time(0));
        x1 = rand() % 30 + 1;
        y1 = rand() % 30 + 1;

        do
        {
                distance = sqrt(static_cast<double>((x - x1)*(x - x1) + (y - y1)*(y - y1)));
                cout << "Enter enter a direction to travel" << endl;
                cin >> direction;

                if (direction == 'n' || direction == 'N')
                        y += 1;
                else if (direction == 's' || direction == 'S')
                        y -= 1;
                else if (direction == 'e' || direction == 'E')
                        x += 1;
                else if (direction == 'w' || direction == 'W')
                        x -= 1;
                
                cout << "Your coordinates are now: " << endl;
                cout << "X-Coordinate: " << x << endl;
                cout << "Y-Coordinate: " << y << endl;
                cout << "You are " << distance << " coordinates away from the treasure" << endl;

                counter++;//<-- steps count up

                if (distance == 0)
                {
                        treasure = true;
                        cout << "Treasure Found" << endl;
                        return 0;
                }
        } while (treasure == false);

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML...
Homework Draw class diagrams for your HW4 - the Tetris Game shown below: Part 1: UML As a review, Here are some links to some explanations of UML diagrams if you need them. • https://courses.cs.washington.edu/courses/cse403/11sp/lectures/lecture08-uml1.pdf (Links to an external site.) • http://creately.com/blog/diagrams/class-diagram-relationships/ (Links to an external site.) • http://www.cs.bsu.edu/homepages/pvg/misc/uml/ (Links to an external site.) However you ended up creating the UML from HW4, your class diagram probably had some or all of these features: • Class variables: names, types, and...
JAVA Bike shares are becoming increasingly common in cities in the United States. Commuters have been...
JAVA Bike shares are becoming increasingly common in cities in the United States. Commuters have been using bike sharing as a method of transportation for the flexibility, cost savings, exercise, and a myriad of other benefits. While being quite successful, many bike shares are still relatively new and have room for optimization. To help with the optimization process, we want to analyze bike share data from the city of Los Angeles. The first step of the process is to collect...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
Two waves traveling in opposite directions on a stretched rope interfere to give the standing wave...
Two waves traveling in opposite directions on a stretched rope interfere to give the standing wave described by the following wave function: y(x,t) = 4 sin⁡(2πx) cos⁡(120πt), where, y is in centimetres, x is in meters, and t is in seconds. The rope is two meters long, L = 2 m, and is fixed at both ends. In terms of the oscillation period, T, at which of the following times would all elements on the string have a zero vertical...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated...
IN JAVA Speed Control Problem: The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated panel) with a circle that moves on the panel and rebounds from the edges. (NOTE: the program is derived from Listing 8.15 and 8.16 in the text. That program uses an image rather than a circle. You may have used it in an earlier lab on animation.) The Circle class is in the file Circle.java. Save the program to your directory and run it...
Hi there, I've been asked to write a program in C which can read values from...
Hi there, I've been asked to write a program in C which can read values from a file then sort them, and then write to a binary file. I'm getting stuck when I write my binary file as the output is just spitting out garbage values and not the values that are being read in. When I print my input file reader everything is perfect but after sorting and then writing, the output is completely wrong. I have checked that...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT