Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 CGS 3460 Variables Continued Getting Input & Operators

3 CGS 3460 Making calculations n We know the different data types lint lfloat ldouble lchar n How do we transform data into results

4 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 = -65.0 c = ‘{‘ x = 55 ASCII TABLE

5 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 = -65.0 c = ‘{‘ x = 55 ASCII TABLE

6 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

7 CGS 3460 Integer math - Examples n 7 + 4 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 % 5 11 48 -8 3 0 2 4 0 1 7 4 -2

8 CGS 3460 Integer math – More Examples n 20 % 5 n 20 / 5 * 5 n 24 % 5 n 24 / 5 * 5 n 1204309383 / 10 n 1204309383 % 10 0 20 4 120430938 3

9 CGS 3460 float: single-precision Variables n For values containing decimal 3., 125.8, -0.1 lScientific notation 2.25e-3 = 2.25 * 10 -3 = 0.00225 Use e or E for exponent lno commas

10 CGS 3460 float Variables - II n Ranges lIEEE floating-point standard e = 8, f = 23 ±3.4×10 38

11 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

12 CGS 3460 Floating point math - Examples n 7.2 + 4.3 n 6.5 * 8.0 n 4.2 – 12.3 n 10.0 / 3.0 n 4.0 / 8.0 n 24.0 / 5.0 11.5 52.0 -8.1 3.333 0.5 4.8

13 CGS 3460 Mixed arithmetic - Examples n 7.2 + 4 n 6.5 * 8 n 4 – 12.3 n 10 / 3.0 n 4.0 / 8 n 24 / 5.0 11.2 52.0 -8.3 3.333 0.5 4.8

14 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 = 0.000000

15 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); } 10.000000 / 20.000000 = 0 10.000000 / 20.000000 = 0.500000

16 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);

17 CGS 3460 Unary Operators n Unary plus / minus l+ / - lExample: -a n Unary increment/decrement l++ / -- M = M + 1; M += 1; ++M; M++;

18 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

19 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--

20 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 + )

21 CGS 3460 Operator Return Types (z = x ? y) xyz int float intfloat intfloat

22 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?

23 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

24 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)^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

25 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 20.000000 years old or older.


Download ppt "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."

Similar presentations


Ads by Google