Create a for loop that counts from 0 to 10 by an increment of 1
for (int i = 0; int <= 10; int++) { cout << i << ", "; i++;}
Alternative version in a while loop
int i = 0;while (i <= 10) { cout << i << ", "; i++; // OR i += 1 OR i = i + 1}
indefinite loop
What happens to this while loop code?
int i;while (true) { cout << i << endl; i++;}
Answer: The loop runs infinitely (indefinite)
multiple if statements inside an if
If I run this code, what happens?
// Checking divisibility by 2, 3, 5;int y = 12;if (y % 2 == 0) { cout << y << " is divisible by 2" << endl; if (y % 3 == 0) { cout << y << " is divisible by 3" << endl; }}
Answer:
It passes through the first if statement if (y % 2 == 0) because 2 % 2 == 0, which makes the condition true
After the first if statement, it runs the code inside that if statement (lines 5-8).
Line 5: It prints 12 is divisible by 2
Line 6: it goes to the second if statement if (y % 3 == 0)
Since 12 % 2 == 0 is true, line 7 prints 12 is divisible by 3
Git sample problems
Commits alternative name
Professor Angelo refer commits as milestones (friendly version of calling them records for every change).
What are commits?
Commits are basically records about your changes that you made to the source file, which often includes a short commentary.
Initial commit is a commit message btw
Explain the diff (difference) changes in this file
Keynote
Explain the differences that are highlighted in bold
Example 1
Answer: Line 8 was modified when i = i + 2 is changed to i += 2 . A commentary was added after the first brace in {, which defined the similarity between i += 1 and i = i + 2
Example 2
Note: Even though the code is not in C++, you should be able to explain the code difference
Answer:
In line 67 and 78, the description changed Presents a message to to Removes and with supplied message to disguises
In Line 70 and 81, target was replaced as otherPlayer.
In Line 71 and 82, target:SetAttribute("DisplayName", args[2]) was changed to otherPlayer:disguiseAsPlayer(0)
Understanding Interactive Code Rewind
Commit
|< - Goes to the first commit of the current branch from the repository
< - Goes to the previous commit
> - Goes to the next commit
>| - Goes to the latest commit of the current branch
Git Checkout (if you see detached HEAD)
If you reached the latest commit (notice that >| is greyed out), you must checkout the git repository (simply means that you go back to the main branch, not commit_hash branch)
You will notice that commit_hash branch is just a branch with that only commit, not the main branch that has the latest commits of all source codes. It’s obvious when you see this on the bottom left of your Theia IDE:
How to time travel between commits (milestones)
Answer: Using the left/right arrow buttons to navigate between commits.
Freeform problem
Student Grades Logger
Write a C++ program that keeps track of current grade for a class:
For each assignment, print out the grade received based on the number of points received and total points then store it.
Make sure to ask the user for these values!!
After knowing the number of points received and total points, print out the class grade
If the user wants to finish logging, have them input -1 to end the logger loop
Once the loop is ended, print out a message that says that the logger is done
Solution
#include <iostream>using namespace std;double sumOfAllReceivedPoints = 0, sumOfAllTotalPoints = 0;int main() { // Display message cout << "Assignment Logger (tracks your current grade of the class)" << endl; while (true) { double pointsReceived, pointsMax; cout << endl << "---" << endl; // Divider for each interaction cout << "How many points did you receive on an assignment? [enter -1 to exit] "; cin >> pointsReceived; if (pointsReceived == -1) break; if (pointsReceived < 0) continue; cin.clear(); cin.ignore(); cout << "How many points is your assignment worth? "; cin >> pointsMax; if (pointsMax <= 0) continue; cout << "You received " << ((pointsReceived/pointsMax)*100) << "\% on the assignment" << endl; sumOfAllReceivedPoints += pointsReceived; sumOfAllTotalPoints += pointsMax; cout << "Your current class grade: " << ((sumOfAllReceivedPoints/sumOfAllTotalPoints)*100) << "%" << endl; } cout << "Logger ended" << endl; }