Workshop 1- Week of October 11th 2021
- Sadie Read
- Oct 13, 2021
- 6 min read
-Started by playing around with the basic hello world. Used /n to see how it breaks down the words and displays them on different lines
-Also played around with spacing out the words by using << which prints the word on the same line as no /n is used
-Task 1
1. Replace the string with a character, for example '@'. Remember that characters have single quotes around them rather than double quotes.
Prints @symbol to console
2. Now replace it with an integer, such as 5. Remember that integers are whole numbers with no quote marks.
Prints the integer 5
3. Now replace it with a floating-point number such as 23.6f.
Prints the float 23.6
4. Now replace it with the Boolean value, true. Run the code - does it do what you expect?
Expecting it to say True
It prints 1. True in coding can also be represented as the binary of 1 as it is “on”
5. Now replace it with the Boolean value, false, but before you run the code, try and guess what it will display. Were you right?
Expecting it to say 0 as false is represented as 0 in binary
I was corrected it says 0
6. Finally, change the code back so it uses the string "hello world!" again.
-Task 2
-int X;
-x=5;
- This is better written as in x=5; as this saves space in the memory and is easier to identify when there’s hundreds of lines of code. Can be printed to console using cout statements
Now edit your code so it looks like this:
int x = 5;
int y = 10;
std::cout << x << y << std::endl;
Guess what you think might happen before you run the code. Was the result as you expected?
Without using any more "cout" statements, or adding any more lines of code, see if you can edit the existing lines of code to get the values of x and y to appear on different lines of the console.
-Going to print on the same line with no spaces
- To print them on new lines I will edit the code as followed
int x = 5;
int y = 10;
std::cout << x << “\n”<< y << std::endl;
-I was correct this prints them on separate lines
-Task 3
Edit your code so it looks like this:
int x = 5;
int y = 10;
int z = (x + y);
std::cout << "the value of z is " << z << std::endl;
Build the code to make sure you have typed everything properly. One it builds successfully, have a guess at what you think might happen before you run the code. Then run the code and see if you were right.
-I think that Z will output 15, Bidmas and such is important to know that brackets come first. X+Y is simply 5+10 so it should be 15.
-I was correct
-Task 4
Replace the code you wrote in the previous step with the following:
int x = 3;
std::cout << "x is now " << x << std::endl;
x = 7;
std::cout << "x is now " << x << std::endl;
Save, build and run the code.
Note that the value of x changes between the two "cout" statements, so first it says x = 3 but then it says x = 7.
During each of the "cout" statements we display the current value of x. The first time we still had the initial value of 3, but then we reassigned it with a different value (7) before the second "cout" statement. In other words, we made a change in the data, so we needed to visualise that piece of data again (via a second "cout" statement) so we could actually observe the change happening. If we don't revisualize our game data regularly, the player has no way of seeing if things have changed during the game.
-TASK 4
Edit your code so it looks like this:
int x = 3;
std::cout << "x is now " << x << std::endl;
x = (x + 2);
std::cout << "x is now " << x << std::endl;
Save, build and run the code. The value of x changes again, but this time we are reassigning it a new value that is relative to its current value.
Basically, we are saying "set the value of x to be whatever it is now, plus 2". The value of x after this line will depend on the value it had before this line. In the example above, x begins with the value of 3, so it ends up with a value of two more than that, which is 5. However, if we had initialised it with some other value, say 45, it would end up as 47.
Edit your code to give x an different initial value and observe how the final value is always 2 more.
-I gave X the value of 10 and it became 12
Replace the line that says:
x = (x + 2);
With this:
x = (x * 2);
Run the code again. What has changed?
-Using 10 still from when I changed the value of X this becomes 20 as * is used to multiply
We can use the signs + - / * to add, subtract, divide and multiply values in variables.
Change the line again so it now says: x = (x / 2); But before you run the code, make sure x is initialised with an odd number. Have a guess at what might be outputted. Was it what you were expecting? If not, is there anything in the lecture slides or videos that might explain this?
-Replace the line that says: x = (x + 2); With this: x = (x * 2); Run the code again. What has changed? We can use the signs + - / * to add, subtract, divide and multiply values in variables. Change the line again so it now says: x = (x / 2); But before you run the code, make sure x is initialised with an odd number. Have a guess at what might be outputted. Was it what you were expecting? If not, is there anything in the lecture slides or videos that might explain this?
-Changing 10 to 3 meant that the division would 3 divided by 2. We know this is 1.5 however the computer has to used whole numbers for the integer so the output should be 1. If we were to use a float, then 1.5 would have been shown.
- I was correct
-TASK 5
Add a line to your code so it looks like this:
char myCharacter = 'd';
myCharacter = (myCharacter + 1);
std::cout << myCharacter << std::endl;
Can you predict/explain the output?
-Its going to take the ascii value of a lower case d and add 1 to it. I would assume this would be presented as e in the cout
-I was correct
- I played around with ascii values adding numbers to their value and printing them to console as characters to see what charters held which values.
-Next was a focus on naming variables as x,y and z are not appropriate as we learnt in our lecture this week. Therefore, we need to pick a suitable name that does not contain spaces or start with a number, and we must remember they are case sensitive. I will now continue to name my variables number to see that a number is being placed here. As its only me using this code to practice the naming is less important but it’s good practice. If multiple people where to be working on this code it would need to be something more descriptive with the use of comments for them to understand how this variable is used in the code
- The next task looked at shorting lines of code to make it easier to read and to require less memory. This is a short hand form on how to do simple addition, subtraction, multiplication and division. It also showed a short hand form on how to add or subtract by 1. This will be helpful when creating games that need a counter to add or subtract by 1.
-TASK 6
-Using the lecture slides as guidance I was tasked with creating an array of 5 player scores, setting the value of each score to zero. The array name is player_score. An easy to understand naming system
- I then continued to build arrays with different data types and printing them out. I used an array and sorted each string “toga”, “is”, “awesome”. I removed the endl to get these to print all on the same line to spell the sentence. I added a space at the end of toga and is to ensure that it was spaced correctly and to make it seem I had just printed it as a cout string to the console.
Comments