1 / 34

Chapter 2

Chapter 2. Decisions, Decisions. Representation of Data Types. Integer format: Floating point format: Moral of the story: you can't store arbitrary size integers in a computers int: -2^31 upto 2^31 (total of 2^32 different values = 4 bytes). 150. 0. sign.

alayna
Download Presentation

Chapter 2

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. Chapter 2 Decisions, Decisions

  2. Representation of Data Types • Integer format: • Floating point format: • Moral of the story: • you can't store arbitrary size integers in a computers • int: -2^31 upto 2^31 (total of 2^32 different values = 4 bytes) 150 0 sign 0 2 1.5 sign exponent mantissa

  3. Data conversions double x; x = 3; //OK: converts from double to int double x=3.2; int y = x; // problem: can't convert double // to int int y =(int)x; // OK, chops off the decimal // part Rule of thumb: need explicit cast when possible lost of data

  4. Standard Output: printf Function • Must have #include <stdio.h> • printf(format, expr-1, expr-2,……); • examples: • printf("%d",3); • printf("%f",3.233); • pintf("%c",'s'); • printf("%s","abcd"); • printf("%d kilometers is equal to %d miles",i,j);

  5. Different Types

  6. scanf -> opposite of printf #include <stdio.h> int main() { char alphabet; printf("Enter any alphabet: \n"); scanf("%c", &alphabet); printf("You entered %c \n", alphabet); return 0; }

  7. Sometimes you need to use flushall() #include <iostream> #include "math.h" using namespace std; int main(){ char ch; int d; printf("enter number\n"); scanf("%d",&d); //flushall(); printf("enter character\n"); scanf("%c",&ch); printf("%d\t%c\n",d,ch); return 0; }

  8. Conversion Specifiers : Field Width •  denotes a blank space

  9. Conversion Specifiers : Field Width • If the field width is omitted for floating-point values, a default of 6 is used for floating-point numbers to specify the precision of the value printed

  10. Conversion Specifiers : Field Width double a=-123.4561; printf("a=cs\n",a);

  11. Conversion Specifiers : Field Width

  12. Escape Character • The backslash (\) is called an escape characterwhen it is used in a control string • printf("\"What\’s up\?\"\a\a\n"); • "What’s up?"and you will hear two beeps • Reference p.54 in the textbook for more examples

  13. Mathematical Functions • In order to use the pre-defined math functions in the Standard C library, we should include #include <math.h> • Syntax return_type function name (arguments ) • Most of the math functions assume that the arguments are double • Example sin(theta), sqrt(x*x+y*y),log10(x)

  14. Mathematical Functions

  15. Mathematical Functions /* This C code computes the length of one side */ /* of a triangle with the given lengths of the */ /* two other sides and angle between them. */ /* Since we will use cosine and sqrt function */ /* math.h must be included. */ #include <stdio.h> #include <math.h> #define PI 3.141593 int main () { /* Declare variables */ double side1, side2, ang_deg; double ang_rad, side3;

  16. Mathematical Functions /* Input from keyboard */ printf("Enter the length of one side of triangle \n"); scanf("%lf",&side1); printf("Enter the length of one other side of triangle \n"); scanf("%lf",&side2); printf("Enter the angle between the two sides in degrees \n"); scanf("%lf",&ang_deg); /* Convert degrees into radians */ ang_rad = ang_deg * (PI / 180); /* Compute the unknown length */ side3=sqrt(side1*side1+side2*side2-2*side1*side2*cos(ang_rad)); /* Display output on the screen */ printf("The length of the remaining side of" " the triangle is %5.3lf \n", side3); /* Exit */ return 0; }

  17. Structured C Programming • Selection algorithm contains • A condition that is evaluated as "true" (non-zero) or "false" (0) • Instructions for what to do based upon the evaluation result of the condition int your_age; printf("Enter your age \n"); scanf("%d",&your_age); if (your_age < 21){ printf("Under! \n"); } else { printf("Over! \n");}

  18. General Syntax if(condition) { statements } or if(condition){ statements } else { statements } no ; at the end

  19. Example cout << "Enter two numbers"; cin >> i; cin >> j; if(i == j){ cout << "Numbers are equal"; } else { cout << "Numbers are different"; }

  20. Bad use cout << "Enter two numbers"; cin >> i; cin >> j; if(i == j) cout << "Numbers are equal"; else cout << "Numbers are different";

  21. = and == are different! • Use of = (the assignment operator) • int i; • i = 3; • Note: that = operator returns the assignment value! • e.g. cout << (a=3); works • Use of == (the comparison operator) • compares two numbers • returns true (i.e., 1 if equal) • returns false (i.e., 0 if not equal)

  22. Example: Odd or Even? #include <iostream> #include "string.h" using namespace std; void main(){ cout << "Enter a number: "; int i; cin >> i; if(i%2 == 0){ cout << "Your number is even"; } else { cout << "Your number is odd"; } }

  23. The % operator • Returns the reminder when two numbers are divided. • Examples: • 10%3 = 1 • 8 % 5 = 3 • 99% 5 = 4 • In other words, what number needs to be subtracted to get a number that is divisible by the second argument.

  24. While loop • syntax: while(condition){ statements; } • example: while( grass is yellow && not raining){ water grass; } • important: all loops in C continue executing until the condition becomes FALSE • if condition is true, then execute the loop again

  25. Example: print the alphabet #include <iostream> using namespace std; void main(){ char c = 'a'; while(c<='z'){ cout << c << " "; c = c+1; } }

  26. Comparison operators

  27. Boolean variables bool is_less_than; is_less_than = ( i< n); if(is_less_than){ … }

  28. Shorthands

  29. Fun with ++ • What is the result of: i= 5; cout << i++ + ++i; cout << i;

  30. Statement vs. Expression • statement: ends with ; • example: • cout << i++ << " "; • expressions: • x • x+=3 • num++ • i=num+2 • You can convert an expression into a statement by following it with a semicolon.

  31. Boolean logic

  32. Logical Operators Exercise: Assume a=1 and b=2

  33. Example cout << "What is your age?"; int age; cin >> age; if((age >= 13) && (age <= 19)){ cout << "But you are just a teenager!"; }

  34. Prime number test bool isPrime(int n){ int i=2; while(i<=sqrt((double)n)){ if(n%i == 0){ return false; } i++; } return true; }

More Related