CGS 3460 Servers n rain.cise.ufl.edu n sand.cise.ufl.edu n shine.cise.ufl.edu n thunder.cise.ufl.edu n storm.cise.ufl.edu
CGS 3460 Variables Continued Getting Input & Operators
CGS 3460 Making calculations n We know the different data types lint lfloat ldouble lchar n How do we transform data into results
CGS 3460 Casting - Implicit n int x = 7.9; n int x = -3.9; n float y = 7; n float y = -65; n char c = 123; n int x = ‘7’ x = 7 x = -3 y = 7.0 y = c = ‘{‘ x = 55 ASCII TABLE
CGS 3460 Casting - Explicit n int x = (int)7.9; n int x = (int)-3.9; n float y = (float)7; n float y = (float)-65; n char c = (char)123; n int x = (int)‘7’ x = 7 x = -3 y = 7.0 y = c = ‘{‘ x = 55 ASCII TABLE
CGS 3460 Operations for int type n Declaration int x, y, z; n Assignment ly = 10; lz = 6; n Calculation lPlus: + x = y + z; lMinus: - x = y – z; lMultiply: * x = y * z; lDivide: / x = y / z; lModulus x = y % z; result of y/z will be truncated
CGS 3460 Integer math - Examples n n 6 * 8 n 4 – 12 n 10 / 3 n 3 / 8 n 14 / 7 n 24 / 5 n 24 % 6 n 22 % 7 n 23 % 8 n 4 % 5 n -7 %
CGS 3460 Integer math – More Examples n 20 % 5 n 20 / 5 * 5 n 24 % 5 n 24 / 5 * 5 n / 10 n %
CGS 3460 float: single-precision Variables n For values containing decimal 3., 125.8, -0.1 lScientific notation 2.25e-3 = 2.25 * = Use e or E for exponent lno commas
CGS 3460 float Variables - II n Ranges lIEEE floating-point standard e = 8, f = 23 ±3.4×10 38
CGS 3460 Operations for float type n Declaration float x, y, z; n Assignment ly = 10.00; lz = 5.8; n Calculation lPlus: + x = y + z; lMinus: - x = y – z; lMultiply: * x = y * z; lDivide: / x = y / z; result of y/z will NOT be truncated
CGS 3460 Floating point math - Examples n n 6.5 * 8.0 n 4.2 – 12.3 n 10.0 / 3.0 n 4.0 / 8.0 n 24.0 /
CGS 3460 Mixed arithmetic - Examples n n 6.5 * 8 n 4 – 12.3 n 10 / 3.0 n 4.0 / 8 n 24 /
CGS 3460 Example – I #include 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 =
CGS 3460 Example – II #include 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); } / = / =
CGS 3460 Assignment Operators n Join the arithmetic operators lFormat: op= n Examples: count = count + 10; count += 10; count = count - 5; count -= 5; a /= b + c; a = a / (b + c);
CGS 3460 Unary Operators n Unary plus / minus l+ / - lExample: -a n Unary increment/decrement l++ / -- M = M + 1; M += 1; ++M; M++;
CGS 3460 Example – III #include 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
CGS 3460 Arithmetic Operators n add: +, minus: -, multiply: *, divide: /, modulus: % n Parentheses (grouping): ( ) n Unary plus / minus l+ l- n Unary increment/decrement l++ l--
CGS 3460 c =( * b ) Operator Precedence n Precedence lOperators with higher precedence are evaluated first lOperators with same precedence are evaluated from left to right lIn decreasing precedence ( ) unary increment (++), unary decrement (--) unary plus (+), unary minus (-) multiply (*), divide(/), modulus(%) add(+), minus(-) assignment (=) n Order for lc = -a * b la + b * c / d (-a) (b * c) ( / d )(a + )
CGS 3460 Operator Return Types (z = x ? y) xyz int float intfloat intfloat
CGS 3460 How do we get data n We know how to turn the data into “useful information” n How do we get the data from the user?
CGS 3460 Getting Input n Need input from user lscanf 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
CGS 3460 Example-III #include int main() { int x, y; printf("What is the value for x? \n"); scanf("%i", &x); //read the input //calculate (x-1)^ 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
CGS 3460 Example-IV Half your age plus 7 #include 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 years old or older.