SOEE1160: Computers and Programming in Geosciences Semester /08. Dr. Sebastian Rost

Similar documents
Week - 01 Lecture - 04 Downloading and installing Python

Programming with Python

AMath 483/583 Lecture 7. Notes: Notes: Changes in uwhpsc repository. AMath 483/583 Lecture 7. Notes:

AMath 483/583 Lecture 7

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Stating the obvious, people and computers do not speak the same language.

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Introduction Objectives: 1) differentiate between high-level, low-level, and machine language; 2) explain why it is necessary to translate a program

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Pseudo Code and Flow Charts. Chapter 1 Lesson 2

MAS115: R programming Lecture 2: Pseudocode Lab Class and Technical Material: Matrices, Factors, Data frames, Lists

2 A little on Spreadsheets

National 5 Computing Science Software Design & Development

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Introduction to Programming: Variables and Objects. HORT Lecture 7 Instructor: Kranthi Varala

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Flow Control: Branches and loops

Week One: Introduction A SHORT INTRODUCTION TO HARDWARE, SOFTWARE, AND ALGORITHM DEVELOPMENT

Introduction to Programming Style

How to approach a computational problem

ENT 189: COMPUTER PROGRAMMING. H/P: Home page:

Week 1 Introduction to Programming

Full file at

Introduction. Arizona State University 1

Welcome to Python! If you re the type of person who wants to know

CSC 101: Lab Manual#9 Machine Language and the CPU (largely based on the work of Prof. William Turkett) Lab due date: 5:00pm, day after lab session

Identifiers. Identifiers are the words a programmer uses in a program Some identifiers are already defined. Some are made up by the programmer:

Topics. Hardware and Software. Introduction. Main Memory. The CPU 9/21/2014. Introduction to Computers and Programming

Introduction to computers and Python. Matthieu Choplin

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

FLOW CHART AND PSEUDO CODE

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

Python: Functions. Thomas Schwarz, SJ Marquette University

C Language, Token, Keywords, Constant, variable

SNS COLLEGE OF ENGINEERING,

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Outline. Program development cycle. Algorithms development and representation. Examples.

Microprocessors & Assembly Language Lab 1 (Introduction to 8086 Programming)

INTRODUCTION 1 AND REVIEW

A quick guide to Fortran

printf( Please enter another number: ); scanf( %d, &num2);

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

5 The Control Structure Diagram (CSD)

CMSC 201 Computer Science I for Majors

Intro. Scheme Basics. scm> 5 5. scm>

Chapter 2 Basic Elements of C++

Why Study Assembly Language?

Memory Addressing, Binary, and Hexadecimal Review

C++ Programming Language Lecture 1 Introduction

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

Chapter 1: An Overview of Computers and Logic

Programming Fundamentals

St. Benedict s High School. Computing Science. Software Design & Development. (Part 2 Computer Architecture) National 5

An Introduction to Python (TEJ3M & TEJ4M)

COMP 102: Computers and Computing

2. Numbers In, Numbers Out

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Perl Basics. Structure, Style, and Documentation

Computers and Computation. The Modern Computer. The Operating System. The Operating System

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

GCSE Computer Science Component 02

Chapter 17. Fundamental Concepts Expressed in JavaScript

Chapter 1 Operations With Numbers

If you have more in-depth questions after the presentation please feel free to contact me at the e- address below.

This is the basis for the programming concept called a loop statement

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Chapter 1 INTRODUCTION TO COMPUTER AND PROGRAMMING

Section 1. The essence of COBOL programming. Mike Murach & Associates

Game keystrokes or Calculates how fast and moves a cartoon Joystick movements how far to move a cartoon figure on screen figure on screen

Working with JavaScript

CMSC 201 Fall 2018 Python Coding Standards

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Numerical Methods in Scientific Computation

Students received individual feedback throughout year on assignments.

1. Lexical Analysis Phase

CS 201 Problem Solving with Computers

بسم اهلل الرمحن الرحيم

DEVS306 Tables & Graphs. Rasa Zakeviciute

Chapter 2 Author Notes

Higher Computing Science Software Design and Development - Programming Summary Notes

Module 1: Introduction RStudio

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Business Data Analysis MA0123. Dr Gavin Shaddick Department of Mathematical Sciences 4W 5.7

Steps to program development

Visual Basic Course Pack

CITS2401 Computer Analysis & Visualisation

Python Input, output and variables. Lecture 23 COMPSCI111/111G SS 2018

And Parallelism. Parallelism in Prolog. OR Parallelism

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Bits and Bytes and Numbers

Civil Engineering Computation

6.170 Laboratory in Software Engineering Java Style Guide. Overview. Descriptive names. Consistent indentation and spacing. Page 1 of 5.

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Method & Tools for Program Analysis & Design

Engineering Computing M1H Together Towards A Green Environment

Computer Organization and Assembly Language. Lab Session 3

ENGR 105: Introduction to Scientific Computing. Dr. Graham. E. Wabiszewski

(Refer Slide Time: 01:12)

Transcription:

SOEE1160 L3-1 Structured Programming SOEE1160: Computers and Programming in Geosciences Semester 2 2007/08 Dr. Sebastian Rost In a sense one could see a computer program as a recipe (this is pretty much true for any interaction with a computer). You could type your commands into the keyboard and the operating system encodes then and then passes them to the CPU and the results come back to you onto the screen. This works pretty well, but is probably not the best use of a computer. Why? Computers normally calculate much faster (at the rate of a couple of billion instructions per second (naturally depending on the speed of the computer)) than we can (I guess that is the reason why we use them). To get instructions to a computer at this speed typing won t do. We must assemble a list of instructions in advance and then get them to the computer by normally loading them into the computers memory. The CPU can then read and execute these at its own pace (which is normally pretty high). For a computer to understand the instructions they must be in a certain format or language. It is actually not necessary to write in a language that the computer understands directly since there is normally a layer between you and the CPU that actually translate your commands into something the computer understands. So, the computer program contains step-by-step instructions of what the computer should do. The computer normally works through the program from the top to the bottom, but computer programs also allow control of flow which means you can actually tell the computer to repeat part of the programs several times or to give conditions under which a part of the computer code should be run. Anything you want the computer to do has to be written in the recipe. Computers don t interpret what you might have done, they follow strictly the recipe. Normally you cannot blame the computer when things go wrong. Computer languages are lists of instructions that tell the computer what to do. As the CPU can only understand instructions written in binary code, the program passed to the CPU must be in binary code, or be executable. It is possible to write computer programs in binary code, but this is difficult and unnecessary. High Level languages have been developed, which deal with the concepts and abstractions needed for the user s problems. Typical examples of high-level languages are FORTRAN and C. So you will learn one of these during this class, which makes it easier to learn other languages as well.

SOEE1160 L3-2 Important parts of a Computer program A program can only instruct a computer to: Read Input Sequence Calculate Store data Compare and branch Iterate or Loop Write Output We will discuss what this means in the lecture. Pseudo-code and structure diagrams One of the first steps you should do when you write a computer program is to plan what it should do and how the structure of the code should look like. This normally makes code that is easier to read and to understand. The programmer should break down the task into simple stages. These should be expressed in pseudo-code, using normal English to express the steps required for a task Start by noting the overall tasks to be achieved then break these down into sub-tasks. The idea is to continue re-writing the instructions with the tasks resolved into smaller and smaller units until each sub-task is trivial enough to be represented by one high-level computer instruction. Later on we will see that good design at this stage gets carried through into the subprogram structure of the finished code, making it clear and easy to maintain. If you start your own code this is very good practice, together with documenting the code well through comments and using indentations to make the flow of the program clear. For example, let s assume the task to be carried out is to make some sandwiches. We might decide the steps are: 1. Get the ingredients out 2. Make the sandwiches 3. Tidy up Breaking each step down into smaller parts gives: 1.1 Get the bread out 1.2 Get the jam out 1.3 Get the butter out 2.1 Slice the bread 2.2 Put the butter and jam on

SOEE1160 L3-3 2.3 Cut the bread in half and put the halves together Put in the sandwich box Tidy up Note how the numbering indicates the subdivisions. In real applications, sections on the instructions may need to be repeated many times (e.g. when you want to make more than one slice). We can represent this using For each end of in our pseudo-code. 1.1 Get the bread out 1.2 Get the jam out 1.3 Get the butter out 2.1 For each slice of bread 2.1.1 Slice the bread 2.1.2 Put the butter and jam on 2.1.3 Cut the bread in half and put the halves together 2.1.3 Put in the sandwich box end of this slice 3. Tidy up. Some instructions may need to be executed only if a certain condition is true. We can represent this using if end of if in our pseudo-code. We can include or else to specify what actions should happen if the condition is not true. 1.1 if the bread-bin is empty 1.1.1 go to the shop and buy another loaf or else 1.1.2 Get the bread out end of if 1.2 Get the jam out. Logic Flowcharts Another way to represent the logic within a computer program is to use structure diagrams or flow charts. In these, different symbols show the parts of the computer program and flow lines connecting the different parts. To make things in the flow charts more obvious one uses special forms in the graphical display. A list of these symbols is given below. There are three ways a task can be broken down, into a sequence of sub-tasks, where the boxes drawn below are all executed in order; conditionals are represented as diamonds,

SOEE1160 L3-4 and repetitions are normally based on some kind of conditional and then a loop back towards the start. Processing steps are shown as boxes and input output parts are shown as trapezes. This way the logic of a program is very easy to understand and the different processing steps are obvious. Not all of the symbols actually have to appear within a computer program. The minimum number of steps in any useful program are probably 3: an input, a processing stage and an output stage. Below is an example of how a logic flowchart for a cash register would look like. Common Flowchart Terminator. Shows the starting and ending points of the program. A terminator has flowlines in only one direction, either in (a stop node) or out (a start node). Data Input or Output. Allows the user to inputdata and results to be displayed. Processing. Indicates an operation performed by the computer, such as a variable assignment or mathematical operation. Decision. The diamond indicates a decision structure. A diamond always has two flowlines out. One flowlineout is labeled the yes branch and the other is labeled the no branch. Predefined Process. One statement denotes a group of previously defined statements. For instance, Calculate m! indicates that the program executes the necessary commands to compute m factorial. Connector. Connectors avoid crossing flowlines, making the flowchart easier to read. Connectors indicate where flowlines are connected. Connectors come in pairs, one with a flowline in and the other with a flowline out. Off-page connector. Even fairly small programs can have flowcharts that extend several pages. The off-page connector indicates the continuation of the flowchart on another page. Just like connectors, off-page connectors come in pairs. Flowline. Flowlines connect the flowchart symbols and show the sequence of operations during the program execution.

SOEE1160 L3-5 Start sum=0 Input price sum=sum+price Yes More items? No tax=sum x 0.0725 total=sum+tax Output sum, tax, and total Stop Introducing Fortran Fortran was the first widely used high-level language. Originally designed in the early 1950 s for scientific and engineering applications, Fortran stands for Formula Translation. The intention was that mathematical expressions could be coded in more or less the same was as they would be written. There is a very large base of Fortran code, with a large number of programmers and users, mostly in the fields of science and engineering. This ensures that, although new versions of Fortran are being introduced, they retain the ability to compile older Fortran codes without modification. Many codes in the geosciences are written in an older standard called Fortran 77. That s the reason why we will learn Fortran by the Fortran 77 standard. The American National Standards Institute (ANSI) produced the first standardized version in 1966 (FORTRAN 66) later replaced by FORTRAN 77. Modern versions are Fortran 90, Fortran 95, Fortran 2000, Fortran 2003 and the new standard of Fortran 2008 is looming at the horizon. Some of these newer standards actually saw only rather minor changes, especially in the areas of interest here (i.e. the level of coding you will do here), but the jump from 77 to 90/95 was actually pretty major and if you start a new piece of larger code you might want to think of using Fortran 90/95. Fortran was designed to be input via punchcards, one for each statement or line of data. Shown below is one of the punchcards of a Fortran code (http://www.wikipedia.org)

SOEE1160 L3-6 Each card had 80 positions available for punching. This led to Fortran 77 fixed-format source-code, with particular columns assigned to particular uses, and to conventional data layout using 80-character long records. The table below shows what can be written in each column of a Fortran program. Some editors (e.g. emacs) actually notice when you are writing in Fortran and put in the right tabs for this format. Column Content Comment 1 C or * With a C or * the line is marked as a commentary line. You can put any text in here 1-5 Number between 1 and 99999 Statement labels 6 Empty This is the normal case 6 + or & 7 to 72 Fortran Code 73 to 80 This continues a line, i.e. you can write additional content into the next file and it will be read together with the first line Fortrans are to be written in columns 7 through 72. No need to left justify the commands These lines will not be used by FORTRAN Newer standards starting with Fortran 90 did get rid of the fixed-format since punch cards are no longer. But as mentioned before the modern compilers (more about that in a

SOEE1160 L3-7 minute) understand the legacy format of Fortran 77 and won t shout at you for using the older format. How can you write something in Fortran? The Fortran character set consists of: Letters A-Z (uppercase and lowercase, but not case sensitive) Decimal digits 0-9 Arithmetic operators + - * / and = Brackets () Comma and Period,. Colon : Apostrophe Currency symbol $ Blank (Space) So you actually don t have too many characters available. Fortran is not case sensitive, so a command WRITE is the same as write. Using upper case and lower case characters might help with the readability (for a human) of the code. Spaces are actually again a very important part of the code since they clarify the structure of the code and they are needed to separate things. More about this in the next lecture. We call the Fotran program you are writing the source code or simply the code. The source code is written in ASCII in an editor of your choice. So how is the computer actually able to understand what you wrote in the source code and to execute the recipe you wrote? Directly the computer does not understand a thing you wrote in that file. It is simply an ASCII file written in a specific format. You have to compile the code for it to be useful to the computer. The compiler then translates the code into binary that can be understood and executed by the CPU. Compiling is very different from what you did to your C-shell script with the chmod command. There, you only told the operating system that the file is executable. The shell then translated the commands you wrote in the C-shell script to binary code on the fly. So there is no compilation step involved. It is simply a translating stage. Things are different for Fortran code which needs to be compiled. The compiled binary of a Fortran program actually lives in its own file. This file is written in binary, which can be understood by the computer, but no longer by a human. There are several Fortran compilers out there. They more or less follow the published Fortran standards, but each one has its own little interpretation of these standards. So code that compiles well with one compiler might have to be changed a little when you

SOEE1160 L3-8 use another compiler. A common compiler for Fortran under Linux is gfortran which is the fortran compiler from the GNU initiative. How to compile a code in the EFC Lab? To compile a code in the EFC lab please use the g77 which is the gfortran compiler for Fortran 77. Fortran code should have the extension.f, e.g. hello.f. That way it is clear that the content is a Fortran code and the editor has a chance to actually fit the right format to the file (when you are using emacs). Some editors actually use syntax color coding that might show which part is a variable, a command, etc. Again more about this later. Syntax for the Fortran compiler: g77 input.f If you do this, the executable binary will be a file called a.out. This is the default output file of g77. If you compile another Fortran code, this executable will be overwritten. So this is probably not desirable. Therefore there is a way tom define an output file: g77 o output input.f Now your binary lands in the file output. You can now run this file. There is no need for an extension for these files (so no.exe or something is necessary, the shell checks if things are executable by looking at the rwx fields).