310 likes | 324 Views
Lecture 13 Transition from Fortran to C. Yi Lin Feb 27, 2007. PROGRAM hello IMPLICIT NONE !This is my first program WRITE (*,*) “Hello World!“ END PROGRAM hello. int main() { printf( “Hello World!<br>”); return 0; }. Hello World!. C is space insensitive but case sensitive.
E N D
Lecture 13 Transition from Fortran to C Yi Lin Feb 27, 2007
PROGRAM hello IMPLICIT NONE !This is my first program WRITE (*,*) “Hello World!“ END PROGRAMhello int main() { printf( “Hello World!\n”); return 0; } Hello World! 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
Programming language in general • Variable, expression, statement • Conditional statement (if-else) • Iteration statement (loop) • Sub-program units (functions) T F Log Exp 1st Block 2nd block F i < n T 1st Block
INTEGER REAL CHARACTER int short long unsigned int unsigned short unsigned long float double char Comparison of Fortran and C, variable types
What is the difference from a Fortran program? 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 Example 2: int
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
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 c1=A, c2=B str=Hello world!
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 ++i Before using i, increasing i by 1 --i Before using i, decreasing i by 1 high low
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; }
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; }
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
Operators (cont.) • Ternary operator • <logical-exp> ? <exp1> : <exp2> • 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
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; }
scanf and printf • Syntax: • scanf(<formats>, <list of variables>); • printf(<formats>, <list of variables>); • Formats: • d: decimal int • o: octal int • x: hexdecimal int • c: character • s: string • f: real number, floating point • e: real number, exponential format
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<TAB>4<TAB>5 3, 4, 5 Invalid input: 3,4,5
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 Example 10, 11: scanf and printf It doesn’t matter if written in one line.
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
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
if (logical-exp) statement if (logical-exp) statement1 else statement2 IF (logical-exp) statement IF (logical-exp) THEN 1st block of statements ELSE 2nd block of statements END IF Conditional statement
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 Example 14: if, if-else What if there are more than 1 statement between if and else? INPUT: -5 OUTPUT: 5
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 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; } INPUT: 3, 1 OUTPUT: 1 3 3 INPUT: 1, 3 OUTPUT: 1 3 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
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; }
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 Switch case, example 17 “break” is very important! Without it, if grade==‘A’, output: 85~100 70~84 60~69 <60 error
C (i<n as an example logical-exp) while(i<n) { <loop body> i++; } do { <loop body> i++; } while(i<n); for(i=1; i<n; i++){ <loop body> } Fortran (i<n as an example logical-exp) DO count=1, n, 1 <loop body> END DO DO IF(i>n) EXIT <loop body> i=i+1 END DO DO WHILE(i<n) <loop body> i=i+1 END DO ITERATION
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; } Example 19: 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; } Example 20: while v.s. do-while INPUT: 1 OUTPUT: 55 INPUT: 11 OUTPUT: 11 INPUT: 1 OUTPUT: 55 INPUT: 11 OUTPUT: 0
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) for: example 22 exp1 F Log-exp2 T statement(s) exp3
// 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; } Example 23, 24: 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; } Example 25,26: for
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 Example 27: break “continue” is similar.