Programming Language A Takako Nemoto (JAIST) 22 October Takako Nemoto (JAIST) 22 October 1 / 28
From Homework 2 Homework 2 1 Write a program calculate something with at least two integer-valued inputs, which is not appeared in this lecture. Add comments to explain what is calculated in the program. 2 Send the program to me via e-mail, with the title Homework Lecture2. Please make sure to write your name and student ID number in the mail. Examples etc.. absolute value; area of a triangle; exponentiation; Takako Nemoto (JAIST) 22 October 2 / 28
Absolute value int main(void) { int n1, n2; puts("input two integers."); printf("integer 1: "); scanf("%d", &n1); printf("integer 2: "); scanf("%d", &n2); printf("the sum of them is %d.\n", abs(n1 - n2)); Depending on compiler, you may have an error at compiling. This is because the absolute value function abs is not in stdio.h but in stdlib.h. You will not have an error if you add #include <stdlib.h>. Takako Nemoto (JAIST) 22 October 3 / 28
Operators and operand Binary operators + plus - minus * multiplication / quotient % reminder Example int x, y; puts("input two integers"); printf("integer x: "); scanf ("%d", &x); printf("integer y: "); scanf ("%d", &y); printf("x + y is %d\n", x + y); printf("x - y = %d\n", x - y); printf("x * y = %d\n", x * y); printf("x / y = %d\n", x / y); printf("x %% y = %d\n", x % y); Input two integers Integer x: 9 Integer y: 8 x + y = 17 x + y = 1 x * y = 72 x / y = 1 x % y = 1 Takako Nemoto (JAIST) 22 October 4 / 28
Operators and operand Unary operators unary + operator: + unary operator: - Example int x; printf("input an integer x: "); scanf("%d", &x); printf("+x is %d and -x is %d\n", +x, -x); Input an integer x: 9 +x is 9 and -x is -9 %d can be used several times in one statement. The 1st %d corresponds to the 2nd argument +x and the 2nd %d to the 3rd argument -x. Takako Nemoto (JAIST) 22 October 5 / 28
Types Example int main(void) { int n1, n2; printf("input the width: "); scanf("%d",&n1); printf("input the height: "); scanf("%d",&n2); printf("the area of this triangle is %d.\n", n1 * n2 / 2); Input the lwidth: 3 Input the height: 5 The area of this triangle is 7. The last statements calls the integer-value n1 * n2 / 2. Therefore the fraction part is omitted. Takako Nemoto (JAIST) 22 October 6 / 28
Types Example int main(void) { double n1, n2; printf("input the width: "); scanf("%lf",&n1); printf("input the height: "); scanf("%lf",&n2); printf("the area of this triangle is %f.\n", n1 * n2 / 2); Input the width: 3 Input the height: 5 The area of this triangle is 7.500000. double is the type for floating point decimal. Note that the conversion specifications are %f in printf and %lf in scanf, both for value with the type double. Takako Nemoto (JAIST) 22 October 7 / 28
Type and operators In principle, the output of the operation has the same type as its inputs. If the inputs have different types, then the result has superior type. Example: 5/2 with types int and double 5 int / 2 int = 2 int 5 / 2.0 = 2.5 int double double 5.0 / 2.0 = 2.5 double double double 5.0 / 2 = 2.5 double int double Takako Nemoto (JAIST) 22 October 8 / 28
Example of typing int n1, n2, n3, n4; double d1, d2, d3, d4; n1 = 5 / 2; n2 = 5.0 / 2.0; n3 = 5.0 / 2; n4 = 5 / 2.0; d1 = 5 / 2; d2 = 5.0 / 2; d3 = 5.0 / 2; d4 = 5 / 2.0; printf("n1 = %d\n", n1); printf("n2 = %d\n", n2); printf("n3 = %d\n", n3); printf("n4 = %d\n", n4); n1 = 2 n2 = 2 n3 = 2 n4 = 2 d1 = 2.000000 d2 = 2.500000 d3 = 2.500000 d4 = 2.500000 printf("d1 = %f\n", d1); printf("d2 = %f\n", d2); printf("d3 = %f\n", d3); printf("d4 = %f\n", d4); Takako Nemoto (JAIST) 22 October 9 / 28
Example of a type error int n, m; double x, y, z, w; printf("input 4 positive integers.\n"); printf("n = "); scanf("%d", &n); printf("m = "); scanf("%d", &m); printf("x = "); scanf("%lf", &x); printf("y = "); scanf("%lf", &y); z = n / m; w = x / y; Input 4 integers. n = 5 m = 2 x = 5 y = 2 n / m is 0 x / y is 2.500000 printf("n / m is %d\n", z); printf("x / y is %f\n", w); Takako Nemoto (JAIST) 22 October 10 / 28
As usual arithmetic, * is stronger than +, so we have to write (a+b). %2.1f specifies to print the floating-point-valued (a + b) / 2.0 with at least 2 digits, including 1 fraction part. See p.36 of the textbook. What do we have if we replace 2.0 with 2? Takako Nemoto (JAIST) 22 October 11 / 28 Type and operators int a, b; puts("input two integers."); printf("a = "); scanf("%d", &a); printf("b = "); scanf("%d", &b); Input two integers. a = 5 b = 2 The average is 3.5 printf("the average is %2.1f", (a + b) / 2.0);
Cast In the previous example, we can declare that (a + b) / 2.0 has the floating-point value as follows: int a, b; puts("input two integers."); printf("a = "); scanf("%d", &a); printf("b = "); scanf("%d", &b); printf("the average is %2.1f", (double) (a + b) / 2.0); Takako Nemoto (JAIST) 22 October 12 / 28
Exponentiation Some operator has a specific type of output. #include <math.h> double pow(double x, double y); int x, y; printf("input an integer x: "); scanf("%d", &x); printf("input an integer y: "); scanf("%d", &y); printf("exp(x, y) is %lf.", pow(x,y)); /*show the yth power of x*/ Integer x: 5 Integer y: 3 exp(x,y) is 125. Takako Nemoto (JAIST) 22 October 13 / 28
If statements int n; printf("input an integer: "); scanf("%d", &no); if (n % 5) puts("your input is not \ndivisible by 5."); Input an integer: 9 Your input is not divisible by 5. Input an integer: 10 The hilighted part means that if the inside of the brackets following if is satisfied, then do the following statements. In this case, puts ("Your input is not \n divisible by 5") is executed if no % 5 is not equal to 0 Takako Nemoto (JAIST) 22 October 14 / 28
If-else statements int n; printf("input an integer: "); scanf("%d", &no); if (n % 5) puts("your input is \nnot divisible by 5."); else puts("your input is \ndivisible by 5"); Input an integer: 9 Your input is NOT divisible by 5. Input an integer: 10 Your input is divisible by 5. By else statements, you can execute something if it is not the case of if (...). Takako Nemoto (JAIST) 22 October 15 / 28
Multiple if statements int year; printf("input year: "); scanf("%d", &year); if (year % 2) puts("you will have no Olympic game."); else if (year % 4) puts("you will have an winter Olympic game."); else puts("you will have an summer Olympic game."); Input year: 2018 You will have an winter Olympic game. Takako Nemoto (JAIST) 22 October 16 / 28
Multiple if statements int year; printf("input year: "); scanf("%d", &year); if (year % 2) puts("you will have no Olympic game."); else if (year % 4) puts("you will have an winter Olympic game."); else puts("you will have an summer Olympic game."); Input year: 2019 You will have no Olympic game. Takako Nemoto (JAIST) 22 October 17 / 28
Multiple if statements int year; printf("input year: "); scanf("%d", &year); if (year % 2) puts("you will have no Olympic game."); else if (year % 4) puts("you will have an winter Olympic game."); else puts("you will have an summer Olympic game."); Input year: 2020 You will have an summer Olympic game. Takako Nemoto (JAIST) 22 October 18 / 28
Control expression The green part is called control statement: if ( ). Control statements has the int type and if( val )... means that if the value of val is not equal to 0, do the following statements. We can express many conditions with relation and logical operators. Takako Nemoto (JAIST) 22 October 19 / 28
Equality & comparison operators Let a and b be floating point (or integer). The following expressions are evaluated as 0 or 1. expression a == b a!= b a > b a < b a >= b a <= b Evaluated 1 if The value of a is equal to the one of b The value of a is not equal to the one of b The value of a is strictly greater than the one of b The value of a is strictly smaller than the one of b The value of a is greater or equal to the one of b The value of a is smaller or equal to the one of b Takako Nemoto (JAIST) 22 October 20 / 28
Logical operators Logical operators && and can compound the expressions: Logical operators For control expressions A and B, value of A value of B value of A && B value of A B 1 1 1 1 1 0 0 1 0 1 0 1 0 0 0 0 Takako Nemoto (JAIST) 22 October 21 / 28
Example int x, y, z; printf("input 3 integers\n"); printf("x = "); scanf("%d", &x); printf("y = "); scanf("%d", &y); printf("z = "); scanf("%d", &z); if (x == y y == z z == x) printf("at least two of them are equal."); else printf("none of them are equal"); Takako Nemoto (JAIST) 22 October 22 / 28
Some more examples Example: Comparison three integers int main (void){ int x, y, z, max; puts("input three integers."); printf("x = "); scanf("%d", &x); printf("y = "); scanf("%d", &y); printf("z = "); scanf("%d", &z); max = x; if (y > max) max = y; if (z > max) max = z; printf ("The maximal value of them is %d.", max); Takako Nemoto (JAIST) 22 October 23 / 28
Block You can write multiple statements after if and else using {. int x, y; printf("input an integer x: "); scanf("%d", &x); printf("input an integer y: "); scanf("%d", &y); if (x > y){ printf("the maximum value is %d.\n", x); printf("the minimum value is %d.\n", y); else{ printf("the maximum value is %d.\n", y); printf("the minimum value is %d.\n", x); Takako Nemoto (JAIST) 22 October 24 / 28
Case distinction with switch int n; printf("input an integer: "); scanf("%d", &n); switch(n % 3){ case 0 : puts("it is divisible by 3."); break; case 1 : puts("the reminder divided by 3 is 1"); break; case 2 : puts("the reminder divided by 3 is 2"); break; Depending on the value n%3, the statements after case x is executed. break means go out from the switch statement. Takako Nemoto (JAIST) 22 October 25 / 28
Switch statement The number after case is label. Labels must be a constant, not a variable. You have to put whitespace between case and the label. If you don t put break;, then the next case x is executed. Try the example List 3-20 in p. 66. Takako Nemoto (JAIST) 22 October 26 / 28
Today s exercise and homework Do Exercise 2-4 in the textbook p. 33. Write a program in C s.t. ask 4 integers; print the minimum of them. Rewrite the Olympic-year program with switch. Homework 3 1 Write a program to convert the input Celsius degree temperature into Fahrenheight with 1 floating digit. 2 Write a such program s.t. it asks the elevator A s location (floor); the elevator B s location (floor); the elevator C s location (floor) and your current location (floor), and prints the closest elevator. Hint: Function abs. You should include stdlib.h. 3 See the next page. 4 Read ch.2-4 (pp. 21-104) of the textbook. Takako Nemoto (JAIST) 22 October 27 / 28
Homework 3 Rewrite the following using switch instead of if: int no; printf("input an integer: "); scanf("%d", &no); if(no % 2) printf("it is odd."); else printf("it is even."); Takako Nemoto (JAIST) 22 October 28 / 28