Outline. Introduction to Programming (in C++) Introduction. First program in C++ Programming examples

Similar documents
Subprograms. Introduction to Programming (in C++) Subprograms: procedures and functions. Subprograms. Subprograms. Functions are defined as follows:

Outline. Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Maximum of a sequence. Maximum of a sequence

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

A little bit of history. Jordi Cortadella Department of Computer Science

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

Chapter 1: Why Program? Computers and Programming. Why Program?

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

COMP 201: Principles of Programming

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

Add Subtract Multiply Divide

CSCI 3136 Principles of Programming Languages

Topic I. Introduction and motivation References: Chapter 1 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Programming II (CS300)

Announcements Tues., Aug and 2. to call them ( ) ! For next time, read Learning Computing with Robots, chs.

Problem Solving and Program Design - Chapter 1. Cory L. Strope

Principles in Programming: Orientation & Lecture 1. SWE2004: Principles in Programming Spring 2014 Euiseong Seo

Mechanization. A little bit of history. Mechanization. The old dream of mechanical computing. Jordi Cortadella Department of Computer Science

PLAGIARISM. Administrivia. Course home page: Introduction to Programming Languages and Compilers

Introduction to Programming Languages and Compilers. CS164 11:00-12:00 MWF 306 Soda

CSI32 Object-Oriented Programming

BITG 1113: Introduction To Computers And Programming Language LECTURE 1 LECTURE 1 1

Low-Level Languages. Computer Programs and Programming Languages

Concepts in Programming Languages

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

Programming Languages, Summary CSC419; Odelia Schwartz

MIDTERM EXAMINATION - CS130 - Spring 2003

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

Programming 1. Lecture 1 COP 3014 Fall August 28, 2017

Chapter 1 INTRODUCTION

Chapter 1: An Overview of Computers and Programming Languages. Objectives. Objectives (cont d.) Introduction

1. Introduction to Programming

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Scientific Computing

Programmiersprachen (Programming Languages)

Lecture 01 & 02 Computer Programming

Programming 1 - Honors

Computer Programming-1 CSC 111. Chapter 1 : Introduction

CS2303 C14 Systems Programming Concepts. Bob Kinicki

CS187 - Science Gateway Seminar for CS and Math

Introduction. A. Bellaachia Page: 1

Principles in Programming: Orientation & Lecture 1. SWE2004: Principles in Programming Spring 2015 Euiseong Seo

CS102 Unit 2. Sets and Mathematical Formalism Programming Languages and Simple Program Execution

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

CSI31 Introduction to Computer Programming I. Dr. Sharon Persinger Fall

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

Principles of Programming Languages. Lecture Outline

Lecture 1: Course Introduction

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Chapter 1: Why Program? Main Hardware Component Categories 8/23/2014. Main Hardware Component Categories: Why Program?

Programming Language Concepts 1982, 1987, Outline. Period

The Programming Process Summer 2010 Margaret Reid-Miller

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

CMSC 132: Object-Oriented Programming II

Command-line interface DOS Graphical User interface (GUI) -- Windows

C++ Programming Language Lecture 1 Introduction

Computer Basics 1/6/16. Computer Organization. Computer systems consist of hardware and software.

Introduction to Engineering Using Robotics Experiments. Dr. Yinong Chen

Programming. leads to programming languages. leads to operating systems

Chapter 1: Introduction to Computers and Programming

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

The births of the generations are as follow. First generation, 1945 machine language Second generation, mid 1950s assembly language.

Lecture 1: Course Introduction

Discovering Computers Chapter 13 Programming Languages and Program Development

CSCE150A. Administrivia. Overview. Hardware. Software. Example. Program. Pseudocode. Flowchart. Control Structures. Hello World Program CSCE150A

Computer Science & Engineering 150A Problem Solving Using Computers

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

Software II: Principles of Programming Languages. Why Expressions?

CS558 Programming Languages

Compiler Construction D7011E

Programming Languages

Programming Languages

An Introduction to Python (TEJ3M & TEJ4M)

Administration Computers Software Algorithms Programming Languages

Simply-Typed Lambda Calculus

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

CS558 Programming Languages

Lecture 09. Ada to Software Engineering. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

Chapter 1 Introduction to Computers and C++ Programming

What is a programming language?

Introduction to Scientific Computing Languages

Algorithm: 1. Introduction

Why study Programming Language Concepts? Chapter One. Language Evaluation Criteria. Programming Domains. Readability Writability Reliability Cost

C++ for Everyone, 2e, Cay Horstmann, Copyright 2012 John Wiley and Sons, Inc. All rights reserved. Using a Debugger WE5.

CSE 230 Computer Science II (Data Structure) Introduction

Chapter 1 Introduction to Computers and C++ Programming

Introduction to Scientific Computing Languages

From High Level to Machine Code. Compilation Overview. Computer Programs

Harvard-MIT Division of Health Sciences and Technology HST.952: Computing for Biomedical Scientists HST 952. Computing for Biomedical Scientists

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

G Programming Languages - Fall 2012

Programming & Computers

Seminar in Programming Languages

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Spring 2018 PhD Qualifying Exam in Languages

do { statements } while (condition);

Java and Software Design

Preview from Notesale.co.uk Page 3 of 79

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

EKT 120/4 Computer Programming KOLEJ UNIVERSITI KEJURUTERAAN UTARA MALAYSIA

Welcome to Python 3. Some history

Transcription:

Outline Introduction to Programming (in C++) Introduction Programming examples Algorithms, programming languages and computer programs Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC Steps in the design of a program First program in C++ Introduction to Programming Dept. CS, UPC 2 // This program reads two numbers and // writes their sum int x, y; cin >> x >> y; int s = x + y; cout << s << endl; > sum 8 13 21 > sum -15 9-6 > cin cout Introduction to Programming Dept. CS, UPC 3 Introduction to Programming Dept. CS, UPC 4

Calculate x y Algorithm: repeated multiplication x x x x y times y x i p=x i 4 3 0 1 4 3 1 3 4 3 2 9 4 3 3 27 4 3 4 81 Calculate x y // Input: read two integer numbers, x and y, // such that y >= 0 // Output: write x y int x, y; cin >> x >> y; int i = 0; int p = 1; while (i < y) { // Repeat several times (y) i = i + 1; p = p x; cout << p << endl; // p = x i Introduction to Programming Dept. CS, UPC 5 Prime factors Decompose a number in prime factors Example: input 350 output 2 5 5 7 Intuitive algorithm: Try all potential divisors d, starting from 2 If divisible by d, divide and try again the same divisor If not divisible, go to the next divisor Keep dividing until the number becomes 1 Introduction to Programming Dept. CS, UPC 6 Prime factors n d divisible write 350 2 yes 2 175 2 no 175 3 no 175 4 no 175 5 yes 5 35 5 yes 5 7 5 no 7 6 no 7 7 yes 7 1 finish The algorithm will never write a non-prime factor. Why? Introduction to Programming Dept. CS, UPC 7 Introduction to Programming Dept. CS, UPC 8

Prime factors // Input: read a natural number n > 0 // Output: write the decomposition in prime factors int n; cin >> n; int d = 2; // Variable to store divisors // Divide n by divisors from 2 in ascending order while (n!= 1) { if (n%d == 0) { // Check if divisible cout << d << endl; n = n/d; else d = d + 1; ALGORITHMS, PROGRAMMING LANGUAGES AND COMPUTER PROGRAMS Introduction to Programming Dept. CS, UPC 9 An algorithm An algorithm is a method for solving a problem. It is usually described as a sequence of steps. Example: How can we find out whether a number is prime? Read the number (N). Divide N by all numbers between 2 and N-1 and calculate the remainder of each division. If all remainders are different from zero, the number is prime. Otherwise, the number is not prime. Introduction to Programming Dept. CS, UPC 10 A programming language A programming language is a language used to describe instructions for a computer. What s in a programming language? Data (numbers, strings, structures, ) Instructions (arithmetic, sequence, repetition, ) A programming language has very strict syntax and semantics, as it must be understood by a computer! Introduction to Programming Dept. CS, UPC 11 Introduction to Programming Dept. CS, UPC 12

A computer program A computer system A computer program is an algorithm written in a in a programming language that executes a certain task. Program (high-level language) This course: Design of programs Language: C++ Examples of tasks a computer program can execute: Compiler Calculate the square root of a number Find the number of times the word equation appears in a math book Program (machine language) Loader Play a music file Find the shortest path between two cities Input devices (keyboard, mouse, microphone, etc.) Instruction Memory Data Memory Output devices (display, printer, speakers, etc.) CPU Introduction to Programming Dept. CS, UPC 13 High-level language Computers understand very low-level instructions (machine language). Introduction to Programming Dept. CS, UPC Assembly and machine language 14 Software is usually constructed using high-level languages. Higher productivity Better readability Simpler debugging But some time and memory efficiency may be lost A compiler can translate a high-level language into machine language automatically. There is a huge number of programming languages: C, C++, Java, Pascal, PHP, Modula, Lisp, Python, Excel, Fortran, Cobol, APL, Basic, Tcl, Ruby, Smalltalk, Haskell, Perl, SQL, Prolog, (From http://en.wikipedia.org/wiki/assembly_language) Introduction to Programming Dept. CS, UPC 15 Introduction to Programming Dept. CS, UPC 16

Steps in the design of a program 1. Specification The task executed by the program must be described rigorously (without ambiguities). 2. Design of the algorithm The method for executing the task must be selected and designed in such a way that the program is correct according to the specification. 3. Coding in a programming language The algorithm must be written in a programming language that can be executed by the computer. STEPS IN THE DESIGN OF A PROGRAM 4. Execution The program must be executed with a set of examples that reasonably cover all the possible cases of data input. If the program does not work properly, the algorithm will have to be redesigned. Introduction to Programming Dept. CS, UPC 17 Design a program that Example given a natural number representing a certain amount of time in seconds (N), calculates three numbers (h, m, s) that represent the same time decomposed into hours (h), minutes (m) and seconds (s) Example Given N=3815, Calculate h=1, m=3, s=35 Introduction to Programming Dept. CS, UPC 18 Specification Precondition: Specification of the data before the program is executed Postcondition: Specification of the data after the program is executed Example Precondition: N 0 Postcondition: 3600 h + 60 m + s = N Introduction to Programming Dept. CS, UPC 19 Introduction to Programming Dept. CS, UPC 20

Specification A bad specification Alternatively, specifications can describe the input and output data of a program. Input: the program reads a natural number representing a number of seconds. Precondition: N 0 Postcondition: 3600 h + 60 m + s = N, Output: the program writes the same time decomposed into hours, minutes and seconds. Specifications can be described in many ways, e.g. using plain English or formal logic propositions. Even when written in English, specifications must be rigorous and unambiguous. Introduction to Programming Dept. CS, UPC 21 A bad specification Does the specification really describe what the program is supposed to calculate? Example Assume N = 3815 The solution h=1, m=3, s=35 meets the specification (1 3600 + 3 60 + 35 = 3815) But the solutions h=0, m=30, s=2015 and h=0, m=0 and s=3815 also meet the specification. What s wrong? Introduction to Programming Dept. LSI, CS, UPC 22 Precondition: N 0 A good specification Postcondition: 3600 h + 60 m + s = N, 0 <= s < 60, 0 <= m < 60 The solution h=1, m=3, s=35 fulfils the specification. The solutions h=0, m=30, s=2015 and h=0, m=0, s=3815 do not. Introduction to Programming Dept. CS, UPC 23 Introduction to Programming Dept. CS, UPC 24

Algorithms Program in C++ An algorithm: h = N / 3600 (integer division) m = (N mod 3600) / 60 (mod: remainder) s = N mod 60 Another algorithm: s = N mod 60 x = N / 60 m = x mod 60 h = x / 60 Many algorithms may exist to solve the same problem. Use the most efficient one whenever possible. But, which one is the most efficient? There is no easy answer. // This program reads a natural number that represents an amount // of time in seconds and writes the decomposition in hours, // minutes and seconds int N; cin >> N; int h = N / 3600; int m = (N % 3600) / 60; int s = N % 60; cout << h << " hours, " << m << " minutes and " << s << " seconds" << endl; Introduction to Programming Dept. CS, UPC 25 Execution Introduction to Programming Dept. CS, UPC 26 > decompose_time 3815 1 hours, 3 minutes and 35 seconds > decompose_time 60 0 hours, 1 minutes and 0 seconds Introduction to Programming Dept. CS, UPC 27