ITEC 113 Algorithms and Programming Techniques C programming

  • Slides: 36
Download presentation
ITEC 113 Algorithms and Programming Techniques C programming: Variables, Expressions part II

ITEC 113 Algorithms and Programming Techniques C programming: Variables, Expressions part II

Contents Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound Assignment

Contents Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound Assignment Operators Increment/Decrement Operators Operator Precedence Table Exercises

Data Type of an Arithmetic Expression • Data type of an expression depends on

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

Data Type of an Arithmetic Expression Example int * int; result int result float

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

Data Type of an Arithmetic Expression : (Continued) on the left hand side of

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 16. 66667 But avg will be 16

Data Type of an Arithmetic Expression : (Continued) • Division operation is normally expected

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 float avg; int sum=100, cnt = 6; avg = sum / cnt; The result of the division will be 16 avg will be 16. 0 • Only the integer part of the result will be considered if two operands are integer • Even when the target variable is float

Data Type of an Arithmetic Expression : (Continued) Example : #include <stdio. h> int

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); }

Data Type of an Arithmetic Expression : (Continued) Type Casting Example : #include <stdio.

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.

Data Type of an Arithmetic Expression : (Continued) Type Casting Example : #include <stdio.

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

Relational Expressions: • Relational expression is an expression which compares 2 operands and returns

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.

Relational Expressions: (Continued) Example int k=7; if ( k = 5) printf(“ k is

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

Relational Expressions: (Example) • Write a program which will prompt the user to enter

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

Flowchart Relational Expressions: (Example)

Flowchart Relational Expressions: (Example)

Relational Expressions: (Continued) #include <stdio. h> int main() { int num 1, num 2;

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

Logical Expressions • Logical Expressions are used to carry out logical operations on –

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

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

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

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

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

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

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

Logical Expressions: (Example) • Write an expression to check whether num 1 is between

Logical Expressions: (Example) • Write an expression to check whether num 1 is between 1 and 100 (inclusive) if ( (num 1 >= 1) && (num 1 <=100) ) … • Here two conditions (num 1 >= 1), and (num 1 <=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

Logical Expressions: (Example) #include <stdio> /* Calculate the Letter Grade of a student*/ void

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()*/

Multiple Assignment Statements • It is sometimes necessary to assign the same value to

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 r i g h t m o s t position Example: sum = 0; count = 0; sum = 0=count = 0; Can be written as 0=sum=count ; sum = count = 0; sum = 0=count; sum, count = 0;

The Compound Assignment Operators Operator Meaning + = is increased by – = is

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

Increment and Decrement Operators Operator Meaning + + Increment by one – - Decrement

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

Increment and Decrement Operators • If the value produced by ++ or – –

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.

Increment and Decrement Operators Examples For each expression given below assume a = 5;

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

Operator Precedence Operators [] () ++ -- ! * / % + < <=

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

Exercises Write a program which will prompt the user to input the length and

Exercises 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*/

Exercises Write a program which will prompt the user to input the temperature in

Exercises 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); }

Exercises Write a C program code which will prompt the user to input integer

Exercises Write a C program code which will prompt the user to input integer values to X and Y. It will then calculate and display X 2+Y 2. # 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); }

Exercises Write a C program code which will prompt the user to input 3

Exercises 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 ch 1=‘ ’, ch 2=‘ ’, ch 3=‘ ’; printf(“n Enter a character string 3 characters long = ”); scanf(“%c%c%c”, &ch 1, &ch 2, &ch 3); printf(“The reverse of your string is ”); printf(“%c %c %c”, ch 3, ch 2, ch 1); }

Exercises Write a C program code which will prompt the user to input a

Exercises 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); }

Exercises Write a C program that reads in an integer value and then display

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); }

Exercises Write a C program that reads in radius (r) and height (h) of

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*r 2 * 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 * h; printf(“n The volume of the cone is %f”, v); }

Exercises Write a C program that reads two resistance values (R 1 and R

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

Exercises Write a C program that reads in an integer (x) and then calculates

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

THAT’S IT FOR NOW!

THAT’S IT FOR NOW!