Variables, types, and remembered values
Choose a representation, initialize it, and understand what assignment really changes.
A name, a type, and a current value
A variable is a named storage location. A declaration introduces its name and type; initialization gives its first value. Assignment replaces that current value. Read score = score + 5 as “calculate the old score plus five, then store the result.”
Copying a variable copies its value at that moment. It does not create a live connection. In this program, b stays 5 when a changes to 9. The output is 9 5.
#include <iostream>
int main() {
int a = 5;
int b = a;
a = 9;
std::cout << a << ' ' << b << '\n';
return 0;
}Choose from the constraints
Use int for suitably bounded whole numbers, long long for larger integers or totals, double for fractional values, char for one character such as 'A', and bool for a true/false fact. Text such as "Ahmed" is a string, which you will study in Session 2.
In the training environment, a 32-bit int holds roughly −2.1 billion to 2.1 billion, while a 64-bit long long holds roughly −9.2 × 10¹⁸ to 9.2 × 10¹⁸. C++ itself only guarantees at least 16 bits for int and at least 64 for long long. Choose for the largest intermediate calculation, not merely the inputs.
Floating-point values are usually approximations. Keep exact counts as integers. A bool normally prints as 1 or 0; use a condition when the required answer is YES or NO.
Initialization, constants, and scope
An ordinary local int without initialization does not have a dependable starting zero. Initialize a sum to 0 and a “found something” flag to false. Use meaningful names; names cannot begin with a digit or contain spaces, and score differs from Score.
const int passMark = 50 gives a meaningful name to a value that should not change. A name declared inside a block is normally available from that declaration until the block ends. Declare a result before a block if you need it afterward.
Optional recognition: short, long, float, long double, and unsigned types also exist. Do not assume long is always 64 bits or make every value unsigned simply because the inputs are nonnegative.
Variables & operators
These inputs represent C++ int values. Integer division discards the fractional part.
Change a and b to compare integer division with remainder.
Guided simulation · Change the inputs to test your understanding.
After int score = 10; int saved = score; score += 5;, what are score and saved?
Turn the idea into code
Two inputs can each be as large as 1,000,000,000. Choose types and print their sum and product. Explain why choosing only from the individual inputs would be misleading.
Give me a hint
The product can reach 10¹⁸. Make the multiplication itself use long long.
Show the explained solution
Read both values into long long. Their product is then computed with the wider type, and the maximum allowed product fits a signed 64-bit integer.
#include <iostream>
int main() {
long long a = 0, b = 0;
std::cin >> a >> b;
std::cout << a + b << ' ' << a * b << '\n';
return 0;
}Your notes
Mark complete when you can explain it yourself.