Imperative Programming The Case of FORTRAN ICOM 4036 Lecture 4.

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

C Language.
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Procedural programming in Java
Chapter 5 ( ) of Programming Languages by Ravi Sethi
Chapter 7: User-Defined Functions II
Programming in Visual Basic
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Fortran 9x HTML version. New F90 features Free source form Modules User-defined data types and operators Generic user-defined procedures Interface blocks.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Computer Science 210 Computer Organization Introduction to C.
Introduction to FORTRAN
Imperative Programming The Case of FORTRAN ICOM 4036 Lecture 5 READINGS: PLP Chapters 6 and 8.
DAT602 Database Application Development Lecture 5 JAVA Review.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
FALL 2001ICOM Lecture 21 ICOM 4015 Advanced Programming Lecture 2 Procedural Abstraction Reading: LNN Chapter 4, 14 Prof. Bienvenido Velez.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
FORTRAN FORmula TRANslator -Anand Trivedi. HISTORY  Designed and written from scratch in by an IBM team lead by John W. Backus as the first.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Fundamental Programming: Fundamental Programming Introduction to C++
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Procedural programming in Java Methods, parameters and return values.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
Introduction to Programming
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
1 Programming a Computer Lecture Ten. 2 Outline  A quick introduction to the programming language C  Introduction to software packages: Matlab for numerical.
FORTRAN History. FORTRAN - Interesting Facts n FORTRAN is the oldest Language actively in use today. n FORTRAN is still used for new software development.
Imperative Programming The Case of FORTRAN ICOM 4036 Lecture 4.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
By Mr. Muhammad Pervez Akhtar
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Textbook C for Scientists and Engineers © Prentice Hall 1997 Available at NUS CO-OP at S$35.10.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Imperative Programming The Case of FORTRAN ICOM 4036 Lecture 5 READINGS: PLP Chapters 6 and 8.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 jcmt Summer 2003Programming Languages CSE3302 Programming Languages Summer 2003 Dr. Carter Tiernan.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Definition of the Programming Language CPRL
Chapter 4 – C Program Control
The Machine Model Memory
Chapter 7: User-Defined Functions II
Chapter 7: Expressions and Assignment Statements
A bit of C programming Lecture 3 Uli Raich.
GC211Data Structure Lecture2 Sara Alhajjam.
Chapter 7: Expressions and Assignment Statements
ICS103 Programming in C Lecture 3: Introduction to C (2)
Imperative Programming The Case of FORTRAN
Summary of what we learned yesterday
An Overview of C.
REPETITION Why Repetition?
Presentation transcript:

Imperative Programming The Case of FORTRAN ICOM 4036 Lecture 4

The Imperative Paradigm Computer Model consists of bunch of variables A program is a sequence of state modifications or assignment statements that converge to an answer PL provides multiple tools for structuring and organizing these steps –E.g. Loops, procedures This is what you have been doing since INGE 3016!

A Generic Imperative Program START Initialize Variables Modify Variables Converged? END yes no

Imperative Fibonacci Numbers (C) int fibonacci(int f0, int f1, int n) { // Returns the nth element of the Fibonacci sequence int fn = f0; for (int i=0; i<n; i++) { fn = f0 + f1; f0 = f1; f1 = fn; } return fn; }

Examples of (Important) Imperative Languages FORTRAN (J. Backus IBM late 50’s) Pascal (N. Wirth 70’s) C (Kernigham & Ritchie AT&T late 70’s) C++ (Stroustrup AT&T 80’s) Java (Sun Microsystems late 90’s) C# (Microsoft 00’s)

FORTRAN Highlights For High Level Programming Language ever implemented First compiler developed by IBM for the IBM 704 computer Project Leader: John Backus Technology-driven design –Batch processing, punched cards, small memory, simple I/O, GUI’s not invented yet

Some Online References Professional Programmer’s Guide to FORTRAN Getting Started with G77 Links available on course web site

Structure of a FORTRAN program PROGRAM END SUBROUTINE (args) END FUNCTION (args) END …

Lexical/Syntactic Structure One statement per line First 6 columns reserved Identifiers no longer than 6 symbols Flow control uses numeric labels Unstructured programs possible

Hello World in Fortran PROGRAM TINY WRITE(UNIT=*, FMT=*) 'Hello, world' END First 6 columns Reserved One Statement Per line Designed with the Punched Card in Mind

FORTRAN By Example 2 PROGRAM LOAN WRITE(UNIT=*, FMT=*)'Enter amount, % rate, years' READ(UNIT=*, FMT=*) AMOUNT, PCRATE, NYEARS RATE = PCRATE / REPAY = RATE * AMOUNT / (1.0 - (1.0+RATE)**(-NYEARS)) WRITE(UNIT=*, FMT=*)'Annual repayments are ', REPAY END Implicitly Defined Variables Type determined by initial letter I-M ~ INTEGER A-H, O-Z FLOAT

FORTRAN By Example 2 PROGRAM LOAN WRITE(UNIT=*, FMT=*)'Enter amount, % rate, years' READ(UNIT=*, FMT=*) AMOUNT, PCRATE, NYEARS RATE = PCRATE / REPAY = RATE * AMOUNT / (1.0 - (1.0+RATE)**(-NYEARS)) WRITE(UNIT=*, FMT=*)'Annual repayments are ', REPAY END FORTRAN’s Version of Standard Output Device

FORTRAN By Example 2 PROGRAM LOAN WRITE(UNIT=*, FMT=*)'Enter amount, % rate, years' READ(UNIT=*, FMT=*) AMOUNT, PCRATE, NYEARS RATE = PCRATE / REPAY = RATE * AMOUNT / (1.0 - (1.0+RATE)**(-NYEARS)) WRITE(UNIT=*, FMT=*)'Annual repayments are ', REPAY END FORTRAN’s Version of Default Format

FORTRAN By Example 3 PROGRAM REDUCE WRITE(UNIT=*, FMT=*)'Enter amount, % rate, years' READ(UNIT=*, FMT=*) AMOUNT, PCRATE, NYEARS RATE = PCRATE / REPAY = RATE * AMOUNT / (1.0 - (1.0+RATE)**(-NYEARS)) WRITE(UNIT=*, FMT=*)'Annual repayments are ', REPAY WRITE(UNIT=*, FMT=*)'End of Year Balance' DO 15,IYEAR = 1,NYEARS,1 AMOUNT = AMOUNT + (AMOUNT * RATE) - REPAY WRITE(UNIT=*, FMT=*)IYEAR, AMOUNT 15 CONTINUE END A loop consists of two separate statements -> Easy to construct unstructured programs

FORTRAN Do Loops PROGRAM REDUCE WRITE(UNIT=*, FMT=*)'Enter amount, % rate, years' READ(UNIT=*, FMT=*) AMOUNT, PCRATE, NYEARS RATE = PCRATE / REPAY = RATE * AMOUNT / (1.0 - (1.0+RATE)**(-NYEARS)) WRITE(UNIT=*, FMT=*)'Annual repayments are ', REPAY WRITE(UNIT=*, FMT=*)'End of Year Balance' DO 15,IYEAR = 1,NYEARS,1 AMOUNT = AMOUNT + (AMOUNT * RATE) - REPAY WRITE(UNIT=*, FMT=*)IYEAR, AMOUNT 15 CONTINUE END A loop consists of two separate statements -> Easy to construct unstructured programs Enter amount, % rate, years 2000, 9.5, 5 Annual repayments are End of Year Balance E-04

FORTRAN Do Loops PROGRAM REDUCE WRITE(UNIT=*, FMT=*)'Enter amount, % rate, years' READ(UNIT=*, FMT=*) AMOUNT, PCRATE, NYEARS RATE = PCRATE / REPAY = RATE * AMOUNT / (1.0 - (1.0+RATE)**(-NYEARS)) WRITE(UNIT=*, FMT=*)'Annual repayments are ', REPAY WRITE(UNIT=*, FMT=*)'End of Year Balance' DO 15,IYEAR = 1,NYEARS,1 AMOUNT = AMOUNT + (AMOUNT * RATE) - REPAY WRITE(UNIT=*, FMT=*)IYEAR, AMOUNT 15 CONTINUE END Enter amount, % rate, years 2000, 9.5, 5 Annual repayments are End of Year Balance E-04 optional increment (can be negative) final value of index variable index variable and initial value end label

FORTRAN Functions I PROGRAM TRIANG WRITE(UNIT=*,FMT=*)'Enter lengths of three sides:' READ(UNIT=*,FMT=*) SIDEA, SIDEB, SIDEC WRITE(UNIT=*,FMT=*)'Area is ', AREA3(SIDEA,SIDEB,SIDEC) END FUNCTION AREA3(A, B, C) * Computes the area of a triangle from lengths of sides S = (A + B + C)/2.0 AREA3 = SQRT(S * (S-A) * (S-B) * (S-C)) END No recursion Parameters passed by reference only Arrays allowed as parameters No nested procedure definitions – Only two scopes Procedural arguments allowed No procedural return values Think: why do you think FORTRAN designers made each of these choices?

FORTRAN IF-THEN-ELSE REAL FUNCTION AREA3(A, B, C) * Computes the area of a triangle from lengths of its sides. * If arguments are invalid issues error message and returns * zero. REAL A, B, C S = (A + B + C)/2.0 FACTOR = S * (S-A) * (S-B) * (S-C) IF(FACTOR.LE. 0.0) THEN STOP 'Impossible triangle' ELSE AREA3 = SQRT(FACTOR) END IF END NO RECURSION ALLOWED IN FORTRAN77 !!!

FORTRAN ARRAYS SUBROUTINE MEANSD(X, NPTS, AVG, SD) INTEGER NPTS REAL X(NPTS), AVG, SD SUM = 0.0 SUMSQ = 0.0 DO 15, I = 1,NPTS SUM = SUM + X(I) SUMSQ = SUMSQ + X(I)**2 15 CONTINUE AVG = SUM / NPTS SD = SQRT(SUMSQ - NPTS * AVG)/(NPTS-1) END Subroutines are analogous to void functions in C Parameters are passed by reference

subroutine checksum(buffer,length,sum32) C Calculate a 32-bit 1's complement checksum of the input buffer, adding C it to the value of sum32. This algorithm assumes that the buffer C length is a multiple of 4 bytes. C a double precision value (which has at least 48 bits of precision) C is used to accumulate the checksum because standard Fortran does not C support an unsigned integer datatype. C buffer - integer buffer to be summed C length - number of bytes in the buffer (must be multiple of 4) C sum32 - double precision checksum value (The calculated checksum C is added to the input value of sum32 to produce the C output value of sum32) integer buffer(*),length,i,hibits double precision sum32,word32 parameter (word32= D+09) C (word32 is equal to 2**32) C LENGTH must be less than 2**15, otherwise precision may be lost C in the sum if (length.gt )then print *, 'Error: size of block to sum is too large' return end if do i=1,length/4 if (buffer(i).ge. 0)then sum32=sum32+buffer(i) else C sign bit is set, so add the equivalent unsigned value sum32=sum32+(word32+buffer(i)) end if end do C fold any overflow bits beyond 32 back into the word 10 hibits=sum32/word32 if (hibits.gt. 0)then sum32=sum32-(hibits*word32)+hibits go to 10 end if end

WhiteBoard Exercises