Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Introduction to Programming CS 115 Introduction to Computing PART I : Computer Basics PART II: Introduction to Computing/Programming.
Advertisements

Chapter 1: An Overview of Computers and Programming Languages
Introduction to Programming (in C++) Subprograms: procedures and functions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science,
LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Wednesday, 9/4/02, Slide #1 1 CS 106 Intro to CS 1 Wednesday, 9/4/02  Today: Introduction, course information, and basic ideas of computers and programming.
1 Lecture 1  Getting ready to program  Hardware Model  Software Model  Programming Languages  The C Language  Software Engineering  Programming.
Computer Science 1620 Programming & Problem Solving.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 1: An Overview of Computers and Programming Languages C++ Programming:
Chapter 1 Program Design
Lecture Notes 8/30/05 Program Design & Intro to Algorithms.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 1: An Overview of Computers and Programming Languages Updated by: Dr\Ali-Alnajjar.
Introduction to Programming (in C++) Data types and visibility Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Computer Science 101 Introduction to Programming.
1 I.Introduction to Algorithm and Programming Algoritma dan Pemrograman – Teknik Informatika UK Petra 2009.
Tools make jobs easier to do -A computer is a tool used by many professions A computer can do many different jobs because they are programmable - Machine.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Conclusions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction CSE 1310 – Introduction to Computers and Programming
High-Level Programming Languages: C++
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
Hello World 2 What does all that mean?.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
UniMAP Sem1-08/09EKT120: Computer Programming1 Week 1 – Lecture 1.
Introduction CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Prime numbers Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Overview of Programming and Problem Solving. Objectives In this chapter you will: Learn about different types of computers Explore the hardware and software.
Python – Part 1 Python Programming Language 1. What is Python? High-level language Interpreted – easy to test and use interactively Object-oriented Open-source.
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
Chapter 1: An Overview of Computers and Programming Languages
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 1- 1 October 20, October 20, 2015October 20, 2015October 20,
Overview of Programming and Problem Solving Textbook Chapter 1 1.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 1: An Overview of Computers and Programming Languages.
Chapter 1 Introduction Chapter 1 Introduction 1 st Semester 2015 CSC 1101 Computer Programming-1.
First steps Jordi Cortadella Department of Computer Science.
COMPUTER PROGRAMMING. Computer programming the objective of the module to gain the necessary skills to develop a computer program using one of the high.
CHAPTER 1 INTRODUCTION 1 st Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
CHAPTER 1 INTRODUCTION 1 st semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
CHAPTER 1 INTRODUCTION 2 nd Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
CHAPTER 1 INTRODUCTION 2 nd Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
CS Computer Science I. BCPL was developed in 1967 as a language for writing operating systems and software compilers In 1970, the creators of the.
Chapter 1 Computers, Compilers, & Unix. Overview u Computer hardware u Unix u Computer Languages u Compilers.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 1.
LESSON 1 Introduction to Programming Language. Computer  Comprised of various devices that are referred to as HARDWARE.  The computer programs that.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
Functions Jordi Cortadella Department of Computer Science.
Introduction to Computer Programming using Fortran 77.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 1: An Overview of Computers and Programming Languages.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Software Engineering Algorithms, Compilers, & Lifecycle.
Chapter 1 Introduction 2nd Semester H
Jordi Cortadella, Ricard Gavaldà, Fernando Orejas
Introduction to Computers and C++ Programming
Engineering Problem Solving With C An Object Based Approach
Computer Programming (BCT 1113)
Chapter 1: An Overview of Computers and Programming Languages
Lecture 1: Introduction to JAVA
Chapter 1: An Overview of Computers and Programming Languages
C++ Programming: From Problem Analysis to Program Design
Introduction CSC 111.
King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1 Done By: Asmal Alosaimi Edited By: Fatimah Alakeel.
King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1 Done By: Asmal Alosaimi Edited By: Fatimah Alakeel.
Presentation transcript:

Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Outline Programming examples Algorithms, programming languages and computer programs Steps in the design of a program 2© Dept. CS, UPCIntroduction to Programming

First program in C++ #include using namespace std; // This program reads two numbers and // writes their sum int main() { int x, y; cin >> x >> y; int s = x + y; cout << s << endl; } Introduction to Programming© Dept. CS, UPC3

Introduction to Programming© Dept. CS, UPC4 cincin coutcout > sum sum > sum sum >

Calculate x y Algorithm: repeated multiplication x  x  x    x Introduction to Programming© Dept. CS, UPC5 y times yxi p=x i

Calculate x y #include using namespace std; // Input: read two integer numbers, x and y, // such that y >= 0 // Output: write x y int main() { 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; // p = x i } cout << p << endl; } Introduction to Programming© Dept. CS, UPC6

Prime factors Decompose a number in prime factors – Example: input 350 output 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, UPC7

Prime factors nddivisiblewrite 3502yes2 1752no 1753no 1754no 1755yes5 355yes5 75no 76 77yes7 1finish Introduction to Programming© Dept. CS, UPC8 The algorithm will never write a non-prime factor. Why ?

Prime factors #include using namespace std; // Input: read a natural number n > 0 // Output: write the decomposition in prime factors int main() { 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; } } Introduction to Programming© Dept. CS, UPC9

ALGORITHMS, PROGRAMMING LANGUAGES AND COMPUTER PROGRAMS Introduction to Programming© Dept. CS, UPC10

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, UPC11

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, UPC12

A computer program A computer program is an algorithm written in a in a programming language that executes a certain task. Examples of tasks a computer program can execute: – Calculate the square root of a number – Find the number of times the word “equation” appears in a math book – Play a music file – Find the shortest path between two cities Introduction to Programming© Dept. CS, UPC13

A computer system CPU Instruction Memory Data Memory Input devices (keyboard, mouse, microphone, etc.) Output devices (display, printer, speakers, etc.) Program (machine language) Program (high-level language) Compiler Loader 14© Dept. CS, UPCIntroduction to Programming This course: Design of programs Language: C++

High-level language Computers understand very low-level instructions (machine language). 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, … Introduction to Programming© Dept. CS, UPC15

Assembly and machine language Introduction to Programming© Dept. CS, UPC16 (From

STEPS IN THE DESIGN OF A PROGRAM Introduction to Programming© Dept. CS, UPC17

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. 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, UPC18

Example Design a program that – 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, UPC19

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, UPC20

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. 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, UPC21

A bad specification Precondition: N ≥ 0 Postcondition: 3600  h + 60  m + s = N, Introduction to Programming© Dept. LSI, UPC22Introduction to Programming© Dept. CS, UPC22

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   = 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. CS, UPC23

A good specification Precondition: N ≥ 0 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, UPC24

Algorithms 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. Introduction to Programming© Dept. CS, UPC25

Program in C++ #include using namespace std; // This program reads a natural number that represents an amount // of time in seconds and writes the decomposition in hours, // minutes and seconds int main() { 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, UPC26

Execution > decompose_time hours, 3 minutes and 35 seconds > decompose_time 60 0 hours, 1 minutes and 0 seconds Introduction to Programming© Dept. CS, UPC27