CHAPTER 1 INTRODUCTION TO COMPUTING 1.1 Introduction.

Slides:



Advertisements
Similar presentations
1 Chapter 2 Basic Elements of Fortran Programming.
Advertisements

Introduction to C Programming
Introduction to C Programming
23 March, 2000 CS1001 Lecture 5 Completion of Lecture 4 –Talked about Data Types & Arithmetic Errors –Continue with Operations and Functions –Program Testing.
Introduction to Computers and Programming. Some definitions Algorithm: –A procedure for solving a problem –A sequence of discrete steps that defines such.
Chapter 2 Basic Elements of Fortan
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Data types and variables
2.4 The Assignment Statement Values are assigned to variables using the assignment statement : variable = expression Examples: Ex.1: REAL :: XCoordinate,
Chapter 2 Data Types, Declarations, and Displays
Introduction to C Programming
21 March, 2000 CS1001 Lecture 4 Data Types + Algorithms = Programs Data Types & Arithmetic Errors Arithmetic Operators and Functions Program Testing Tips.
1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.
Basic Elements of C++ Chapter 2.
Computer Science 101 Introduction to Programming.
A First Book of ANSI C Fourth Edition
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Computer Science 101 Introduction to Programming.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
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,
1 CSC103: Introduction to Computer and Programming Lecture No 6.
Input, Output, and Processing
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
1 Week 2 n Organizational matters n Fortran 90 (subset F): Basics n Example programs in detail.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Introduction to Programming with RAPTOR
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Chapter 2 Variables.
Constants, Variables and Data types in C The C character Set A character denotes any alphabet, digit or special symbol used to represent information.
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)
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
CISC105 – General Computer Science Class 2 – 6/7/2006.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
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.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Basic Elements of C++.
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
The Selection Structure
Basic Elements of C++ Chapter 2.
INPUT & OUTPUT scanf & printf.
Introduction to C++ Programming
Chapter 2 Variables.
Core Objects, Variables, Input, and Output
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2 Variables.
DATA TYPES There are four basic data types associated with variables:
DATA TYPES AND OPERATIONS
Presentation transcript:

CHAPTER 1 INTRODUCTION TO COMPUTING 1.1 Introduction

1.2 Computer Organization The main components of a computer machine are: Central Processing Unit (CPU): it is the heart of the machine, and it controls all the operations and performs all the arithmetic and logic operations. Memory Unit: this unit stores instructions and data. Control Unit: This unit fetches instructions of the Memory Unit, decodes them and directs the system to execute the operations indicated by the instructions. Arithmetic-Logic Unit (ALU): This unit is part of the CPU and carries out arithmetic and logical operations. The Memory Unit consists of several components: one of these components is used to store the instructions and data of the programs being executed and has many names, including internal, main, primary, and random access memory (RAM).

Control UnitArithmetic Logic Unit Main Memory Input Devices Output Devices External Memory Figure 1: basic computer components

1.3 Computer Language In the very early age of computers, computer programs were written using the machine language. The machine language words are some sort of strings having two parts: opcode and operand and each consist of either 0 or 1 numerals. Later, it became possible to write programs in assembly language, which uses names in place of numeric opcodes and variable names in place of numeric addresses. Nowadays, most computer programs are written in a high-level-language such as FORTRAN, and a compiler translates each statement in the program into a sequence of basic machine (or assembly language) language instructions. FORTRAN word is an abbreviation for “FORmula TRANslator” and was one of the first high-level languages to gain acceptance worldwide

1.4 Programming and Problem Solving A minimum of five steps can be identified in the program development process: 1. Problem analysis and specification 2. data organization and algorithm design 3. program coding 4. execution and testing 5. program maintenance

BASIC FORTRAN 2.1 Introduction FORTRAN provides five basic data types: INTEGERREALCOMPLEXCHARACTERLOGICAL

2.2 Data Types, Constants, and Variables: Identifiers Identifiers must begin with a letter, which may be followed by up to 30 letters, digits, or underscores. Thus, Identifiers must begin with a letter, which may be followed by up to 30 letters, digits, or underscores. Thus,MassRateVelocitySpeed_of_Light Are valid FORTRAN identifiers, but the following are invalid for the reasons indicated: R2-D2 (Only letter, digits, and underscores are allowed in identifiers) 6Feet (Identifiers must begin with a letter).

Integer costants An integer is a whole number (positive, negative, or zero) and may be represented in FORTRAN by a string of digits that does not contain commas or a decimal point; negative integer constants must be preceded by a minus sign, but a plus sign is optional for nonnegative integers. Thus 0, 137, -2516, are valid integer constants

whereas the following are invalid for the reasons indicated: 9,999commas are not allowed in numeric constants 16.0 integer constants may not contain decimal points --5only one algebraic sign is allowed 7-the algebraic sign must precede the string of digits.

Real constants Real is another data type and it can be represented as ordinary decimal numbers or in exponential notation. When representing a real constant using decimal numbers, a decimal point must be present, but no commas are allowed. Negative real constants must be preceded by a minus sign, but the plus sign is optional for nonnegative reals. Thus, are valid real constants, whereas the following are invalid for the reasons indicated: 12,345(Commas are not allowed in numeric constants) 63 (Real constants must contain a decimal point)

Real constants The exponential representation of a real constant consists of an integer or decimal number representing the mantissa or fractional part, followed by an exponent written as the letter E with an integer constant following. For example, the real constant may also be written as E2 which means x 102, or it may be written in a variety of other forms, such as E E E E-3

Character Strings Character constants, also called strings, are sequences of symbols from the FORTRAN character set. The ANSI standard character set for FORTRAN is given in the table below:

MeaningCharacterMeaningCharacter Colon:digits 0, ….., 9 Equal sign=uppercase letters A, ….., Z Exclamation mark !lowercase letters a, ….., z Ampersand&Apostrophe (single quote) ' Dollar sign$double quote" Semicolon;Left parenthesis( Less than<Right parenthesis) Greater than>Asterisk* Percent symbol%Plus sign+ Question mark?Minus sign- Comma,Slash/ Period.Blank or spaceblank

Variables When a variable is used in a FORTAN program, the compiler associated it with a memory location. The value of a variable at any time is the value stored in the associated memory location at that time. Variable names are identifiers; thus, they must follow the rules for identifiers. In FORTRAN programming the type of any variable used has to be declared. INTEGER : : list REAL :: list A comma must separate the names in the list. For instance, the statements: INTEGER :: NumValues, Factorial, Sum REAL :: Mass, Velocity

Variables CHARACTER (LEN = n ) : : list Or in a simpler form: CHARACTER(n) : : list CHARACTER (LEN = 15) : : FirstName, LastName Or CHARACTER(15) : : FisrtName, LastName CHARACTER(15) : : FirstName, LastName*20, Initial*1, Street, City

IMPLICIT NONE Statement FORTRAN Implicit naming convention is applied to any variable whose type is not explicitly declared in a type statement. The convention says that: any undeclared identifier whose name begins with I, J, K, L, M, or N or their lowercase equivalents will be typed as integer, and all other will be types as real. FORTRAN 90 provides the statement IMPLICIT NONE to cancel this naming convention. It should be used in every program to guard against errors caused by implicit typing of a variable to have a type different than what was intended. When using this statement all the types of all named constants and variables (and functions) must be specified explicitly in type statements. Otherwise, when using any undeclared variable (or function) without declaring it, it will be considered an error by the compiler.

Variable Initialization As all variables in FORTRAN are initially undefined, initial values can be assigned to variables (at compile time) in their declarations. For example, to initialize the values for variables W, X, Y, and Z to 1.0, 2.5, 7.73, and , respectively, one could use the following statement: REAL :: W = 1.0, X = 2.5, Y = 7.73, Z =

Named Constants: the PARAMETER Attribute Type-specifier, PARAMETER :: list Examples: INTEGER, PARAMETER :: Limit = 50 REAL, PARAMETER :: Pi = , TwoPi = 2.0 * Pi CHARACTER(2), PARAMETER :: Units = “cm” The last declaration example can also be written CHARACTER(*), PARAMETER :: Units = “cm” XCoordinate = Rate * Cos(TwoPi * Time)

2.3 Numeric Operations The following characters are used in FORTRAN as denoted: * multiplication, -subtraction + addition /division ** exponentiation For example, the equality B2 – 4 A C is written as B ** 2 – 4 * A * C

Priority Rules  All exponentiations are performed first; consecutive exponentiations are performed from right to left.  All multiplications and divisions are performed next, in the order in which they appear from left to right.  The additions and subtractions are performed last, the order in which they apear from left to right.

The following examples illustrate these priorities: 2 ** 3 ** 2 = 2 ** 9 = – 8 – 2 = 2 -2 = 0 10 / 5 * 2 = 2 * 2 = / 2 = = ** 2 / 2 = / 2 = 2+ 8 = 10 Parentheses can be used to modify the standard order of evaluation, for example: (5 * (11 – 5) ** 2) * = (5 * 6 ** 2) * = 180 * = = 729 (5 * (11 – 5) ** 2) * = (5 * 6 ** 2) * = 180 * = = 729

Mixed-Mode Expressions / 5 »» »» / 5.0 »» »» 4.6 The last two examples show the use of mixed-mode operations is considered as a poor practice. Note the following two expressions 2.0 ** 3 »» 2.0 * 2.0 * 2.0 »» 8.0 (-4) ** 2 »» (-4) * (-4) »» 16

More Examples 7.0 ** (1.0 / 2.0) »» 7.0 ** (0.5) 7.0 ** ( 1 / 2 ) »» 7.0 ** 0 = 1.0

Table 2.2 Some FORTRAN functions Description Function Absolute value of xABS(x) Cosine of x radiansCOS(x) Exponential functionEXP(x) Integer part of xINT(x) M of x part (mantissa) of xFRACTION(x) Natural logarithm of xLOG(x) Maximum of x1, ….., x n MAX(x1, ….., x n ) Minimum of x1, ……., x n MIN(x1, …., x n ) x(mod y): x – INT(x/y) * y MOD(x, y) x rounded to nearest integerNINT(x) Conversion of x to real typeREAL(x) Sine of x radiansSIN(x) Square root of xSQRT(x) Tangent of x radiansTAN(x)

Examples on Numeric Functions SQRT(7.0) SQRT(B ** 2 – 4 * A * C) X ** (1.0 / REAL (N)) REAL(NUM) ** (1.0 / REAL(N))

2.6 Input / Output FORTRAN provided two types of input/output statements: list-directed and formatted. The list directed method is simpler and has the following simplest form: PRINT *, output-list Or WRITE (*, *) output list Examples: PRINT *, “At time”, Time, “seconds” PRINT *, “the vertical velocity is”, Velocity, “m / sec” PRINT *, “and the height is”, Height, “meters”

2.6 Input / Output The PRINT and WRITE statement may be used to produce a blank line of output as shown in the following example: PRINT * Or WRITE ( *, *) The simplest form of list-directed input statement is READ *, input-list Or READ (*, *) input-list

Example: The following line of input data 100.0, 90.0, 4.5 can be used to respond to the following statement: READ*, InitialHeight, InitialVelocity, Time That means value will be assigned to the variable InitialHeight, the value 90.0 will be assigned to the variable InitialVelocity and the value 4.5 will be assigned to the variable Time. Spaced can also be used as separators in place of the commas so that the line of input data will be as follows: or more than one line of data could be used: 100.0,

2.7 Program Composition and Format The general form of a FORTRAN computer program is:  heading  Specification part  Execution part  Subprogram part  END PROGRAM statement The program heading has the form PROGRAM name

execution part of a program. The last statement in every program must be the END PROGRAM statement. This statement indicates to the compiler the end of the program. It also halts execution of the program. Execution can also be terminated with a STOP statement of the form

Program Format In FORTRAN90 programming the main rules that must be followed are: A line may have a maximum of 132 characters. A line may contain more than one statement, provided the statements are separated by semicolons (;). It is good practice, however, to consider at most one statement per line. An ampersand (&) must be placed at the end of each line that is to be continued to the next line. At most 39 continuation lines are permitted. : for example: PRINT *, “Enter the initial height (m) and & &the initial velocity (m/sec): “ Any characters following an exclamation mark (!)— except within a string constant—and running to the end of the line form a comment. For example, INTEGER :: Number! Number of data values read If a statement requires a statement label, this label must precede the statement and must be separated from it by at least one blank. Statement labels must be integers in the range 1 through

2.8 Types of Errors There are different Errors that a programmer may face during compilation or running of a computer program. Errors in the program’s syntax, such as incorrect punctuation or misspelled key words, will be detected during compilation. Such errors are called syntax errors or compile-time errors and usually make it impossible to complete the compilation and execution of the program. Other errors, such as an attempt to divide by zero in an arithmetic expression, may not be detected until execution of the program has begun. Such errors are called run-time errors. There are other errors that are more subtle and difficult to identify. These are logic errors that arise in the design of the algorithm or in the coding of the program that implements the algorithm.

Examples