Computational Physics Operating systems

Similar documents
Computational Physics Compiling a C++ program

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Computer Programming : C++

6.096 Introduction to C++ January (IAP) 2009

A First Program - Greeting.cpp

CHAPTER 3 BASIC INSTRUCTION OF C++

VARIABLES & ASSIGNMENTS

Creating a C++ Program

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CS242 COMPUTER PROGRAMMING

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Chapter 1 Introduction to Computers and C++ Programming

Lecture 3. More About C

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Introduction to Programming

Chapter 2: Overview of C++

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

OBJECT ORIENTED PROGRAMMING

BASIC ELEMENTS OF A COMPUTER PROGRAM

Chapter 1 INTRODUCTION

LECTURE 02 INTRODUCTION TO C++

Computational Physics

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

CSCE 110 PROGRAMMING FUNDAMENTALS

JAVA OPERATORS GENERAL

Tutorial-2a: First steps with C++ programming

REVIEW. The C++ Programming Language. CS 151 Review #2

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Operators in C. Staff Incharge: S.Sasirekha

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

DEPARTMENT OF MATHS, MJ COLLEGE

C++ Programming: From Problem Analysis to Program Design, Third Edition

Lecture 3 Tao Wang 1

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Operators & Expressions

Programming in C++ 5. Integral data types

A flow chart is a graphical or symbolic representation of a process.

Chapter 2: Using Data

UNIT- 3 Introduction to C++

Objectives. In this chapter, you will:

CS1500 Algorithms and Data Structures for Engineering, FALL Virgil Pavlu, Jose Annunziato,

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

The C++ Language. Arizona State University 1

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

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

SECTION II: LANGUAGE BASICS

Data types Expressions Variables Assignment. COMP1400 Week 2

Fundamentals of Programming CS-110. Lecture 2

Introduction to Programming using C++

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Lecture 2 Tao Wang 1

Introduction to C# Applications

CS2141 Software Development using C/C++ C++ Basics

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Introduction to Programming EC-105. Lecture 2

CS313D: ADVANCED PROGRAMMING LANGUAGE

Computers Programming Course 6. Iulian Năstac

Scientific Computing

2 nd Week Lecture Notes

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

ISA 563 : Fundamentals of Systems Programming

The Arithmetic Operators

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Quick Reference Guide

BITG 1233: Introduction to C++

Basic Types, Variables, Literals, Constants

Computer Programming CS F111

Computational Physics

CS 376b Computer Vision

bphanikrishna.wordpress.com

Reserved Words and Identifiers

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Preview from Notesale.co.uk Page 6 of 52

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Prof. Navrati Saxena TA: Rochak Sachan

1.3b Type Conversion

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Unit 3. Constants and Expressions

Programming. C++ Basics

Professor Peter Cheung EEE, Imperial College

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Quick Reference Guide

Transcription:

Computational Physics numerical methods with C++ (and UNIX) 2018-19 Fernando Barao Instituto Superior Tecnico, Dep. Fisica email: fernando.barao@tecnico.ulisboa.pt Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (1) Computational Physics Operating systems UNIX (linux) Fernando Barao, Phys Department IST (Lisbon) Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (2)

Operating systems Many linux distributions: - Debian - Ubuntu (& derivatives) - Linux Mint - Fedora Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (3) Computer programming Symbolic languages use words ( add, move,...) instead of operation codes High-level symbolic languages: FORTRAN FORmula TRANslator mid 1950 s BASIC Beginner s All-purpose Symbolic Instruction Code mid 1960 s PASCAL early 1970 s C mid 1970 s C++, Java,... mid 1980 s on C and C++ allow the manipulation of bits and bytes and memory addresses (some people tag it as mid-level languages) Other languages like Mathematica, Matlab or Maple: very rapid coding up but...code is interpreted (slower) The lowest level symbolic language is called the assembly language The assembler program translates the assembly into machine code (object code) that will be understood by the CPU Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (4)

Computer programming Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (5) Creating an executable An executable file contains binary code encoding machine-language instructions To create it, we need to start by writing a program in a symbolic language, the source code use some Unix editor like pico, gedit, emacs Next, we produce the object code, by compiling the source code and eventually linking with other pieces of code located in libraries or being compiled at the same time compilers: C++ g++, c gcc, FORTRAN gfortran the compiler assigns memory addresses to variables and translates arithmetic and logical operations into machine-language instructions The object code is loaded into the memory (RAM) and it is runned by the CPU (no further need of the compiler) the object files are specific to every CPU and are not necessarily portable across different versions of the operating system Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (6)

Computational Physics C++ An object oriented language Fernando Barao, Phys Department IST (Lisbon) Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (7) C... Programming languages The C language was originally developed by computer scientists to write operating systems. It is considered a flexible and very powerful language. All UNIX operating systems are written in C. Although C is a high-level language, it incorporates many comparatively low-level features, as pointers. The C++ language is a major extension of C with the purpose of exploring the object-oriented programming. Object-oriented lamguages are well suited to large projects involving many people. But it requires some thinking about the problem before implementation... Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (8)

Computer languages: a possible ranking... Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (9) C++ general rules C++ is case sensitive A C++ statement may begin at any place in the line and can continue into the next line The end of the statement is indicated by a semicolon ; There can be multiple staements in a line int a=5; int b=10; Comments to code can be inserted by using // int a=5; //... A large part of the code can be commented using /*...*/ The name of a variable must start with a letter and shal contain only letters, numbers and underscore _ Every C++ program has a main function 1 #define PRINT 2 #include <iostream> 3 int main() { 4 int a = 5; 5 std::cout << a << std::endl; 6 return 0; //successful return (can be omitted) 7 } Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (10)

C++ data types A variable has allways to be declared in order the appropriate space is reserved in memory by the compiler Once declared, a numerical variable can be initialized or evaluated 1 / / i n t e g e r s 2 i n t a = 5; 3 i n t a ; a=5; 4 i n t a ( 5 ) ; 5 unsigned i n t year ; / / p o s i t i v e i n t e g e r 6 7 / / characters 8 char a = 66; / / B (66 = i n t code ) 9 char a = B ; / / s i n g l e quotes 10 11 / / constants 12 const i n t a = 5; / / cannot be modified 13 14 / / r e a l s 15 f l o a t b = 10.50; / / s i n g l e p r e c i s i o n 16 f l o a t b = 1.05e+1; 17 double p i = 3. 1 4 1 5 9 2.... ; / / double prec 1 / / boolean vars 2 bool f l a g = true ; / / or f a l s e 3 4 / / s t r i n g s (C++ std l i b ) 5 s t r i n g name = " a l b e r t o " ; 6 s t r i n g name( " a l b e r t o " ) ; 7 8 / / character s t r i n g s 9 char word [ 2 0 ] = " f o u r " ; 10 / word [ 4 ] = \ 0 ( n u l l character ) 11 the n u l l character i s a u t o m a t i c a l l y 12 added to the end of the character 13 s t r i n g enclosed i n double quotes / Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (11) C++ data types (cont.) Type Description Byte size short int short integer 2 short ranges from -32768 to 32767 signed short int ranges from -32768 to 32767 unsigned short int ranges from 0 to 65535 int integer 4 signed int ranges from -2147483648 to 2147483647 unsigned int ranges from 0 to 4294967295 float floating point number, single precision 4 double floating point number, double precision 8 long double floating point number, long double precision 12 bool boolean value, true or false 1 char character 1 signed char one byte integer from -128 to 127 unsigned char one byte integer from 0 to 255 Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (12)

C++ data structures A data structure groups a set of characteristics of a given object (it is the prelude of a class in C++) 1 #include < s t r i n g > 2 using namespace std ; 3 4 / / d e f i n e s t r u c t u r e 5 struct alunoist { 6 s t r i n g name ; / / nome 7 f l o a t mark ; / / nota 8 } ; 9 10 i n t main ( ) { 11 alunoist A ; 12 A. name = " Joao " ; 13 A. mark = 2 0. 0 ; 14 } Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (13) C++ operators arithmetic + sum a == b logical equal to subtraction a! = b not equal to multiplication a < b less than / division % modulo (remainder) a <= b a > b less than or equal to greater than compound assignation a+ = b a = a + b a = b a = a b a = b a = a b a/ = b a = a/b a = b + c a = a (b + c) a >= b greater than or equal to a&&b AND a b OR!a boolean opposite bitwise << >> left and right bit shit a + + a = a + 1 + + a a = a + 1 a a = a 1 a a = a 1 & sizeof(a) bit AND OR others byte size Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (14)

C++ operators (cont.) Arithmetic operators (*) and (/) have precedence over (+) and (-) What C++ code to evaluate: a + b/c +d Unary operators (only act on single operands) like (++), ( ) and signs (+), (-) have precedence over arithmetic operators What does this C++ code: int a, b=5, c; // (a=0, b=5, c=0) b = a++; // b=? (after execution: b=0, a=1) c = ++a; // c=? (after execution: a=2, c=2) Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (15) 1 / / i f else 2 i n t a = 10; 3 i f ( a < 5) { 4 true statement ; 5 } else { 6 false statement ; 7 } 8 9 / / while 10 double dx =1., eps =1.e 6; 11 while ( dx > eps ) { 12 statements ; 13 } 14 15 / / do while 16 do { 17 } while ( dx > eps ) ; 18 19 / / f o r loop 20 for ( i n t i =0; i < 10; i ++) { 21 statements ; 22 } C++ control statements Computational Physics 2018-19 (Phys Dep IST, Lisbon) Fernando Barao (16)

16-1