Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC113 Algorithms and Programming Techniques

Similar presentations


Presentation on theme: "ITEC113 Algorithms and Programming Techniques"— Presentation transcript:

1 ITEC113 Algorithms and Programming Techniques
C programming: Variables, Expressions part II Shaded text wrapped around a corner (Basic) To reproduce the effects on this slide, do the following: On the Insert tab, in the Text group, click Text Box, and then on the slide, drag to draw the text box. On the Home tab, in the Slides group, click Layout, and then click Blank. Enter text in the text box, select the text, and then on the Home tab, in the Font group, select Haettenschweiler from the Font list, and then select 24 from the Font Size list. On the Home tab, in the Paragraph group, click Center to center the text on the slide. Select the text box. Under Drawing Tools, on the Format tab, in the WordArt Styles group, click Text Effects, point to Transform, and then under Warp click Triangle Up (first row, third option from the left). Drag the pink diamond adjustment handle (at the left side of the text box) to adjust the amount of text warp. Select the text. Under Drawing Tools, on the Format tab, in the WordArt Styles group, click the arrow next to Text Fill, point to Gradient, and click More Gradients. In the Format Text Effects dialog box, click Text Fill in the left pane, select Gradient fill in the Text Fill pane, and then do the following: In the Type list, select Linear. Click the button next to Direction, and then click Linear Right (first row, fourth option from the left). In the Angle box, enter 0°. Under Gradient stops, click Add or Remove until five stops appear in the drop-down list. Also under Gradient stops, customize the gradient stops that you added as follows: Select Stop 1 from the list, and then do the following: In the Stop position box, enter 0%. Click the button next to Color, and then under Theme Colors click Blue, Accent 1, Lighter 60% (third row, fifth option from the left). Select Stop 2 from the list, and then do the following: Click the button next to Color, and then under Theme Colors click Blue, Accent 1, Lighter 80% (second row, fifth option from the left). In the Stop position box, enter 17%. Select Stop 3 from the list, and then do the following: In the Stop position box, enter 50%. Click the button next to Color, and then under Theme Colors click White, Background 1 (first row, first option from the left). Select Stop 4 from the list, and then do the following: In the Stop position box, enter 51%. Click the button next to Color, and then under Theme Colors click Blue, Accent 1, Lighter 40% (fourth row, fifth option from the left). Select Stop 5 from the list, and then do the following: In the Stop position box, enter 100%. Under Drawing Tools, on the Format tab, in the WordArt Styles group, click Text Effects, point to Reflection, and then under Reflection Variations click Tight Reflection, touching (first row, first option from the left). Click the button next to Color, and then under Theme Colors click Blue, Accent 1, Darker 25% (fifth row, fifth option from the left). To reproduce the background on this slide, do the following: Right-click the slide background area, and then click Format Background. In the Format Background dialog box, click Fill in the left pane, select Gradient fill in the Fill pane, and then do the following: Click the button next to Direction, and then click Linear Down (first row, second option from the left). Under Gradient stops, click Add or Remove until two stops appear in the drop-down list. In the Stop position box, enter 64%. Click the button next to Color, and then under Theme Colors click Black, Text 1 (first row, second option from the left).

2 Contents Data Types of Arithmetic Expressions Relational Expressions
Logical Expressions Multiple Assignments Compound Assignment Operators Shaded text boxes with arrows (Intermediate) To reproduce the top shape with text effects on this slide, do the following: On the Home tab, in the Slides group, click Layout, and then click Blank. On the Home tab, in the Drawing group, click Shapes, and then under Rectangles, click Rounded Rectangle (second option from the left). On the slide, drag to draw a rounded rectangle. Select the rectangle. Under Drawing Tools, on the Format tab, in the Size group, do the following: In the Shape Height box, enter .52”. In the Shape Width box, enter 3.75”. Drag the yellow diamond adjustment handle (at the top left of the rectangle) to the right to increase the amount of rounding at the corners of the rectangle. Select the rectangle. On the Home tab, in the bottom right corner of the Drawing group, click the Format Shape dialog box launcher. In the Format Shape dialog box, click Fill in the left pane, select Gradient fill in the Fill pane, and then do the following: In the Type list, select Linear. Click the button next to Direction, and then click Linear Down (first row, second option from the left). Under Gradient stops, click Add or Remove until two stops appear in the drop-down list. Also under Gradient stops, customize the gradient stops that you added as follows: Select Stop 1 from the list, and then do the following: In the Stop position box, enter 0%. Click the button next to Color, and then under Theme Colors click White, Background 1 (first row, first option from the left). Select Stop 2 from the list, and then do the following: In the Stop position box, enter 100%. Click the button next to Color, and then under Theme Colors click White, Background 1, Darker 15% (third row, first option from the left). Also in the Format Shape dialog box, click Line Color in the left pane, select Gradient Line in the Line Color pane, and then do the following: Click the button next to Direction, and then click Linear Up (second row, second option from the left). Under Gradient stops, click Add or Remove until two stops appear in the drop-down list. Also under Gradient stops, customize the gradient stops that you added as follows: Click the button next to Color, and then under Theme Colors click White, Background 1 (first row, first option from the left). Click the button next to Color, and then under Theme Colors click White, Background 1, Darker 25% (fourth row, first option from the left). Also in the Format Shape dialog box, click Line Style in the left pane, and then in the Line Style pane, in the Width box, enter 2 pt. On the Home tab, in the Drawing group, click Shape Effects, and then do the following: Point to Glow, and then under Glow Variations click any option in the first row to set a 5 pt glow. Point to Glow, point to More Glow Colors, and then under Theme Colors click White, Background 1, Darker 25% (fourth row, first option from the left). On the slide, right-click the rounded rectangle, click Edit Text, then enter text. Select the text. On the Home tab, in the Font group, select Franklin Gothic Medium Cond from the Font list, select 24 from the Font Size list, and then click the arrow next to Font Color and under Theme Colors click White, Background 1, Darker 35% (fifth row, first option from the left). On the Home tab, in the Paragraph group, click Align Text Left to align the text left in the text box. Under Drawing Tools, on the Format tab, in the bottom right corner of the WordArt Styles group, click the Format Text Effects dialog box launcher. In the Format Text Effects dialog box, click Text Box in the left pane. In the Text Box pane, under Internal margin, enter 0.6” in the Left box to increase the left margin in the rounded rectangle to accommodate the embossed circle. To reproduce the olive-green circle and arrow for the top shape on this slide, do the following: On the Home tab, in the Drawing group, click Shapes, and then under Basic Shapes click Oval (first row, second option from the left). Press and hold SHIFT to constrain the shape to a circle, and then on the slide, drag to draw a circle. Select the circle. Under Drawing Tools, on the Format tab, in the Size group, do the following: In the Shape Height box, enter .4”. In the Shape Width box, enter .4”. On the Home tab, in the bottom right corner of the Drawing group, click the Format Shape dialog box launcher. In the Format Shape dialog box, click Fill in the left pane. In the Fill pane, select Solid Fill, and then click the button next to Color and under Theme Colors click Olive Green, Accent 3, Lighter 60% (third row, seventh option from the left). Also in the Format Shape dialog box, click Line Color in the left pane, and then select No line in the Line Color pane. Also in the Format Shape dialog box, click Shadow in the left pane, and then in the Shadow pane, do the following: Click the button next to Presets, and then under Inner click Inside Diagonal Top Left (first row, first option from the left). In the Transparency box, enter 80%. In the Distance box, enter 2 pt. Drag the circle onto the left side of the rounded rectangle. On the Home tab, in the Drawing group, click Shapes, and then under Block Arrows click Chevron (second row, eighth option from the left). On the slide, drag to draw the chevron on the circle. Select the chevron. Under Drawing Tools, on the Format tab, in the Size group, do the following: In the Shape Height box, enter .23”. In the Shape Width box, enter .23”. On the Home tab, in the Drawing group, click Shape Fill, and then click White, Background 1 (first row, first option from the left). On the Home tab, in the Drawing group, click Shape Outline, and then click No Outline. Press and hold SHIFT and select all three shapes. On the Home tab, in the Drawing group, click Arrange, point to Align, and then click Align Middle. To reproduce the other shapes and arrange them on this slide, do the following: Press and hold SHIFT and select all three shapes. On the Home tab, in the Drawing group, click Arrange, and then under Group Objects click Group. On the Home tab, in the Clipboard group, click the arrow under Paste, and then click Duplicate. Repeat the process until there is a total of four groups of shapes. Separate each group of shapes and loosely arrange them on the slide. Press and hold SHIFT and select all four groups of shapes. On the Home tab, in the Drawing group, click Arrange, and then do the following: Point to Align, and then click Align Selected Objects. Point to Align, and click Distribute Vertically. Point to Align, and then click Align Center. Under Group Objects click Group. With the group still selected on the slide, on the Home tab, in the Drawing group, click Arrange, point to Align, and then do the following: Click Align to Slide. Click Align Center. Click Align Middle. To change the color for the duplicate circles (second, third, and fourth from the top), do the following: Press and hold SHIFT and select all four groups of shapes. On the Home tab, in the Drawing group, click Arrange, and click Ungroup. Select the circle that you would like to change. Under Drawing Tools, on the Format tab, in the Shape Styles group, click the arrow next to Shape Fill, and then do the following: For the second circle from the top, under Theme Colors, click Blue, Accent 1, Lighter 60% (third row, fifth option from the left). For the third circle from the top, under Theme Colors, click Purple, Accent 4, Lighter 60% (third row, eighth option from the left). For the fourth circle from the top, under Theme Colors, click Red, Accent 2, Lighter 60% (third row, sixth option from the left). To reproduce the background on this slide, do the following: Right-click the slide background area, and then click Format Background. In the Format Background dialog box, click Fill in the left pane, select Gradient fill in the Fill pane, and then do the following: In the Type list, select Radial. Click the button next to Direction, and then click From Center (third option from the left). In the Stop position box, enter 80%. Increment/Decrement Operators Operator Precedence Table Exercises

3 Data Type of an Arithmetic Expression
Data type of an expression depends on the type of its operands Data type conversion is done by the compiler If operators are *, /, +, or – , then the type of the result will be: integer, if all operands are integer. float, if all operands are integer and floats If at least one operand is float and there is no double double, if at least one operand is double

4 Data Type of an Arithmetic Expression
Example int * int; int + float; double / float; int – double; int*int/float; float*int-int*double; int*(float+double); int/int; result int result float result double result double result float result double result double result int

5 Data Type of an Arithmetic Expression : (Continued)
on the left hand side of the assignment operator (‘=‘) The data type of the target variable is also important If the result is a real number and the target variable is declared as integer, only the integer part of the result will be kept, and decimal part will be lost. Example int avg; float sum=100.0, cnt = 6.0; avg = sum / cnt; The result is calculated as But avg will be 16

6 Data Type of an Arithmetic Expression : (Continued)
Division operation is normally expected to give a real result but actually it may produce an integer result. Example The result of the division will be 16 avg will be 16.0 float avg; int sum=100, cnt = 6; avg = sum / cnt; Only the integer part of the result will be considered if two operands are integer Even when the target variable is float

7 Data Type of an Arithmetic Expression : (Continued)
Example : #include <stdio.h> int main() { int i=5, j=2, rm; float dv; j=j+1; rm= i%j; dv= i/j; printf(“%d %f”,rm,dv); }

8 Data Type of an Arithmetic Expression : (Continued) Type Casting
Example : #include <stdio.h> int main() { int i=5, j=2, rm; float dv; j=j+1; rm= i%j; dv= (float)i/j; printf(“%d %f”,rm,dv); } Type cast: tells the compiler to treat i as a floating point number Because of the type cast the result of the arithmetic operation is float and the target is also float. This means, we will see the correct result on the output.

9 Data Type of an Arithmetic Expression : (Continued) Type Casting
Example : #include <stdio.h> int main() { int i=5, j=2, rm; float dv; j=j+1; rm= i%j; dv= i/(float)j; printf(“%d %f”,rm,dv); } Type cast: tells the compiler to treat j as a floating point number It does not matter whether the first or the second operand is float. If an arithmetic operation contains integer and floating point operands the result will be float. dv= i/j;

10 Relational Expressions:
Relational expression is an expression which compares 2 operands and returns a TRUE or FALSE answer. Example : a >= b , a == c , a >= 99 , ‘A’ > ‘a’   Relational expressions are used to test the conditions in selection, and looping statements. == Equal To != Not Equal To < Less Than <= Less Than or Equal To > Greater Than >= Greater Than or Equal To The operator that tests for equality is “= =”, not “=”. “=” is the assignment operator.

11 Relational Expressions: (Continued)
Example This is an assignment statement. It actually assigns 5 to the variable k. Assignment statements are always evaluated to TRUE!!!!. int k=7; if ( k = 5) printf(“ k is 5”); Output A very common mistake is to use assignment (=) operator instead of the relational comparison operator (==)

12 Relational Expressions: (Example)
Write a program which will prompt the user to enter 2 integer numbers, ‘num1’, ‘num2’. The program would then compare these 2 numbers and display one of the following 3 options: num1 > num2 num1 < num2 num1 = num2

13 Relational Expressions: (Example)
Flowchart

14 Relational Expressions: (Continued)
#include <stdio.h> int main() { int num1, num2; printf("Enter the Number 1 :"); scanf("%d",&num1); printf("Enter the Number 2 :"); scanf("%d",& num2); if (num1 == num2) printf("\n Number1 is equal to Number 2"); else if (num1<num2) printf("\n Number 1 is smaller then Number 2"); else printf("\n Number 1 is greater then Number 2"); }

15 Logical Expressions Logical Expressions are used to carry out logical operations on logical operands or relational expression results. Logical Operators:  Operator Meaning ! Not (Highest Priority) && And || Or (Lowest Priority) ______.

16 Logical Expressions: (Continued)
OR ( || ) TABLE A B A || B F T

17 Logical Expressions: (Continued)
AND ( && ) TABLE A B A && B F T

18 Logical Expressions: (Continued)
NOT ( ! ) TABLE A !A F T

19 Logical Expressions: (Example)
Write an expression to check whether num1 is between 1 and 100 (inclusive) if ( (num1 >= 1) && (num1 <=100) ) Here two conditions (num1 >= 1), and (num1 <=100) will be tested. Since the conditions are connected with && (And) logical operator, the overall result of the condition will be true only for the case when both of the conditions are true. the action part of the “the overall result of the condition will be true if” statement will be executed only when both conditions are satisfied

20 Logical Expressions: (Example)
#include <stdio> /* Calculate the Letter Grade of a student*/ void main() { int result; char grade; printf("\n Enter the exam result :"; scanf(“%d”,&result); if (result < 50) printf("\n Student got F“); if ((result < 60) && (result >= 50)) printf("\n Student got D“); if ((result < 70) && (result >= 60)) printf("\n Student got C“); if ((result < 80) && (result >= 70)) printf("\n Student got B“); if (result >= 80) printf("\n Student got A“); } /* End of main()*/

21 Multiple Assignment Statements
It is sometimes necessary to assign the same value to two or more variables. This can be achieved in a single statement. All targets must be variables The value to be assigned must at the rightmost position Example: Can be written as sum = 0; count = 0; sum = count = 0; sum = 0=count = 0; 0=sum=count ; sum = 0=count; sum,count = 0;

22 The Compound Assignment Operators
Meaning + = is increased by – = is decreased by * = is multiplied by / = in divided by Example k = k + 5; k += 5; m = m ; m – = 100 ; j = j * m ; j * = m ; p = p \ r ; p / = r ;

23 Increment and Decrement Operators
Meaning + + Increment by one – - Decrement by one Example k = k + 1; k = k++; or k ++; or k = ++k; or ++k; k = k - 1; k = k--; or k ++; or k = --k; or ++k; Prefix Operators Postfix Operators

24 Increment and Decrement Operators
If the value produced by ++ or – – is not used in an expression, it does not matter whether it is a pre or a post increment (or decrement). When ++ (or – –) is used before the variable name, the computer first increments (or decrements) the value of the variable and then uses its new value to evaluate the expression. When ++ (or – –) is used after the variable name, the computer uses the current value of the variable to evaluate the expression, and then it increments (or decrements) the value of the variable.

25 Increment and Decrement Operators Examples
For each expression given below assume a = 5; b = 7; c = 3; After execution d = ++b - a++; d = 8 – 5 = 3, b = 8, a=6 After execution d = 6 – 2 = 4, a = 5, c=2 d = a c; After execution d = = 7, a = 4, c=1 d = a-- + c--; After execution d = = 3, a = 3, c=0 d = --a + --c ; After execution d = d + (d+1) = = 7 d += ++d ;

26 Operator Precedence Operators Associatively Type [ ] ( ) Left to right
[ ] ( ) Left to right Highest ! Right to left Unary * / % Multiplicative Additive < <= > >= Relational == != Equality && Logical and || Logical or ?: Conditional = += -= *= /= %= Assignment

27 Exercises #include <stdio.h> main() { float height, base, area;
Write a program which will prompt the user to input the length and the with of a triangle. The program would then calculate and display the area. #include <stdio.h> main() { float height, base, area; printf(“Enter the height = ”); scanf(“%f”,&height); printf(“Enter the base = ”); scanf(“%f”,&base); area = 0.5*height *base; printf(“The area is %f”,area); } /* End of Program*/

28 Exercises # include <stdio.h> main() { float c, f;
Write a program which will prompt the user to input the temperature in Fahrenheit. The program will then convert the Fahrenheit value to Centigrade using the following formula and display the result. C = 9 / 5 ( f – 32 ). # include <stdio.h> main() { float c, f; printf(“Enter the Fahrenheit temperature= ”); scanf(“%f”,&f); c = ( float ) 5/9 * (f – 32); printf(“The Celsius temperature is %f”,c); }

29 Exercises # include <stdio.h> # include <stdio.h> main()
Write a C program code which will prompt the user to input integer values to X and Y. It will then calculate and display X2+Y2 . # include <stdio.h> main() { int x, y, result; printf(“Enter X :”); scanf(“%d”,&x); printf(“Enter Y :”; scanf("%d”,&y); result = x*x + y*y; printf(" x*x + y*y =”); printf(“%d”,result); } # include <stdio.h> main() { int x, y; printf(“Enter X :”); scanf("%d”,&x); printf(“Enter Y :”; scanf("%d”,&y); printf(" x*x + y*y =”); printf( “%d”,x*x + y*y); }

30 Exercises # include <stdio.h> main() {
Write a C program code which will prompt the user to input 3 characters. The program would then display these characters from last to first. # include <stdio.h> main() { char ch1=‘ ’, ch2=‘ ’, ch3=‘ ’; printf(“\n Enter a character string 3 characters long = ”); scanf(“%c%c%c”,&ch1,&ch2,&ch3); printf(“The reverse of your string is ”); printf(“%c %c %c”,ch3,ch2,ch1); }

31 Exercises # include <stdio.h> main() { int hours, min; long sec;
Write a C program code which will prompt the user to input a value for hours. The program would then transform and display this value of hours in minutes and seconds. # include <stdio.h> main() { int hours, min; long sec; printf(“Hours = “); scanf(“%d”,&hours); min = hours * 60; sec =(long) min * 60; printf(“ hours =%d minutes=%d seconds= %ld“, hours, min, sec); }

32 Exercises Write a C program that reads in an integer value and then display it in float data type. # include <stdio.h> main() { int x; float y; printf(“Enter an Integer = ”); scanf(“%d”,&x); y=x; printf(“Now your value is a real number = %f”, y); }

33 Exercises Write a C program that reads in radius (r) and height (h) of a cone and then calculates and displays the volume of the cone. (Hint: Vcone = 1/3*3.14*r2 * h) # include <stdio.h> main() { float r, h; /*r=radius, h=height*/ float v; /*v=volume*/ printf(“\n Enter the radius = ”); scanf(“%f”,&r); printf(“\n Enter the height = ”; scanf(“%f”,&h); v = 1 / 3* 3.14 * r * r * h; printf(“\n The volume of the cone is %f”,v); }

34 Exercises Write a C program that reads two resistance values (R1 and R2) that are connected as parallel on a circuit, and it calculates the equivalent resistance of the circuit. [ Hint:Equivalent resistance  Re = R1R2 / (R1 + R2) ] # include <stdio.h> main() { int r1, r2; float re; printf(“\n Enter the resistance 1=” ); scanf(“%d”,&r1); printf(“\n Enter the resistance 2=” ); scanf(“%d”,&r2); re = (float) r1 * r2 / (r1 + r2); printf("\n The equivalent Resistance is “,re);; }

35 Exercises Write a C program that reads in an integer (x) and then calculates and displays its square (x2) and its cube (x3). # include <stdio.h> main() { int x, x2, x3; printf(“ X = ”); scanf(“%d”,&x); x2 = x * x; x3 = x2 * x; printf(“\n Square of X is %d cube of X is %d”,x2,x3); }

36 That’s It for now!


Download ppt "ITEC113 Algorithms and Programming Techniques"

Similar presentations


Ads by Google