Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conversion Check your class notes and given examples at class.

Similar presentations


Presentation on theme: "Conversion Check your class notes and given examples at class."— Presentation transcript:

1 Conversion Check your class notes and given examples at class.

2 Floating point Check your class notes and given examples at class.

3 Floating point IEEE 754.

4 Choose the biased exponent
(32-bits)(floating point numbers IEEE-754) -18 25 Binary to decimal(32-bit-floating point IEEE-754)

5 Conversion Decimal number to signed integer(32-bit)(2-compliment for negative numbers) -48 118 64 Binary to decimal

6 Placeholders in I/O Statements
Placeholders (or conversion specifiers) will substitute the value of the variable inside the output string (printf). You must absolutely match the placeholder with the variable type. %lf: long floating point (double). %d: decimal (int). %c: character (char). \n: a special character meaning that a new line will be inserted.

7 Integer placeholders %d is the default integer placeholder. When used it will simply display the value as is without any padding. To add padding, to have columns for example, we need formatted placeholders. %nd will reserve n places to display the number. Justification will be to the right. The negative sign takes one place. If the value is 17 and %4d is used, then it will display 2 spaces followed by 17 on the screen. __17 These are spaces, not underscores.

8 Integer placeholders The number is always displayed in its entirety, even when the format is too narrow. With and a %3d placeholder, you would see -1234, therefore using 5 spaces instead of the 3 requested. A negative number change the justification to the left of the field. With a value of and a %-8d placeholder, you will get -1234___ . 3 trailing blanks

9 Double placeholders By default the %lf (or %f) placeholder displays the number with 6 decimal digits and no padding (may vary depending of computer system). The formatted double placeholder has this format: %w.dlf, where w is the total width of the field (including sign and decimal point) and d the number of decimal digits. If the value is 4.56 and the placeholder is %6.3lf then the display will be _ leading blank

10 Double placeholders With a double formatted, you always get the requested number of decimal digits (even if the field is not wide enough). You also always (like the integer placeholder) get all the significant numbers. However, if there are more decimal precision in the value than in the placeholder, the value is truncated and rounded-up if need be.

11 Double placeholders If the value is and the placeholder is %7.3lf, then you will get 3 decimal digits as requested plus the significant numbers: If the value is and the placeholder is %8.3lf, then you will get 3 decimal digits as requested plus the significant numbers and padding: _ See the rounding-up effect. A %8.7lf for a value of will produce a display of Note: The internal value is unaffected by the placeholder, only its screen appearance.

12 Double placeholders By default the %lf (or %f) placeholder displays the number with 6 decimal digits and no padding (may vary depending of computer system). The value is 5.87 and the placeholder is %6.3lf then the display will be b5.870 If the value is and the placeholder is %7.3lf ,then you will have 3 decimal digits and the display will be: If the value is and the placeholder is %8.3lf ,then you will have: b ROUNDING-UP effect. Also you could have TRUNCATE. A %8.7lf will produce: Note!!!!!! The internal value is UNAFFECTED by the placeholder, only its screen appearance.

13 Simple Assignment Operator (=)
Assign a value to a variable Does not mean equality, it means assignment Var1='a'; /* Var1  a */ Var2=15; /* Var2 15 */ Var3=27.62; /* Var3  */

14 Initializing Variable
Giving a value to the variable at the time that the variable is declared. char Var1='X'; int Var2=1095; float Var3= ;

15 Scanf() Another way to fill a variable is to ask the user for its value. We do that with the scanf statement. printf(“Enter your score:”); Scanf(“%d”,&score);

16

17 /. The = operator puts the value on the right. / /
/* The = operator puts the value on the right */ /* into the variable on the left */ #include <stdio.h> int main (void) {/* declarations */ int a, b, c, d, e; a = 10; /* fill variable a */ /* modify variable a few times*/ a = 20; a = 10 + a; a = a + a + 2; a = 2 + a; /* a few more assignments */ b = a; c = b = 5; c = 10 + a; d = a + a + 2; e = 20 + a; a = a - b + c; /* the final values are... */ printf ("a:%4d\n b:%4d\n c:%4d\n d:%4d\n e:%4d\n", a, b, c, d, e); return (0); }

18 /* Numeric Placeholders */
#include <stdio.h> int main (void) { /* declarations */ int a; double x; /* executable statements */ a = 1000; x = ; printf ("%d\n", a); printf ("%3d\n", a); printf ("%4d\n", a); printf ("%5d\n", a); printf ("\n"); printf ("%f\n", x); printf ("%15f\n", x); printf ("%15.4f\n", x); printf ("%18.2f\n", x); printf ("%12.0f\n", x); return (0); }

19 Answer

20 Write a program which reads seven characters
Q Write a program which reads seven characters

21 /* This program reads 7 characters */
/* This program reads seven characters */ #include <stdio.h> int main (void) { /* declarations */ char letter1, letter2, letter3, letter4,letter5,letter6,letter7; printf ("Enter a name: "); scanf ("%c%c%c%c%c%c%c", &letter1, &letter2, &letter3, &letter4,&letter5, &letter6, &letter7); printf ("You entered: %c%c%c%c%c%c%c", letter1, letter2, letter3, letter4,letter5,letter6,letter7); return (0); }

22 scanf( ) function To get input from the user.
Defined in <stdio.h> e.g., scanf("%f", &var); /* get a value for var from the user */ %f tells the program the type of the data &var the address of var in memory. scanf("%f %d %c", &var1, &var2, &var3); /* get values for var1, var2, var3 */ /* var1 is float */ /* var2 is integer */ /* var3 is character */

23 Week 3

24 NOTE for scanf( ): scanf( ) does not use the variable name. It uses the variable address. The value entered by the user at the keyboard. Never put the &sign infront of a variable in a printf!

25 Examples Partial ‘C’ Code  … printf(“This is a C Message.”);
printf("What appears bet. The double quote is printed as message on screen.") Output on the Screen  This is a C Message. What appears bet. The double quote is printed as a message on screen. Note: there are 2 printf statements but messages are not printed on 2 separate lines. They are printed one after each other.                                

26 Examples Partial ‘C’ Code  …
printf(“This is also a \”C\” Message!\n”); printf(“Number 5 is equal to %d”, 5); Output on the Screen  This is also a “C” Message! Number 5 is equal to 5. Note: printing a numerical constant; %d is replaced by the constant to be printed. Note:using \” will cause a double quote to be printed directly to the screen.

27 Examples Partial ‘C’ Code  …
printf(“Number %d is not equal to %d.”, 5, 7); Output on the Screen  Number 5 is not equal to 7. Note: printing 2 numerical constants; first %d is replaced by the 1st constant to be printed and the second %d is replaced by the 2nd constant to be printed.                                

28 Examples Partial ‘C’ Code  …
printf(“This is how to print a character, for example: 'a' is %c.”, 'a'); printf("This is how to print a string, for example: CPS125 is %s.", "CPS125“); Output on the Screen  This is how to print a character, for example: 'a' is a. This is how to print a string, for example: CPS125 is CPS125.

29 Examples Partial ‘C’ Code  …
printf("We can also print a combination of numbers, %d, characters, %c, and strings, %s.", 25, 'X‘ , "CMTH140"); Output on the Screen  We can also print a combination of numbers, 25, characters, X, and CMTH140. NOTE: arguments are separated by commas.                                

30 Examples Partial ‘C’ Code  … int iNTvar=5; float fLOATvar=10.3;
char cHARvar=’X’; printf(“This is how to print different variables, for example: iNTvar is %d, fLOATvar is %f, cHARvar is %c.”, iNTvar, fLOATvar, cHARvar); Output on the Screen  This is how to print different variables, for example: iNTvar is 5, fLOATvar is 10.3, and cHARvar is X.                                

31 Example Partial ‘C’ Code  … Output on the Screen  int iNTvar;
float fLOATvar; char cHARvar; printf(“This is how to print different variables, for example: iNTvar is %d, fLOATvar is %f, cHARvar is %c.”, iNTvar, fLOATvar, cHARvar); Output on the Screen  This is how to print different variables, for example: iNTvar is -108, fLOATvar is , and cHARvar is a.

32 Format specifiers for PRINTF( ) functions
Purpose %d, %i Signed decimal integer %f Signed floating point %lf Signed double %e Signed floating point using e notation %c A single character %s Strings

33 Variables Variable like a memory cell, can only contain one value at a time. Putting a value in a variable that contains another value destroys the previous value. Putting certain values in a variable can lead to inaccuracies Cancellation error: happens when the magnitude of the operands are too different. Ex: would give Arithmetic underflow: happens when a number too small appears as 0. Ex: * would give 0.0

34 Question? Q) What happens if we try to print a variable which is declared but is not initialized (i.e., we didn’t assign any value to it)? Can we assume that if the variable is integer then ZERO will be printed out?

35 Answer if we declare variables and don’t assign values to them, then try to print them, we would see unexpected values on the screen.

36 Arithmetic Operators in C
Denote Example What it does + Addition 2+8 Add numbers & return the result 2+8 returns 10 - Subtraction 12-8 Subtract numbers & return the result 12-8 returns 4 * Multiplication 5*8 Multiply numbers & return the result 5*8 returns 40 / Division 40/5 Divide numbers & return the result 40/5 returns 8 % Remainder (modulo) 7%3 Divide numbers & return the remainder 7%3 Returns 1 NOTE: Unary operation is an operation that ONLY requires one operand, for example, -2.

37 CPS125 Week 2

38 Placeholders in I/O Statements
Placeholders (or conversion specifiers) will substitute the value of the variable inside the output string (printf). You must absolutely match the placeholder with the variable type. %lf: long floating point (double). %d: decimal (int). %c: character (char). \n: a special character meaning that a new line will be inserted.

39 Format specifiers for PRINTF( ) functions
Purpose %d, %i Signed decimal integer %f Signed floating point %lf Signed double %e Signed floating point using e notation %c A single character %s Strings

40 Scanf() Another way to fill a variable is to ask the user for its value. We do that with the scanf statement. printf(“Enter your score:”); Scanf(“%d”,&score);

41 /* Numeric Placeholders */
#include <stdio.h> int main (void) { /* declarations */ int a; double x; /* executable statements */ a = 1000; x = ; printf ("%d\n", a); printf ("%3d\n", a); printf ("%4d\n", a); printf ("%5d\n", a); printf ("\n"); printf ("%f\n", x); printf ("%15f\n", x); printf ("%15.4f\n", x); printf ("%18.2f\n", x); printf ("%12.0f\n", x); return (0); }

42 Answer

43 Write a program which reads seven characters
Q Write a program which reads seven characters

44 /* This program reads 7 characters */
/* This program reads seven characters */ #include <stdio.h> int main (void) { /* declarations */ char letter1, letter2, letter3, letter4,letter5,letter6,letter7; printf ("Enter a name: "); scanf ("%c%c%c%c%c%c%c", &letter1, &letter2, &letter3, &letter4,&letter5, &letter6, &letter7); printf ("You entered: %c%c%c%c%c%c%c", letter1, letter2, letter3, letter4,letter5,letter6,letter7); return (0); }

45 scanf( ) function To get input from the user.
Defined in <stdio.h> e.g., scanf("%f", &var); /* get a value for var from the user */ %f tells the program the type of the data &var the address of var in memory. scanf("%f %d %c", &var1, &var2, &var3); /* get values for var1, var2, var3 */ /* var1 is float */ /* var2 is integer */ /* var3 is character */

46 NOTE for scanf( ): scanf( ) does not use the variable name.
It uses the variable address. The value entered by the user at the keyboard. Never put the &sign infront of a variable in a printf!

47 Examples Partial ‘C’ Code  … printf(“This is a C Message.”);
printf("What appears bet. The double quote is printed as message on screen.") Output on the Screen  This is a C Message. What appears bet. The double quote is printed as a message on screen. Note: there are 2 printf statements but messages are not printed on 2 separate lines. They are printed one after each other.                                

48 Examples Partial ‘C’ Code  …
printf(“This is also a \”C\” Message!\n”); printf(“Number 5 is equal to %d”, 5); Output on the Screen  This is also a “C” Message! Number 5 is equal to 5. Note: printing a numerical constant; %d is replaced by the constant to be printed. Note:using \” will cause a double quote to be printed directly to the screen.

49 Examples Partial ‘C’ Code  …
printf(“Number %d is not equal to %d.”, 5, 7); Output on the Screen  Number 5 is not equal to 7. Note: printing 2 numerical constants; first %d is replaced by the 1st constant to be printed and the second %d is replaced by the 2nd constant to be printed.                                

50 Examples Partial ‘C’ Code  …
printf(“This is how to print a character, for example: 'a' is %c.”, 'a'); printf("This is how to print a string, for example: CPS125 is %s.", "CPS125“); Output on the Screen  This is how to print a character, for example: 'a' is a. This is how to print a string, for example: CPS125 is CPS125.

51 Examples Partial ‘C’ Code  …
printf("We can also print a combination of numbers, %d, characters, %c, and strings, %s.", 25, 'X‘ , "CMTH140"); Output on the Screen  We can also print a combination of numbers, 25, characters, X, and CMTH140. NOTE: arguments are separated by commas.                                

52 Examples Partial ‘C’ Code  … int iNTvar=5; float fLOATvar=10.3;
char cHARvar=’X’; printf(“This is how to print different variables, for example: iNTvar is %d, fLOATvar is %f, cHARvar is %c.”, iNTvar, fLOATvar, cHARvar); Output on the Screen  This is how to print different variables, for example: iNTvar is 5, fLOATvar is 10.3, and cHARvar is X.                                

53 Example Partial ‘C’ Code  … Output on the Screen  int iNTvar;
float fLOATvar; char cHARvar; printf(“This is how to print different variables, for example: iNTvar is %d, fLOATvar is %f, cHARvar is %c.”, iNTvar, fLOATvar, cHARvar); Output on the Screen  This is how to print different variables, for example: iNTvar is -108, fLOATvar is , and cHARvar is a.

54 Format specifiers for PRINTF( ) functions
Purpose %d, %i Signed decimal integer %f Signed floating point %lf Signed double %e Signed floating point using e notation %c A single character %s Strings

55 Variables Variable like a memory cell, can only contain one value at a time. Putting a value in a variable that contains another value destroys the previous value. Putting certain values in a variable can lead to inaccuracies Cancellation error: happens when the magnitude of the operands are too different. Ex: would give Arithmetic underflow: happens when a number too small appears as 0. Ex: * would give 0.0

56 Question? Q) What happens if we try to print a variable which is declared but is not initialized (i.e., we didn’t assign any value to it)? Can we assume that if the variable is integer then ZERO will be printed out?

57 Answer if we declare variables and don’t assign values to them, then try to print them, we would see unexpected values on the screen.

58 CPS125 Week 2

59 Arithmetic Operators in C
Denote Example What it does + Addition 2+8 Add numbers & return the result 2+8 returns 10 - Subtraction 12-8 Subtract numbers & return the result 12-8 returns 4 * Multiplication 5*8 Multiply numbers & return the result 5*8 returns 40 / Division 40/5 Divide numbers & return the result 40/5 returns 8 % Remainder (modulo) 7%3 Divide numbers & return the remainder 7%3 Returns 1 NOTE: Unary operation is an operation that ONLY requires one operand, for example, -2.

60 Assignment Operators A simple assignment operator or a combination of assignment operator (=) with some other arithmetic operator, Example, count=count + 1; /* count  count+1 */ count += 1; /* count  count+1 */

61 Assignment Operators Operator Denote Example Operation = Assignment
X=X+2 += Addition Assignment X+=2; X=X+2; -= Subtraction Assignment X-=2; X=X-2; *= Multiplication Assignment X*=2; X=X*2; /= Division Assignment X/=2; X=X/2; %= Remainder Assignment X%=2; X=X%2; NOTE: The choice of using either X+=2 or X=X+2; depends on the individual programming practice. Both assignments are correct thus you would use the method that is most convenient to you.

62 Increment/Decrement Operators
Increment and decrement operators are unary operators and they can be in the form of postfix or prefix. Operation Of Increment and Decrement Operators Operator Operation Counter++ (postfix) Do the current operation then increment Counter ++Counter (prefix) increment Counter then do the current operation Counter-- Do the current operation then Decrement Counter --Counter

63 Example: Partial Code #1 Partial Code #2 Partial Code #3 …
int count=0; count=count+1; count++; ++count; Partial Code #4 Partial Code #5 Partial Code #6 int w=0, x=2, y=3, z; x = x + y++; z = y + ++x ... int count=0, x=2, y=3, z; x = x + ++y; z = y + x++; ++y; x++; x = x + y; z = y + x;

64 Multiplication (*): 5*10 or 0.6*3.4 Division (/): 50.0/2.0 or 45/2
Arithmetic operators Addition (+): 3+4 or Subtraction (-): or Multiplication (*): 5*10 or 0.6*3.4 Division (/): 50.0/2.0 or 45/2 Remainder (%): Also called modulus Ex: 30%7 is 2, 45%3 is 0, 23%77 is 23. % works only with integers! 64

65 Precedence of Arithmetic Operations
Priority of arithmetic operations over each other Operations are performed from left to right based on the precedence Order Of Arithmetic Operations Priority Operation HIGH LOW ( ) *, / +, - Example: 1 + 8 / 2  5 ( ) / 2  4 1 + 8 / 9 *  -9 Note: Integer divided by another integer results in an integer result

66 Expressions Integer expressions
Expressions containing only integers are called integer expressions. The result of an integer expression is always an integer. This is particularly important for the division operator. For example, 5/2 is an integer division and will give 2,not 2.5. There is never a rounding up of values. 99/100 will give 0 not 1. Now that we know about integer division, we find that a%b is the same as a - (a / b) * b. Double expressions Expressions containing only doubles are called double expressions. The result of an double expression is always a double. For example 5.0/2.0 is a double division and will give 2.5. 99.0/100.0 will give 0.99. Mixed expressions Expressions containing doubles and integers are called mixed expressions. The result of a mixed expression is always a double. For example 5/2.0 or 5.0/2 is a mixed division and will give 2.5. 35*2.0 will give 70.0. 66

67 Evaluating expressions
Rule #1: Parentheses rule: All parentheses must be evaluated first from the inside out. Rule #2: Operator precedence rule: 2.1 Evaluate unary operators first. 2.2 Evaluate *, / and % next. 2.3 Evaluate + and – next. Rule #3: Associativity rule: All binary operators must be evaluated left to right, unary operators right to left. 67

68 Square root Square roots in C are computed with a special function taken from a special library: the math library. To use that library, we need to include the proper header file: #include <math.h> The square root function is called sqrt and is used by calling it this way: sqrt (x) where x is the number we wish to know the square root of. We can put that answer in another variable y=sqrt(x); x sqrt y Math functions Math functions can be integrated in other C statements and expressions. All math functions use doubles. z = a + sqrt (b-c); printf (“The square root of %lf is %lf”, x, sqrt(x));

69 Other math functions y=floor (x): the largest whole number <= x.
If x is 3.7, y will be 3.0. If x is -14.2, y will be y=ceil (x): the smallest number >= x. If x is 3.7, y will be 4.0. If x is -14.2, y will be y=log(x): finds the natural log of x (ln). y=log10(x): finds the decimal log of x (log). y=fabs(x): finds the absolute value of x. sin(x), cos(x), tan(x) and arctan(x) are trig functions giving the sine, cosine, tangent and arctangent of an angle expressed in radians (not degrees). radians = degrees * PI / 180 y = exp (x): gives e to the power of x. z = pow (x,y): gives x to the power of y. atan(x): calculate the arc tangent of a real number giving an angle expressed in radians.

70 Other functions Other functions can be found in the standard library (also need to #include <stdlib.h>). Be careful! b=abs(a): gives the absolute value of an integer. n = rand(): will give a random integer number between 0 and RAND_MAX, a predefined constant macro. To find the value of RAND_MAX on your computer just try this: printf (“%d”, RAND_MAX);

71 Shortcut operators a=a/2; may be shortened to a/=2;
x=x*5; may be shortened to x*=5; a=a/2; may be shortened to a/=2; i=i+1; may be shortened to i+=1; Since adding and subtracting 1 is very common, there is a shorter version still. i=i+1; may be shortened to ++i; i=i-1; may be shortened to - -i; Increment and decrement ++i is called an increment, --i a decrement. i++ and i-- can also be used. There is no difference between the prefix ++i and postfix i++ forms as far as the value of i is concerned. But is an assignment is used, there is a difference. In b=++i; i is incremented and the answer is then placed into b. In b=i++, the value of i is placed in b and then i is incremented.

72 Practice! Mixed Type Arithmetic
#include <stdio.h> void main(void) { int i=1, j=1, k1=10, k2=20, k3=30, k4=40, k5=50, k, h, m, n; float a=7, b=6, c=5, d=4, e, p, q, x, y, z; printf("Before increment, i=%2d, j=%2d\n",i,j); k=i++; h=++j; printf("After increment, i=%2d, j=%2d\n“ k=%2d, h=%2d \n\n",i,j,k,h); m=6/4; p=6/4; n=6/4.0; q=printf("m=%2d, p=%3.1f\nn=%2d, q=%3.1f\n\n",m, p, n, q); printf("Original k1=%2d, k2=%2d, k3=%2d, k4=%2d, k5=%2d\n“, k1,k2,k3,k4,k5); k1 += 2; k2 -= i; k3 *= (8/4); k4 /= 2.0; k5 %= 2; printf("New k1=%2d, k2=%2d, k3=%2d, k4=%2d, k5=%2d\n\n", k1,k2,k3,k4,k5); e= 3; x= a + b -c /d *e; y= a +(b -c) /d *e;z=((a + b)-c /d)*e; printf("a=%3.0f, b=%3.0f, c=%3.0f\nd=%3.1f, e=%3.1f\n\n“, a,b,c,d,e); printf("x= a + b -c /d *e = %10.3f \n“ "y= a +(b -c) /d *e = %10.3f \n" "z=((a + b)-c /d)*e = %10.3f\n", x,y,z); } 72

73 How to Access a Memory Location?
Either use the corresponding address Or Declare a variable and use the variable name Variable Specific memory location Set aside for specific kind of data Has a name for reference Has an address for reference 73 73

74


Download ppt "Conversion Check your class notes and given examples at class."

Similar presentations


Ads by Google