1 / 25

Servers

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.

gayora
Download Presentation

Servers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Servers • rain.cise.ufl.edu • sand.cise.ufl.edu • shine.cise.ufl.edu • thunder.cise.ufl.edu • storm.cise.ufl.edu

  2. Variables Continued Getting Input & Operators

  3. Making calculations • We know the different data types • int • float • double • char • How do we transform data into results

  4. 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

  5. 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

  6. 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;

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

  8. Integer math – More Examples 0 20 4 20 120430938 3 • 20 % 5 • 20 / 5 * 5 • 24 % 5 • 24 / 5 * 5 • 1204309383 / 10 • 1204309383 % 10

  9. 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

  10. float Variables - II • Ranges • IEEE floating-point standard • e = 8, f = 23 • ±3.4×1038

  11. 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;

  12. 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

  13. 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

  14. 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

  15. 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

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

  17. ++M; M += 1; M++; Unary Operators • Unary plus / minus • + / - • Example: -a • Unary increment/decrement • ++ / -- M = M + 1;

  18. 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

  19. Arithmetic Operators • add: +, minus: -, multiply: *, divide: /, modulus: % • Parentheses (grouping): ( ) • Unary plus / minus • + • - • Unary increment/decrement • ++ • --

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

  21. Operator Return Types (z = x ? y)

  22. How do we get data • We know how to turn the data into “useful information” • How do we get the data from the user?

  23. 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

  24. 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

  25. 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.

More Related