Presentation is loading. Please wait.

Presentation is loading. Please wait.

First steps Jordi Cortadella Department of Computer Science.

Similar presentations


Presentation on theme: "First steps Jordi Cortadella Department of Computer Science."— Presentation transcript:

1 First steps Jordi Cortadella Department of Computer Science

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

3 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

4 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

5 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

6 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

7 + 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 3 -8 -5 cin cout Introduction to Programming© Dept. CS, UPC7

8 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

9 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

10 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

11 7415 60 141 123 60 215 3 2 35 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

12 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

13 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

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

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

16 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


Download ppt "First steps Jordi Cortadella Department of Computer Science."

Similar presentations


Ads by Google