Active Logic & Mathematical C Programming
In Lab 2, we bridge real-world math formulas into C code expressions. Master integer division truncation, modulus remainder operator, floating point division, and <math.h> functions!
Essential C Formula Rules
| Operator / Function | C Syntax | Key Rule & Trap |
|---|---|---|
| Integer Division | a / b | Truncates decimal! 7 / 2 = 3 (Not 3.5) |
| Modulus (Remainder) | a % b | Only works on integers! 7 % 2 = 1 |
| Float Division | 5.0 / 9.0 | Writing 5 / 9 in C yields 0! Use floats. |
| Square Root | sqrt(x) | Header: #include <math.h> |
| Power Function | pow(base, exp) | Header: #include <math.h> |
đŻ Learning Philosophy: Active Logic Building
Instead of copy-pasting code, each experiment in Lab 2 features 3 Interactive Learning Modes:
- đ§ Tab 1 (Logic & Hints): Understand the math formula & step-by-step pseudo-code.
- đ§© Tab 2 (Logic Puzzle): Fill in missing operators/variables & run live simulations.
- đ» Tab 3 (C Solution): Verify your answer against verified C source code.
To Convert Seconds into Hours, Minutes & Seconds
Given total seconds S, extract whole hours using S / 3600, remaining minutes using (S % 3600) / 60, and leftover seconds using S % 60.
Mathematical Formula Breakdown
1 Hour = 3600 seconds1 Minute = 60 seconds- Hours:
H = total_sec / 3600(Integer division truncates decimal) - Remaining Sec after Hours:
rem = total_sec % 3600 - Minutes:
M = rem / 60 - Leftover Seconds:
S = rem % 60
Interactive Logic Hints
3665 / 3600 gives 1 (whole hour). It automatically drops the .018 decimal part!3665 % 3600 gives 65 (the seconds left over after removing 1 full hour). Then 65 / 60 = 1 min and 65 % 60 = 5 sec.đ§© Complete the Missing Formula Logic
Fill in the missing C operators/constants below and test your logic:
rem = total_sec 3600;
m = rem / ;
s = rem 60;
â±ïž Live Time Converter Simulator
To Find the Sum of Digits of a Number
Extract digits from right to left: digit = n % 10 isolates the last digit, and n = n / 10 drops it.
Digit Peel Algorithm (Base 10)
- Extract Last Digit:
1234 % 10 = 4 - Accumulate:
sum = sum + digit(0 + 4 = 4) - Remove Last Digit:
1234 / 10 = 123(Integer truncation drops .4) - Repeat loop until
n == 0!
Interactive Logic Hints
5 % 10 = 5 (extracts 5), then 5 / 10 = 0. Loop condition n > 0 becomes false and loop stops cleanly!đ§© Complete the Digit Extraction Code
digit = temp ;
sum = sum digit;
temp = temp 10;
}
đą Visual Step-by-Step Digit Trace
Temperature Converter (°F ↔ °C)
Beware of integer division trap! Writing 5 / 9 in C evaluates to 0. You must use floating-point literal 5.0 / 9.0.
Conversion Formulas
- Fahrenheit to Celsius:
C = (F - 32) * 5.0 / 9.0 - Celsius to Fahrenheit:
F = (C * 9.0 / 5.0) + 32 - Format Specifier for 2 decimals:
%.2f
Interactive Logic Hints
(F - 32) * (5 / 9), C evaluates integer division 5 / 9 = 0, making Celsius result ALWAYS 0.00!5.0 / 9.0 (with decimal point). This forces C to perform floating-point division (0.5556).đ§© Complete the Temperature Calculation
// Choose correct float constants:
C = (F - 32.0) * ;
đĄïž Dual Temp Simulator
Distance Between Two Points (x1, y1) & (x2, y2)
Uses Euclidean distance formula d = √((x2 - x1)2 + (y2 - y1)2) with <math.h> functions sqrt() and pow().
Euclidean Distance Formula
- Header required:
#include <math.h> - Square Root:
sqrt(x) - Power:
pow(x, 2)computes x2 - Expression:
dist = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2))
Interactive Logic Hints
(x2 - x1) * (x2 - x1) is faster than pow() because it avoids library function call overhead.double (with %lf in scanf) for highest precision when calculating square roots!đ§© Complete Distance Calculation Code
#include <>
dist = ((x2 - x1, 2) + pow(y2 - y1, 2));
đ Coordinate Distance Calculator
Area & Circumference of a Circle
Compute Area = πr2 and Circumference = 2πr using constant macro #define PI 3.14159.
Circle Formulas
- Macro Constant:
#define PI 3.14159265 - Area:
area = PI * r * r - Circumference:
circumference = 2 * PI * r
Interactive Logic Hints
PI in your code with 3.14159265 before compilation!đ§© Complete the Circle Geometry Equations
float r = 7.0, area, circum;
area = PI * ;
circum = * PI * r;
â Circle Geometry Calculator
Swap Two Variables (With & Without 3rd Variable)
Compare standard variable swap using temp vs arithmetic swap without third variable.
Swapping Methods
- Method 1 (Temp Var):
temp = a; a = b; b = temp; - Method 2 (Arithmetic):
a = a + b; b = a - b; a = a - b;
Interactive Logic Hints
a = 30. Then b = 30 - 20 = 10. Then a = 30 - 10 = 20. Result: swapped!đ§© Complete Arithmetic Swap Equations
a = a b; // a becomes 30
b = a b; // b becomes original a (10)
a = a b; // a becomes original b (20)