Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 13 Transition from Fortran to C Yi Lin Feb 27, 2007.

Similar presentations


Presentation on theme: "Lecture 13 Transition from Fortran to C Yi Lin Feb 27, 2007."— Presentation transcript:

1 Lecture 13 Transition from Fortran to C Yi Lin Feb 27, 2007

2 Hello World! PROGRAM hello IMPLICIT NONE !This is my first program WRITE (*,*) “Hello World!“ END PROGRAM hello int main() { printf( “Hello World!\n”); return 0; } C is space insensitive but case sensitive. \>gfortran hello.f90 \>gfortran hello.f90 –o hello \>hello \>gcc hello.c \>gcc hello.c –o hello \>hello

3 Programming language in general Variable, expression, statement Conditional statement (if-else) Iteration statement (loop) Sub-program units (functions) Log Exp 1 st Block2 nd block TF i < n 1 st Block T F

4 Comparison of Fortran and C, variable types INTEGER REAL CHARACTER int short long unsigned int unsigned short unsigned long float double char

5 Variable size in C TYPEbytesValue range int4 -2147483648 ~ 2147483647 (-2 15 ~ (2 15 –1)) short2-32768 ~ 32767 (-2 15 ~ (2 15 –1)) long4(32bits) 8(64bits) (-2 63 ~ (2 63 –1)) (-2 31 ~ (2 31 –1)) unsigned short20 ~ 65535 (0 ~ 2 16 -1) unsigned long80 ~ 4294967295 (0 ~ 2 32 -1) float41 bit sign,8 bits exp, 23 bits fraction double81 bit sign,11 bits exp,52bits fraction char1Represented in ASCII code

6 Example 2: int int main(){ int a, b, c, d; unsigned u; a=12; b=-24;u=10; c=a+u; d=b+u; printf(“a+u=%d, b+u=%d\n”, c, d); return 0; } OUTPUT: a+u=22, b+u=-14 PROGRAM test IMPLICIT NONE INTEGER::a,b,c,d,u a=12 b=-24 u=10 c=a+u d=b+u WRITE(*,*) “a+u=“,c, “b+u=“,d END PROGRAM test What is the difference from a Fortran program?

7 Example 3: double int main(){ double x; int i; x=3.6; i=(int)x; // Force type casting from double to int. Not int(x) /* (int)x+i: force x to an int and then plus i */ printf(“x=%f, i=%d”, x, i); // x doesn’t change return 0; } OUTPUT: x=3.600000, i=3

8 Example 4: char and string int main(){ char c1, c2; char* str; // more about “*” in the next class. c1=‘a’; c2=‘b’; printf(“c1=%c, c2=%c\n”, c1, c2); c1=97; c2=98; // In ASCII codes, a-z: 97-123, A-Z:65-91 printf(“c1=%c, c2=%c\n”, c1, c2); c1 = c1 - 32; c2 = c2 - 32; // equivalent to: c1 -=32; c2 -=32; printf(“c1=%c, c2=%c\n”, c1, c2); str = “Hello world!”; printf(“str=%s\n”, str); return 0; } OUTPUT: c1=a, c2=b c1=A, c2=B str=Hello world!

9 Arithmetic Operators Arithmetic operators ++, --, -(minus) right to left *, /, %left to right +, -(sub)left to right post-, pre-, Incrementing(++), decrementing(--) i++After using i, increasing i by 1 i--After using i, decreasing i by 1 ++iBefore using i, increasing i by 1 --iBefore using i, decreasing i by 1 low high

10 Example 5: ++, -- int main(){ int i, j; i=3; j=++i; printf(“i=%d, j=%d\n”, i,j); // output: i=4,j=4 i=3; j=i++; printf(“i=%d, j=%d\n”, i,j); // output: i=4,j=3 i=3; j=--i; printf(“i=%d, j=%d\n”, i,j); // output: i=2,j=2 i=3; j=i--; printf(“i=%d, j=%d\n”, i,j); // output: i=2,j=3 return 0; }

11 Example 6: ++, -- int main(){ int i, j; i=3; printf(“i=%d\n”, ++i); //Output: i=4 j=i++; printf(“i=%d, j=%d\n”, i,j); //Ouput:i=5, j=4 i=3; printf(“i=%d\n”, i++); //Output: i=3 j=i++; printf(“i=%d, j=%d\n”, i,j); //Ouput:i=5, j=4 return 0; }

12 Operators (cont.) Relational operators ==, != (eqv. to /= in Fortran),, >= Logical operators ! (eqv. to.NOT. in Fortran)R to L && (eqv. to.AND. in Fortran)L to R || (eqv. to.OR. in Fortran)L to R Bitwise operators (Not required!) &, |, ^, ~, > high low

13 Operators (cont.) Ternary operator ? : If the logical-exp is true, the value of this expression is exp1’s value, otherwise, it is the value of exp2. Example 7: int main(){ int i, j; scanf(“%d”, &i); // eqv to READ(*,*) in Fortran j=(i>0) ? i : -i; // if i is positive j=i, otherwise, j=-i printf(“i=%d, j=%d\n”, i, j); return 0; } READ in value: -5 OUTPUT: i=-5, j=5

14 Operators (cont.) Condensed operators +=, -=, *=, /=, %= E.g. a += b; // eqv. to a=a+b &=, |=, ^=, >= (Not. Required!) Example 8: int main(){ int i, j; i=2; j=3; i+=j; printf(“i=%d, j=%d\n”, i, j); //output: i=5, j=3 i=2; j=3; i-=j; printf(“i=%d, j=%d\n”, i, j); //output: i=-1, j=3 i=2; j=3; i*=j; printf(“i=%d, j=%d\n”, i, j); //output: i=6, j=3 i=2; j=3; i/=j; printf(“i=%d, j=%d\n”, i, j); //output: i=0, j=3 i=2; j=3; i%=j; printf(“i=%d, j=%d\n”, i, j);//output: i=2, j=3 return 0; }

15 scanf and printf Syntax: scanf(, ); printf(, ); Formats: d: decimal int o: octal int x: hexdecimal int c: character s: string f: real number, floating point e: real number, exponential format

16 Example 9: scanf and printf int main(){ int a, b, c; scanf(“%d%d%d”, &a, &b, &c); printf(“%d, %d, %d\n”, a, b, c); return 0; } Valid input: 3#4#5  3, 4, 5 3##4###5  3, 4, 5 3 4#5  3, 4, 5 3 4 5  3, 4, 5 Invalid input: 3,4,5

17 Example 10, 11: scanf and printf int main(){ // example 10 int a, b, c; scanf(“%d,%d,%d”, &a, &b, &c); printf(“%d, %d, %d\n”, a, b, c); return 0; } Valid input: 3,4,5  3, 4, 5 3,#4,##5  3, 4, 5 int main(){ // example 11 int a, b, c; scanf(“%d:%d:%d”, &a, &b, &c); printf(“%d, %d, %d\n”, a, b, c); return 0; } Valid input: 3:4:5  3, 4, 5 3:#4:##5  3, 4, 5 It doesn’t matter if written in one line.

18 Example 12: scanf and printf int main() { float a, b; double c,d; scanf(“%f%f%lf%lf”, &a, &b, &c, &d); // %lf for double, %ld for long int printf(“a=%f, b=%6.2f, c=%f, d=%6.2f”, a, b, c, d); /* 6.2f: output b with 6 digits width and 2 decimal digits */ return 0; } Valid input: 1.3##2.4#3.5#6.2  a=1.300000, b=##2.40, c=3.500000, d=##6.20

19 Example 13: scanf and printf int main() { int a, b; scanf(“%3d%3d”, &a, &b); printf(“a=%4d, b=%4d\n”, a, b); return 0; } Valid input: 123456  a=123, b=456 12#34#  a=12, b=34

20 Conditional statement if (logical-exp) statement if (logical-exp) statement1 else statement2 IF (logical-exp) statement IF (logical-exp) THEN 1 st block of statements ELSE 2 nd block of statements END IF

21 Example 14: if, if-else int main(){ int i; scanf(“%d”, &i); if(i<0) printf(“%d”, i); else printf(“%d”, -i); return 0; } PROGRAM test IMPLICIT NONE INTEGER::i READ(*,*) i IF(i<0) THEN WRITE(*,*) i ELSE WRITE(*,*) –i END IF END PROGRAM test INPUT: -5 OUTPUT: 5 What if there are more than 1 statement between if and else?

22 Example 15: scope {} int main() { int a, b, t=0; scanf(%d,%d”,&a,&b); if(a>b) t=a; a=b; b=t; printf(“%d %d %d\n”, a, b, t); return 0; } PROGRAM test INTEGER::a, b, t=0 READ(*,*) a, b IF(a>b) THEN t=a a=b b=t END IF WRITE(*,*) a, b END PROGRAM test INPUT: 3, 1 OUTPUT: 1 3 3 INPUT: 1, 3 OUTPUT: 1 3 0 int main() { int a, b, t=0; scanf(%d,%d”,&a,&b); if(a>b){ t=a; a=b; b=t; } printf(“%d %d %d\n”, a, b, t); return 0; } PROGRAM test INTEGER::a, b, t=0 READ(*,*) a, b IF(a>b) & t=a a=b b=t WRITE(*,*) a, b END PROGRAM test INPUT: 3, 1 OUTPUT: 1 3 3 INPUT: 1, 3 OUTPUT: 3 0 0

23 Example 16: if-else if-else Problem: write a program to read an integer x and output the corresponding y: int main() { int x, y; scanf(“%d”, &x); if(x<0) y=-1; else if(x==0) y=0; else y=1; printf(“x=%d,y=%d\n”, x, y); return 0; }

24 Switch case, example 17 int main() { char grade; scanf(“%c”, &grade); switch (grade){ case ‘A’: printf(“85~100”\n”);break; case ‘B’: printf(“70~84\n”); break; case ‘C’: printf(“60~69\n”); break; case ‘D’: printf(“<60\n”); break; default: printf(“error\n”); } return 0; } PROGRAM test IMPLICIT NONE CHARACTER::grade READ(*,*) grade SELECT CASE (grade) CASE ‘A’: WRITE(*,*) “85~100” CASE ‘B’: WRITE(*,*) “70~84” CASE ‘C’: WRITE(*,*) “60~69” CASE ‘D’: WRITE(*,*) “<60” DEFAULT: WRITE(*,*) “error” END SELECT END PROGRAM “break” is very important! Without it, if grade==‘A’, output: 85~100 70~84 60~69 <60 error

25 ITERATION C (i<n as an example logical- exp) while(i<n) { i++; } do { i++; } while(i<n); for(i=1; i<n; i++){ } Fortran (i<n as an example logical-exp) DO count=1, n, 1 END DO DO IF(i>n) EXIT i=i+1 END DO DO WHILE(i<n) i=i+1 END DO

26 Example 19: do while int main(){ // example 18 int i, sum=0; i=1; while(i<=10){ sum +=i; // eqv. to sum=sum+i; i++; // eqv. to i=i+1; or ++i; } prinf(“sum=%d\n”,sum); return 0; } int main(){ // example 19 int i, sum=0; i=1; do { sum +=i; // eqv. to sum=sum+1; i++; // eqv. to i=i+1; or ++i; } while(i<=10) ; prinf(“sum=%d\n”,sum); return 0; }

27 Example 20: while v.s. do-while int main(){ // example 20 int i, sum=0; scanf(“%d”, &i); while(i<=10){ sum +=i; // eqv. to sum=sum+i; i++; // eqv. to i=i+1; or ++i; } prinf(“sum=%d\n”,sum); return 0; } int main(){ // example 21 int i, sum=0; scanf(“%d”, &i); do { sum +=i; // eqv. to sum=sum+i; i++; // eqv. to i=i+1; or ++i; } while(i<=10); prinf(“sum=%d\n”,sum); return 0; } INPUT: 1 OUTPUT: 55 INPUT: 11 OUTPUT: 0 INPUT: 1 OUTPUT: 55 INPUT: 11 OUTPUT: 11

28 for: example 22 int main(){ // example 22 int i, sum=0; for(i=1; i<=10; i++) { sum+=i; } printf(“sum=%d\n”,sum); return 0; } Syntax for(exp1; log-exp2; exp3) statement(s) Log-exp2 exp1 T F statement(s) exp3

29 Example 23, 24: for // Same as example 22 int main(){ // example 23 int i, sum=0; i=1; for(; i<=10; i++) { sum+=i; } printf(“sum=%d\n”,sum); return 0; } // Same as example 22 int main(){ // example 24 int i, sum; i=1; for(sum=0; i<=10; i++) { sum+=i; } printf(“sum=%d\n”,sum); return 0; }

30 Example 25,26: for // Same as example 22 int main(){ // example 25 int i, sum; for(i=1, sum=0; i<=100; i++) { sum+=i; } printf(“sum=%d\n”,sum); return 0; } // Same as example 22 int main(){ // example 26 int i, sum=0; i=1; for(; i<=100; ) { sum+=i; i++; } printf(“sum=%d\n”,sum); return 0; }

31 Example 27: break int main(){ // example 27 int i; for(i=1; i<10; i++){ if(i == 8) break; // Loop terminated when i==8 printf( “ %d\n ”, i) // write numbers 1 to 7 only } return 0; } PROGRAM test DO i=1,10 if(i == 8) EXIT !Loop terminated when i==8 WRITE(*,*) i !write numbers 1 to 7 only END DO END PROGRAM “continue” is similar.


Download ppt "Lecture 13 Transition from Fortran to C Yi Lin Feb 27, 2007."

Similar presentations


Ads by Google