int x, y

advertisement
First steps
Jordi Cortadella
Department of Computer Science
Interacting with computers
program
output devices
input devices
Introduction to Programming
computer
© Dept. CS, UPC
2
First program in C++
Required to enable operations
for reading and writing data.
#include <iostream>
using namespace std;
Comments
// This program prints
// “Hello, world!”
Main program
int main() {
cout << "Hello, world!" << endl;
}
End of line
String to be displayed
Output channel (display)
Introduction to Programming
© Dept. CS, UPC
3
Reading and writing data
#include <iostream>
#include <string>
using namespace std;
/* This program reads your name and prints
“Hello, <your name>!” */
int main() {
cout << "What’s your name? ";
string name;
cin >> name;
cout << "Hello, " << name << "!" << endl;
}
Introduction to Programming
© Dept. CS, UPC
4
Executing the program
> hello
What’s your name? Jordi
Hello, Jordi!
> hello
What’s your name? Anna
Hello, Anna!
>
Introduction to Programming
© Dept. CS, UPC
5
A variable
• Variables have names and types.
• Every variable can only store one value of its type.
• Variables represent memory locations.
“Anna”
“Jordi”
Introduction to Programming
7
-13
15
© Dept. CS, UPC
6
Adding two numbers
#include <iostream>
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;
}
x
cin
3
y -8
+
s -5
cout
Introduction to Programming
© Dept. CS, UPC
7
Adding two numbers
#include <iostream>
using namespace std;
// This program reads two numbers
// and prints their sum
int main() {
int x, y;
cin >> x >> y;
cout << x + y << endl;
}
cin
x
3
y -8
+
cout
Introduction to Programming
© Dept. CS, UPC
8
Arithmetic expressions
#include <iostream>
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, UPC
9
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, UPC
10
Decomposing time
7415 60
141 123 60
215
3 2
hours
35
s
m
h
m
=
=
=
=
n%60;
n/60;
m/60;
m%60;
//
//
//
//
35
123
2
3
minutes
seconds
Introduction to Programming
h = n/3600;
m = (n/60)%60;
s = n%60;
© Dept. CS, UPC
11
Arithmetic with integers
int a = 17;
int b = 4;
a + b
b – a
ab
a/b
a%b
21
-13
68
4
1
Be careful with division by zero!
Introduction to Programming
© Dept. CS, UPC
12
Decomposing time
#include <iostream>
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, UPC
13
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, UPC
14
Swapping the value of two variables
x 5
y 9
x 9
y 5
z = x;
x = y;
y = z;
x = y;
y = x;
x 9
5
y 9
Introduction to Programming
x 5
9
© Dept. CS, UPC
y 5
9
z 5
?
15
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, UPC
16
Download