top of page

Workshop 2- Week beginning 18th October 2021

  • Sadie Read
  • Oct 20, 2021
  • 7 min read

-Step 1

Copied the code into visual studios. It saved the number that I entered a variable and then printed that variable


-Step 2

Step 2 was about adding an if statement and wanted us to boundary test edge cases. This means using the numbers closet to the boundary of what we are testing to ensure that our code worked properly. I was able to do this testing numbers 4 and 5 and 6. 4 printed the that the number was under 5 while 5 and 6 printed that the number was 5 or more showing that the code works. I created the if statement without looking at my notes to try and test myself and my memory. I got everything corrected apart from the placement of the curly brackets but a scan of my notes then helped me to correct that


-Step 3

Step 3 was about changing the if statement around the number 10 that says if the number is greater or less than 10. I changed the if statement so that number 10 was the boundaries.


-Step 4

Step 4 was about editing the code with more parameters and conditionals. It included showing a number that was equal to 7, more than 7 or less than 7. This was really easy to do in an if statement with the operators.


int main()

{

int n;

cout << "please enter a number" << endl;

cin >> n;

cout << "your number was: " << n << endl;

if (n == 7)

{

cout << "Your is exactly 7" << endl;

}

else if (n < 7)

{

cout << "Your number is less than 7" << endl;

}

else

{

cout << "Your number was more than 7" << endl;

}

}


-Step 5

Step 5 was about comparing to integers to each other and printing out which one is bigger. This was really simple to do as it required an extra variable and an extra Cin and then changing the if statements.


int main()

{


int n;

int y;

cout << "please enter your first number: " << endl;

cin >> n;

cout << "Please enter your second number: " << endl;

cin >> y;

cout << "Your first number was " << n << ". Your second number was " << y << endl;



if (n == y)

{

cout << "Your numbers are the exact same" << endl;

}

else if (n < y)

{

cout << n << " is smaller than " << y << endl;


}

else

{

cout << n << " is greater than " << y << endl;

}

}


-Step 6

Step 6 was about creating a switch case statement printing out the words of numbers up until 8. Switch cases are fairly simple but I did have to go over my notes quickly to ensure I knew exactly what I was doing. Example of code:


int z;

cout << "please enter a number: " << endl;

cin >> z;

switch (z)

{

case 1:

cout << "One" << endl;

break;

case 2:

cout << "Two" << endl;

break;

default:

cout << "Something else" << endl;

break;

}


-Task 7

I had to create x and y co-ordinates, setting their value to 0


-Task 8

I completed task 8 using 2 inputs and 2 different if statements for the X and Y co-ordinates. I then had to test boundary cases of 9,10,11 and 19,20,21 for the x co-orientate to ensure that it followed the rules of my statement and the way I wanted it to flow. I then boundary tested 4,5,6 and 24,25.26 for my Y co-ordinate. All my boundaries worked perfectly.


-Task 9

Task 9 was about using Pythagoras theorem to determine the distance that 2 players were from each other.

I started by creating code for both players to enter their x and y co-ordinates and then stored them in variables.

Next was about programming the calculations. First was about finding a using the Y co-ordinates. This needed to be done by finding out which co-ordinate is bigger and taking away the smallest from the largest. This will be done by using a series of if statements. The same will then be done to the X co-ordinates. They then need to be multiplied by themselves This all worked perfectly. I then also used equations to figure out the squaring of numbers by multiplying them to themselves and then use the sqrt() function to square the numbers. A float might have been better to have used here to get a more precise number but as we are only looking for a whole number for the last part of this task, integers are better to work with for now


#include <iostream>

#include<string>

#include<math.h>

#include<cmath>

using namespace std;


int main()

{

int a = 0;

int b = 0;


int player1_X = 0;

int player1_Y= 0;

cout << "Player 1! Enter you X co-ordinate; " << endl;

cin >> player1_X;

cout << "Player 1! Enter you Y co-ordinate; " << endl;

cin >> player1_Y;

int player2_X = 0;

int player2_Y = 0;

cout << "Player 2! Enter you X co-ordinate; " << endl;

cin >> player2_X;

cout << "Player 2! Enter you Y co-ordinate; " << endl;

cin >> player2_Y;

if (player1_Y > player2_Y)

{

a = (player1_Y - player2_Y);

//t a = (a * a);

}

else if (player2_Y > player1_Y)

{

a = (player2_Y - player1_Y);

//nt a = (a * a);

}

else

{

cout << "An error had occured" << endl;

a = 0;

}

int a_sq = (a*a);


if (player1_X > player2_X)

{

b= (player1_X - player2_X);

//ut << b << endl;

//t b = (b * b);

}

else if (player2_X > player1_X)

{

b = (player2_X - player1_X);

//ut << b << endl;

//t b = (b * b);

}

else

{

cout << "An error had occured" << endl;

b = 0;


}

int b_sq = (b * b);


int c = (a_sq + b_sq);


int answer = sqrt(c);

cout << "c is: " << answer << endl;

if (answer <= 2)

{

cout << "You are too close. You must move away" << endl;

}

else

{

cout<<"You guys are ready to play!!"<<endl;

}


-Task 10

I used operators to create this loop and boundary tested it with 0,1,2 and 9,10,11. It works perfectly and is a nested loop.


int main()

{

bool okay = false;

int answer = 0;

while (okay == false)

{

cout << "Please enter a number between 1-9" << endl;

cin >> answer;


if (answer <= 9&&answer >= 1)

{


cout << "Well done you can follow instructions" << endl;

okay = true;

}


else

{

okay = false;


cout << "please follow the insturctions. I'm not asking for much here." << endl;

}


}

}


-Task 11

Started by initialised x and y co-ordinates to 5 and declaring the character variable to #

After this I used my notes to put the code together. I started by writing out exactly what needed to include and started with the if statements for boundaries. I then added the loop and then the cin for the movement and then the switch cases and then the if statement for when the player moves outside the boundaries. I had some issues with the loop constantly looping and then realised I had forgotten curly brackets so that was an easy fix. I then had to play around with he switch cases to get them to work and remembered they were characters not variables so need the single quotation mark. I then also had to figure out where the fail system if statement needed to go. It had to go under the switch case statements but in the loop. This was so that it was the last thing checked in the loop otherwise the loop would fail on 12 and not 11.

To improve this code, I can be more memory friendly my using i++ rather than i=i+1. I can also look at adding more features like enemies on area squares. This would be easy to add by either using my numbers so the enemies spawn in the same place every time or by using a random number generator to keep players on their toes.

In my code as well I need to keep adding comments so that in a few months time when I come back to look at it I understand my thought process and what's going on.


#include <iostream>

#include<string>

using namespace std;


int main()

{

int player_x = 5;

int player_y = 5;

char charcter = '#';

bool in_playable_zone = true;

if (player_x >= 1 && player_x <= 10 && player_y >= 1 && player_y <= 10)

{

cout << "Lets play" << endl;

}

else

{

cout << "The game is over!" << endl;

}


while (in_playable_zone == true)


{

if (player_x==1)

{



cout << "You are very close to the edge. Move east. " << endl;

}


else if (player_x == 10)

{

cout << "You are very close to the edge. Move west. " << endl;


}

else if (player_y == 1)

{

cout << "You are very close to the edge. Move north. " << endl;

}

else if (player_y == 10)

{

cout << "You are very close to the edge. Move south. " << endl;


}

cout << "your current postion is x= " << player_x << " y= " << player_y << endl;

cout << "would you like to move North (n), South (s), East (e) or west (w)\nI would like to move: " << endl;

cin >> charcter;

switch (charcter)

{

case ('n'):

player_y = player_y + 1;

break;

case ('s'):

player_y = player_y - 1;

break;

case ('e'):

player_x = player_x + 1;

break;

case ('w'):

player_x = player_x - 1;

break;

default:

cout << "Enter the lower case intital of which direction you want to move in." << endl;

cout << "would you like to move North (n), South (s), East (e) or west (w)\nI would like to move: " << endl;



}

if (player_x > 10 || player_x < 1 || player_y>10 || player_y < 1)

{

cout << "You have fallen off the edge.The Game is over" << endl;

in_playable_zone = false;

}


}


}


Recent Posts

See All

Comments


Post: Blog2_Post
  • Twitter
  • YouTube

©2020 by Sadie Read. Proudly created with Wix.com

bottom of page