Computer Memory
-
RAM: Random Access Memory
Stores data temporally for use. -
Bit:
“Binary Digit” –The smallest unit of information on a machine…
(Single bit holds 0 or 1) -
Byte:
1 byte contains 8 bits.
Types of Variable:
Declaring Variables & Variable Assignment:
Notice:
age = 18 means set age to 8 (from right to left)
y=x=5
is x=5 -> y=x (this style is better to debugger)
Debugger (on Watch Window)
Place a breakpoint
Press F5 to runs the program with the debugger (orange status bar at the bottom)
The name & value & type of variable be showed on the watch window (down-left)
Press F10 let the compile run the 6th line and watch window will change with it
(Run time can also be showed on the right of line)
Modulus: %
a % b
example: 7 % 2 means divide 7 by 2 and keep remainder (1)
If b > a just keep left (a):
Shorthand (Reduce the frequency of using memory)
f = f + 5 -> f += 5 f = f + 1 -> f++
f = f – 5 -> f -= 5 f = f - 1 -> f–
f = f * 5 -> f *= 5
f = f / 5 -> f /= 5
Prefix & Postfix
Int h = g++ ( assign the value of g to h, then post-increment happen)
Int j = ++i ( pre- increment happen, then assign the value of i to j)
Output the value (printf)
Add type of value and the name of variable on printf (do not miss the comma)
Literals (Constants)
A fixed value that cannot change during the execution of the program
sizeof
Use sizeof to check the type of the variable and how many bytes of it.
(To calculate the size of these type)
Practice:
Requirements
Write a program that reads a Fahrenheit degree in as a real number, and then converts the input into Celsius. The program must then display the resulting temperature as Celsius.
Design
Printf(“…. converter”)=>printf(“pls enter….”)=>declare variables=>scanf(“temperature”)=> declare Celsius=>Celsius = (5 ÷ 9) x (Fahrenheit – 32)=> printf (……same as….“)=>printf(“….degrees Celsius”)
Implementation
Testing
Errors Encountered
Failure
Debugging
Forgot to use address
Because the %10f .There is no need to have %10f (compile will keep numbers after point),
Should use 5.0f, because if there are two integers in division, compile will only keep integer result
Lessons Learnt:
if there are two integers in division, compile will only keep integer result
do not forgot to use address on scanf.
%f is typically enough for float.