340 likes | 467 Views
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.
E N D
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 0 2 1.5 sign exponent mantissa
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
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);
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; }
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; }
Conversion Specifiers : Field Width • denotes a blank space
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
Conversion Specifiers : Field Width double a=-123.4561; printf("a=cs\n",a);
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
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)
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;
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; }
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");}
General Syntax if(condition) { statements } or if(condition){ statements } else { statements } no ; at the end
Example cout << "Enter two numbers"; cin >> i; cin >> j; if(i == j){ cout << "Numbers are equal"; } else { cout << "Numbers are different"; }
Bad use cout << "Enter two numbers"; cin >> i; cin >> j; if(i == j) cout << "Numbers are equal"; else cout << "Numbers are different";
= 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)
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"; } }
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.
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
Example: print the alphabet #include <iostream> using namespace std; void main(){ char c = 'a'; while(c<='z'){ cout << c << " "; c = c+1; } }
Boolean variables bool is_less_than; is_less_than = ( i< n); if(is_less_than){ … }
Fun with ++ • What is the result of: i= 5; cout << i++ + ++i; cout << i;
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.
Logical Operators Exercise: Assume a=1 and b=2
Example cout << "What is your age?"; int age; cin >> age; if((age >= 13) && (age <= 19)){ cout << "But you are just a teenager!"; }
Prime number test bool isPrime(int n){ int i=2; while(i<=sqrt((double)n)){ if(n%i == 0){ return false; } i++; } return true; }