First steps Jordi Cortadella Department of Computer Science.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Chapter 2: Basic Elements of C++
Introduction to Programming (in C++) Subprograms: procedures and functions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science,
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
CS150 Introduction to Computer Science 1
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Introduction to Programming (in C++) Introduction Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to C++ Programming
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Data types and their representation Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
Sequences Jordi Cortadella Department of Computer Science.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
Vectors Jordi Cortadella Department of Computer Science.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Parameter passing Jordi Cortadella Department of Computer Science.
Functions Jordi Cortadella Department of Computer Science.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
CS 31 Discussion, Week 2 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)
General Computer Science for Engineers CISC 106 Lecture 27 Dr. John Cavazos Computer and Information Sciences 04/27/2009.
CS 240 Computer Programming 1
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Jordi Cortadella, Ricard Gavaldà, Fernando Orejas
What Actions Do We Have Part 1
for Repetition Structures
For loops, nested loops and scopes
CS150 Introduction to Computer Science 1
Summary Two basic concepts: variables and assignments Basic types:
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Life is Full of Alternatives
Jordi Cortadella Department of Computer Science
Arithmetic Operations
Reading from and Writing to Files
CS 101 First Exam Review.
Reading from and Writing to Files Part 2
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
Presentation transcript:

First steps Jordi Cortadella Department of Computer Science

Interacting with computers computer programprogram input devices output devices Introduction to Programming© Dept. CS, UPC2

First program in C++ #include using namespace std; // This program prints // “Hello, world!” int main() { cout << "Hello, world!" << endl; } Required to enable operations for reading and writing data. Main program Output channel (display) Comments String to be displayed End of line Introduction to Programming© Dept. CS, UPC3

Reading and writing data #include #include using namespace std; /* This program reads your name and prints “Hello, !” */ int main() { cout > name; cout << "Hello, " << name << "!" << endl; } Introduction to Programming© Dept. CS, UPC4

Executing the program hello > What’s your name? Jordi Hello, Jordi! hello > What’s your name? Anna Hello, Anna! > Introduction to Programming© Dept. CS, UPC5

7 A variable15 “Jordi” “Anna” -13 student string number int Variables have names and types. Every variable can only store one value of its type. Variables represent memory locations. Introduction to Programming© Dept. CS, UPC6

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

Adding two numbers #include using namespace std; // This program reads two numbers // and prints their sum int main() { int x, y; cin >> x >> y; cout << x + y << endl; } + xy 3 -8 cin cout Introduction to Programming© Dept. CS, UPC8

Arithmetic expressions #include using namespace std; // This program reads three numbers // (x, y and z) and prints x(y+2z) int main() { int x, y, z; cin >> x >> y >> z; cout << x(y + 2z) << endl; } Introduction to Programming© Dept. CS, UPC9

Decomposing time 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=7415, Calculate h=2, m=3, s=35 Introduction to Programming© Dept. CS, UPC10

Decomposing time secondsminutes hours s = n%60; // 35 m = n/60; // 123 h = m/60; // 2 m = m%60; // 3 h = n/3600; m = (n/60)%60; s = n%60; Introduction to Programming© Dept. CS, UPC11

Arithmetic with integers int a = 17; int b = 4; a + b21 b – a-13 abab 68 a/b4 a%b1 Be careful with division by zero! Introduction to Programming© Dept. CS, UPC12

Decomposing time #include using namespace std; // This program reads a natural number that represents // an amount of time in seconds and prints the // decomposition in hours, minutes and seconds. int main() { int n; cin >> n; int s = n%60; int m = n/60; int h = m/60; m = m%60; cout << h << " hours, " << m << " minutes and " << s << " seconds" << endl; } Introduction to Programming© Dept. CS, UPC13

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

Swapping the value of two variables x = y; y = x; xy xy 5 5 x 9 9 y ? ? z z = x; x = y; y = z; x 9 9 y Introduction to Programming© Dept. CS, UPC15

Summary Data can be read from the input channel (cin) and written into the output channel (cout). Variables are locations that store values of a certain type (int, string). They must be declared before being used: int x, y; string name; Variables have unknown values if they are not initialized. Values can be assigned to variables by evaluating expressions: s = x + y; Introduction to Programming© Dept. CS, UPC16