250 likes | 482 Views
Servers. rain.cise.ufl.edu sand.cise.ufl.edu shine.cise.ufl.edu thunder.cise.ufl.edu storm.cise.ufl.edu. Variables Continued. Getting Input & Operators. Making calculations. We know the different data types int float double char How do we transform data into results.
E N D
Servers • rain.cise.ufl.edu • sand.cise.ufl.edu • shine.cise.ufl.edu • thunder.cise.ufl.edu • storm.cise.ufl.edu
Variables Continued Getting Input & Operators
Making calculations • We know the different data types • int • float • double • char • How do we transform data into results
Casting - Implicit x = 7 x = -3 y = 7.0 y = -65.0 c = ‘{‘ x = 55 • int x = 7.9; • int x = -3.9; • float y = 7; • float y = -65; • char c = 123; • int x = ‘7’ ASCII TABLE
Casting - Explicit x = 7 x = -3 y = 7.0 y = -65.0 c = ‘{‘ x = 55 • int x = (int)7.9; • int x = (int)-3.9; • float y = (float)7; • float y = (float)-65; • char c = (char)123; • int x = (int)‘7’ ASCII TABLE
result of y/z will be truncated Operations for int type • Declaration int x, y, z; • Assignment • y = 10; • z = 6; • Calculation • Plus: + • x = y + z; • Minus: - • x = y – z; • Multiply: * • x = y * z; • Divide: / • x = y / z; • Modulus • x = y % z;
Integer math - Examples 11 48 -8 3 0 2 4 0 1 7 4 -2 • 7 + 4 • 6 * 8 • 4 – 12 • 10 / 3 • 3 / 8 • 14 / 7 • 24 / 5 • 24 % 6 • 22 % 7 • 23 % 8 • 4 % 5 • -7 % 5
Integer math – More Examples 0 20 4 20 120430938 3 • 20 % 5 • 20 / 5 * 5 • 24 % 5 • 24 / 5 * 5 • 1204309383 / 10 • 1204309383 % 10
float: single-precision Variables • For values containing decimal 3., 125.8, -0.1 • Scientific notation 2.25e-3 = 2.25 * 10-3 = 0.00225 • Use e or E for exponent • no commas
float Variables - II • Ranges • IEEE floating-point standard • e = 8, f = 23 • ±3.4×1038
result of y/z will NOT be truncated Operations for float type • Declaration float x, y, z; • Assignment • y = 10.00; • z = 5.8; • Calculation • Plus: + • x = y + z; • Minus: - • x = y – z; • Multiply: * • x = y * z; • Divide: / • x = y / z;
Floating point math - Examples 11.5 52.0 -8.1 3.333 0.5 4.8 • 7.2 + 4.3 • 6.5 * 8.0 • 4.2 – 12.3 • 10.0 / 3.0 • 4.0 / 8.0 • 24.0 / 5.0
Mixed arithmetic - Examples 11.2 52.0 -8.3 3.333 0.5 4.8 • 7.2 + 4 • 6.5 * 8 • 4 – 12.3 • 10 / 3.0 • 4.0 / 8 • 24 / 5.0
Example – I #include <stdio.h> int main() { int a, b, c; float f; a = 10; b = 20; c = a/b; printf(“%i / %i = %i\n”, a, b, c); f = a/b; printf(“%i / %i = %f\n”, a, b, f); } 10 / 20 = 0 10 / 20 = 0.000000
Example – II #include <stdio.h> int main() { int a; float f, g, h; f = 10.0; g = 20.0; a = f/g; printf(“%f / %f = %i\n”, f, g, a); h = f/g; printf(“%f / %f = %f\n”, f, g, h); } 10.000000 / 20.000000 = 0 10.000000 / 20.000000 = 0.500000
count += 10; count -= 5; a = a / (b + c); Assignment Operators • Join the arithmetic operators • Format: op= • Examples: count = count + 10; count = count - 5; a /= b + c;
++M; M += 1; M++; Unary Operators • Unary plus / minus • + / - • Example: -a • Unary increment/decrement • ++ / -- M = M + 1;
Example – III #include <stdio.h> int main() { int m = 0; //m is 0 at this point printf(“m post increment: %i\n”, m++); //now m is 1 printf(“m pre increment : %i\n”, ++m); //now m is 2 } m post increment: 0 m pre increment : 2
Arithmetic Operators • add: +, minus: -, multiply: *, divide: /, modulus: % • Parentheses (grouping): ( ) • Unary plus / minus • + • - • Unary increment/decrement • ++ • --
Operator Precedence • Precedence • Operators with higher precedence are evaluated first • Operators with same precedence are evaluated from left to right • In decreasing precedence • ( ) • unary increment (++), unary decrement (--) • unary plus (+), unary minus (-) • multiply (*), divide(/), modulus(%) • add(+), minus(-) • assignment (=) • Order for • c = -a * b • a + b * c / d c =( * b ) (-a) (a + ) ( / d ) (b * c)
How do we get data • We know how to turn the data into “useful information” • How do we get the data from the user?
Getting Input • Need input from user • scanf • Same format as printf, except put “&” in front of variable names • scanf(“%i”, &count); • “&” means the "address of“ • to store whatever the user enters into the memory address where number is stored • Leaving out the & will cause your program to work incorrectly! • Exception: double uses %lf in scanf and %f in printf
Example-III #include <stdio.h> int main() { int x, y; printf("What is the value for x? \n"); scanf("%i", &x); //read the input //calculate (x-1)^2 + 10 y = (x-1)*(x-1) + 10; //print the output printf("The result is: %i\n", y); return 0; } What is the value for x? 7 The result is: 46
Example-IVHalf your age plus 7 #include <stdio.h> int main() { float age; printf(“How old are you?\n"); scanf("%f", &age); printf(“You can date someone %f years old or older.\n", 0.5 * age + 7); return 0; } How old are you? 26 You can date someone 20.000000 years old or older.