1 Week 2 n Organizational matters n Fortran 90 (subset F): Basics n Example programs in detail.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Introduction to arrays
1 Chapter 2 Basic Elements of Fortran Programming.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
17 March, 2000 CS1001 Lecture 2 Programming and problem solving Software engineering practices.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Chapter 2 Basic Elements of Fortan
Data types and variables
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
Chapter 6: User-Defined Functions I
Chapter 2 Data Types, Declarations, and Displays
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
CHAPTER 6 FILE PROCESSING. 2 Introduction  The most convenient way to process involving large data sets is to store them into a file for later processing.
21 March, 2000 CS1001 Lecture 4 Data Types + Algorithms = Programs Data Types & Arithmetic Errors Arithmetic Operators and Functions Program Testing Tips.
FORTRAN PROGRAMMING BASICS MET 50. Programming Basics The basic layout of all programs is as follows (p.33) PROGRAM name = heading (i.e., title) Specifications.
1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Objectives You should be able to describe: Data Types
Python Programming Fundamentals
Fortran 1- Basics Chapters 1-2 in your Fortran book.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Fundamental Programming: Fundamental Programming Introduction to C++
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Lecture III Start programming in Fortran Yi Lin Jan 11, 2007.
CHAPTER 1 INTRODUCTION TO COMPUTING 1.1 Introduction.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CISC105 – General Computer Science Class 2 – 6/7/2006.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 Constants A constant is a fixed value of a data type that cannot be changed Integer Constants Whole numbers → Do not have decimal points Examples: 83.
CHAPTER 2:BASIC FORTRAN Data Types INTEGER REAL COMPLEX CHARACTER LOGICAL.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Chapter 2 C++ Syntax and Semantics, and the Program Development Process Topics – Programs Composed of Several Functions – Syntax Templates – Legal C++
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
7 - Programming 7J, K, L, M, N, O – Handling Data.
Bill Tucker Austin Community College COSC 1315
Chapter 6: User-Defined Functions I
BASIC ELEMENTS OF A COMPUTER PROGRAM
ITEC113 Algorithms and Programming Techniques
The Selection Structure
Variables, Expressions, and IO
User-Defined Functions
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES AND OPERATIONS
Presentation transcript:

1 Week 2 n Organizational matters n Fortran 90 (subset F): Basics n Example programs in detail

2 Top-down programming n 4 basic steps –Specify the problem clearly –Analyse the problem and break it down into its fundamental elements –Code the program according to the plan developed at step 2 –Test the program exhaustively, and repeat steps 2 and 3 as necessary until the program works in all situations that you can envisage

3 Program = Data Types + Algorithms n Data types: what you work on n Algorithms: what you do with them

4 Structure of a program (in Fortran 90) Heading ( program, module, etc.) n specification part n execution part n subprogram part end program statement

5 program Radioactive_Decay ! ! This program calculates the amount of a radioactive substance that ! remains after a specified time, given an initial amount and its ! half-life. Variables used are: ! InitalAmount : initial amount of substance (mg) ! HalfLife : half-life of substance (days) ! Time : time at which the amount remaining is calculated (days) ! AmountRemaining : amount of substance remaining (mg) ! ! Input: InitialAmount, HalfLife, Time ! Output: AmountRemaining ! implicit none real :: InitialAmount, HalfLife, Time, AmountRemaining ! Get values for InitialAmount, HalfLife, and Time. print *, "Enter initial amount (mg) of substance, its half-life (days)" print *, "and time (days) at which to find amount remaining:" read *, InitialAmount, HalfLife, Time ! Compute the amount remaining at the specified time. AmountRemaining = InitialAmount * 0.5 ** (Time / HalfLife) ! Display AmountRemaining. print *, "Amount remaining =", AmountRemaining, "mg" end program Radioactive_Decay

6 Data Types n Five basic types: –integer –real –complex –character –logical n Data types of ‘container’ classes

7 Integers n a whole number (positive, negative or zero) n no decimal point Examples

8 Reals n Numbers with decimal fractions n There has to be decimal point Examples Another representation: 1.952e e e-8

9 Character n Sequence of symbols from the Fortran character set n Enclosed between double quotes Examples "This is a string" "I do, I don't" "1234abc345"

10 Logical n Can take only two values:.TRUE..FALSE.

11 Identifiers n Names used to identify programs, constants, variables, etc. n Identifiers must Begin with a letter n This can be followed by up to 30 letters, digits, undescores n Be careful with the case: lower or upper case letters

12 Identifiers Examples Current Decay_Rate pressure an_identifier_with_a_long_name the_best_program

13 Constants n is an integer constant n is a real constant n "What a nice day!" is a character constant

14 Variables: Variables: It is usual for people to associate a name or phrase with a piece of information. For example, the phrase "today's date" has an associated numeric value which varies day by day. This is similar to the concept of a program variable; a program variable is some object (named by the programmer) which uniquely identifies a piece of data stored in memory. n Variables are value containers n Compiler associates with a variable a memory location n Value of a variable at any time is the value stored in the associated memory location at that time

15 Variables MEMORY Hello World Message Payment

16 Declarations of Variables Form: type-specifier :: list n Declares that the identifiers in the list have the specified type n Type statements must appear in the specification part of the program Examples integer :: number_years, counts, months real :: Mass, Velocity, Acceleration character (len=12) :: MyName, YourName

17 implicit none n It should be placed at the beginning of the specification part n You have to declare all variables you will be using in the program!

18 Variable initialization n All variables are initially undefined Initialization in the declarations Examples: real :: W=1.2, z=5.678, mass=4.56 integer :: year=1998, count=0

19 Named constants Form: type-specifier, parameter :: list Examples integer, parameter :: INITCOUNT = 30 real, parameter :: G = 9.81 n It's a good idea to write named constants in upper case

20 Arithmetic operations n Variables and constants can be processed by using operations and functions appropriated to their types. n Operations

21

22 Operations Examples To calculate B 2 - 4AC B**2 - 4*A*C n Types are important: 9/4 = 2 9.0/4.0 = 2.25 Mixed-mode expressions: /5  /5.0    4.6

23 Priority rules n All exponentiations are performed first; consecutive exponentiations are performed from right to left n All multiplications and divisions are performed next; in the order in which they appear from left to right n Additions and subtractions are performed last, in the order in which they appear from left to right

24 Some examples 2 ** 3 ** 2 = 512 n 10/5 *2 = 2 * 2 = 4 n To calculate 5 1/3 5.0**(1.0/3.0) but not 5.0**(1/3)  5.0**0  1.0

25 Library functions abs(x) Absolute value of x cos(x) Cosine of x radians exp(x) Exponential function int(x) Integer part of x sqrt(x) Square root of x

26 Assignment statement Form: variable = expression Assigns the value of expression to variable Assignment is not a statement of algebraic equality; it is a replacement statement Examples Density = Volume = 3.2 Mass = Density*Volume WeightRatio = log(Mass/90.)

27 Programs need to communicate with users! n Two kinds of I/O (for the moment!): –Formatted I/O –List-directed I/O List-directed output print *, output-list write (unit=*, fmt=*) output-list List-directed input read *, input-list read (unit=*, fmt=*) input-list

28 List-directed I/O Examples print *, "Tell me your birthday" write (unit=*, fmt=*) a, b, c**2 read *, day, month, year

29

30

31

32

33

34

35

36

37

38