Docsity
Docsity

Prepara tus exámenes
Prepara tus exámenes

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity


Consigue puntos base para descargar
Consigue puntos base para descargar

Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium


Orientación Universidad
Orientación Universidad


intro pro1, Apuntes de Algoritmos y Programación

Asignatura: Programació I, Profesor: , Carrera: Enginyeria Informàtica, Universidad: UPC

Tipo: Apuntes

2017/2018

Subido el 08/01/2018

usuario desconocido
usuario desconocido 🇪🇸

1 / 27

Toggle sidebar

Esta página no es visible en la vista previa

¡No te pierdas las partes importantes!

bg1
Introduction to Programming
(in C++)
Introduction
Jordi Cortadella, Ricard Gavaldà, Fernando Orejas
Dept. of Computer Science, UPC
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Vista previa parcial del texto

¡Descarga intro pro1 y más Apuntes en PDF de Algoritmos y Programación solo en Docsity!

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

cin

cout

sum 8 13 21 sum

  • 15 9
  • 6

Calculate x

y

• Algorithm: repeated multiplication

x  x  x    x

y times

y x i p=x

i

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

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?

ALGORITHMS, PROGRAMMING LANGUAGES AND COMPUTER PROGRAMS

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.

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

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 This course:

  • Design of programs
  • Language: C++

Assembly and machine language

(From http://en.wikipedia.org/wiki/Assembly_language)

STEPS IN THE DESIGN OF A

PROGRAM

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=

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