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

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
C Programming Input output Conditional statements and Iterative statements Rohit Khokher.
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
1 Chapter Four Boolean Expressions and Control Statements.
C PROGRAMMING LECTURE 17 th August IIT Kanpur C Course, Programming club, Fall by Deepak Majeti M-Tech CSE
Introduction to C Language
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Control Statements Spring Semester 2013Programming and Data Structure1.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Computers and Programming CPC The 2 nd lecture Jiří Šebesta.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Dennis Ritchie 1972 AT & T Bell laboratories (American Telephone and Telegraph) USA 1www.gowreeswar.com.
Chapter 05 (Part III) Control Statements: Part II.
Chapter 3 Flow Control By C. Shing ITEC Dept Radford University.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
IIT Kanpur C Course Lecture 3 Aug 31, Rishi Kumar, Final year BT-MT, CSE.
1 Programming a Computer Lecture Ten. 2 Outline  A quick introduction to the programming language C  Introduction to software packages: Matlab for numerical.
Variables Symbol representing a place to store information
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
G. Pullaiah College of Engineering and Technology
Arithmetic Expressions
ECE Application Programming
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
ECE Application Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
C Short Overview Lembit Jürimägi.
For loops Increment ,decrement operations
Chapter 4: Making Decisions.
Introduction to C Programming
Introduction to Programming
Looping.
Arithmetic operations, decisions and looping
- Additional C Statements
Midterm Review Programming in Fortran
אבני היסוד של תוכנית ב- C
Chapter 6 Decision Making and Looping
Introduction to C Programming
Chapter 4 Managing Input and Output Operations
EECE.2160 ECE Application Programming
By C. Shing ITEC Dept Radford University
EECE.2160 ECE Application Programming
Dale Roberts, Lecturer IUPUI
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

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

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

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

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

Variable size in C TYPEbytesValue range int ~ (-2 15 ~ (2 15 –1)) short ~ (-2 15 ~ (2 15 –1)) long4(32bits) 8(64bits) (-2 63 ~ (2 63 –1)) (-2 31 ~ (2 31 –1)) unsigned short20 ~ (0 ~ ) unsigned long80 ~ (0 ~ ) float41 bit sign,8 bits exp, 23 bits fraction double81 bit sign,11 bits exp,52bits fraction char1Represented in ASCII code

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?

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= , 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: , 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!

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

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 ? : 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(, ); 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

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,  3, 4, 5 Invalid input: 3,4,5

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.

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= , b=##2.40, c= , 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:  a=123, b=456 12#34#  a=12, b=34

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

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?

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: INPUT: 1, 3 OUTPUT: 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: 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; }

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

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

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

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

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

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

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

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.