Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… ·...

46
Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions Introduction to Programming lecture 2 Agata Pólrola Faculty of Mathematics and Computer Science, Lódź University 2016/2017 Agata Pólrola Faculty of Mathematics and Computer Science, Lódź University Introduction to Programming

Transcript of Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… ·...

Page 1: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Introduction to Programminglecture 2

Agata PółrolaFaculty of Mathematics and Computer Science, Łódź University

2016/2017

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 2: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Numbers

Using numbers

C++ “knows” various kinds of numbers (various numeric types)and “knows” how to perform arithmetic operations on them

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 3: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Basic operations on integer numbers

#include <iostream >using namespace std;int main(){

cout << "20 + 2 = " << 20+2 << endl;cout << "20 - 2 = " << 20-2 << endl;cout << "20 * 2 = " << 20*2 << endl;cout << "20 / 2 = " << 20/2 << endl;cout << "20 / 3 = " << 20/3 << endl;cout << "20 % 2 = " << 20%2 << endl;cout << "20 % 3 = " << 20%3 << endl;

return 0;}

operators used: + - addition, − - subtraction, ∗ - multiplication,/ - division, % - modulo (the remainder of a division of two values)

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 4: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Basic operations on real numbers

Real numbers - with a decimal point

#include <iostream >using namespace std;int main(){

cout << "20 + 2.0 = " << 20+2.0 << endl;cout << "20 - 2.0 = " << 20 -2.0 << endl;cout << "20 * 2.0 = " << 20*2.0 << endl;cout << "20 / 2.0 = " << 20/2.0 << endl;cout << "20 / 2.1 = " << 20/2.1 << endl;

return 0;}

operators used: + - addition, − - subtraction, ∗ - multiplication,/ - division (no modulo operator!)

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 5: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Division

the result for two integers is an integer value (20/2 gives 10,20/3 gives 6)

if at least one value is a real, the result is a real number

... thus, if we want to divide “properly” two integers, weshould add a decimal point to at least one of them (20.0/3 or20/3.0 or 20.0/3.0)

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 6: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Division

the result for two integers is an integer value (20/2 gives 10,20/3 gives 6)

if at least one value is a real, the result is a real number

... thus, if we want to divide “properly” two integers, weshould add a decimal point to at least one of them (20.0/3 or20/3.0 or 20.0/3.0)

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 7: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Literals

Literals are a kind of constants (expressions of a fixed value). Theyare used to express particular values (numbers, characters, textstrings etc) in a source code of a program.Numeric literals are: integer literals and floating-point literals (thelatter representing real numbers)

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 8: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Integer literals

decimal numbers: 12, -12, 1234

octal numbers (base 8): 012, -012 (digits are preceded by thezero character)

hexadecimal (base 16): 0xab, 0x12, -0X12, -0XAB (digits arepreceded by 0x or 0X, digits are 0..9, a..f)

binary (base 2) - from the standard C++14: 0B001, -0b1111(digits preceded by 0b or 0B)

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 9: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Integer literals - cont’d

The default type of integer literals of no suffix is int. Suffixesenabling to assign an integer literal to a particular type (to beexplained):u or U – the type unsigned intl or L – the type long intll or LL – the type long long int

(the suffixes can be combined - e.g., ul denotes unsignedlong int. Examples: 75l, 75ul, 75LL).

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 10: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Floating point literals

Express real values, with decimal and/or exponents.

“standard” notation: 12.3, -4.5

scientific (exponential) notation: -3.3e5, 4.6E-2

the default type of floating-point literals is double. Suffixeswhich enable to assign a floating-point literal to another type:f or F – the type floatl or L – the type long double

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 11: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Floating point literals

Express real values, with decimal and/or exponents.

“standard” notation: 12.3, -4.5

scientific (exponential) notation: -3.3e5, 4.6E-2the default type of floating-point literals is double. Suffixeswhich enable to assign a floating-point literal to another type:f or F – the type floatl or L – the type long double

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 12: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Numerical types

The language contains several predefined numerical data types.Fundamental numerical types are:

int, short int, long int, long long int for representinginteger numbers; each of them can be either signed (default)or unsigned (e.g., unsigned short int)(it is allowed to use “short names”, i.e.,: short, long, long long )

float, double and long double for representing realnumbers

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 13: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Type ranges

All the numerical values are represented in the computer memoryas sequences of zeros and ones, using a certain, fixed for a giventype, number of bits.Due to this each type has a range of values - there is a smallestand a greatest value of this type

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 14: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Printing the range of the type int

#include <iostream >#include <limits >using namespace std;

int main(){

cout << "range of int: from " << std::numeric_limits <int >:: min() << " to " << std::numeric_limits <int >:: max() << endl;

cout << "size of int in bytes: " << sizeof(int)<< endl;

return 0;}

Ranges of other numeric types can be printed in a similar way

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 15: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Variables

In order to store in the computer memory a value given by the user(using the keyboard) or computed by the program, we need todefine a variable of an appropriate type

to do this we typevariable type variable name ; (e.g., int a; )the names of variables should be (not reserved) identifiers

the result: when the program runs, in the computer memoryan appropriate area (corresponding to the size of the type) isreserved. The area is referred to by the variable name.

one can define several variables of the same type:int a,b,c;

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 16: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Variables

In order to store in the computer memory a value given by the user(using the keyboard) or computed by the program, we need todefine a variable of an appropriate type

to do this we typevariable type variable name ; (e.g., int a; )the names of variables should be (not reserved) identifiers

the result: when the program runs, in the computer memoryan appropriate area (corresponding to the size of the type) isreserved. The area is referred to by the variable name.

one can define several variables of the same type:int a,b,c;

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 17: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Definition vs. declaration

a declaration informs the compiler that a given name isalready known, but does not cause memory allocation(e.g., extern int a; - an information that a integervariable a is defined in another file (being a part of theprogram))

a definition specifies precisely what a given identifier is .Defining a variable results in allocating memory for it.

Each definition is a declaration, but the reverse is not true.

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 18: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

The value of a variable can be changed while the program runs

The assignment operator (=) allows us to give a value to avariable

the value assigned should belong to the range of the variabletype

variables not assigned any value in the program (uninitialised)have a random value

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 19: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

The value of a variable can be changed while the program runs

The assignment operator (=) allows us to give a value to avariablethe value assigned should belong to the range of the variabletype

variables not assigned any value in the program (uninitialised)have a random value

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 20: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

An example of using variables

#include <iostream >using namespace std;int main(){

int a;int b,c;

a=3;b=6;c = a+b;

cout << "a = " << a << endl;cout << "b = " << b << endl;cout << "a + b = " << c << endl;

return 0;}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 21: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Initialization of variables

Each variable can be assigned a value when it is defined.(initialization of the variable). The value should belong to the typerangeThe form: variable type variable name = expression;

Example

int i = 0;

float s= 2.5234 + 3.234;

int k, l=100, m;only one value is initialised with 100

obviously, values of initialised variables can be changed when the program runs

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 22: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Initialization of variables

Each variable can be assigned a value when it is defined.(initialization of the variable). The value should belong to the typerangeThe form: variable type variable name = expression;

Example

int i = 0;

float s= 2.5234 + 3.234;

int k, l=100, m;only one value is initialised with 100

obviously, values of initialised variables can be changed when the program runs

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 23: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Example (Initial values of variables, and C++ is not Excel ;-) )

#include <iostream >using namespace std;int main(){int a = 3;int x, b=10;int c = a*b;

cout << "uninitialised x = " << x << endl;a = 6;cout << "a = " << a << " b = " << b << " c = " << c

<< endl; /* the value of a changed , thevalue of c not changed (still 30) */

return 0;}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 24: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Constants

Besides variables, a program can use constants. Constants areinitialised when they are defined, and their values cannot bechanged during the program execution

constant definition:const type name constant name = expression;

Examples

const int tax percent = 18;

const float acceleration = 9.80665;

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 25: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Constants

Besides variables, a program can use constants. Constants areinitialised when they are defined, and their values cannot bechanged during the program execution

constant definition:const type name constant name = expression;

Examples

const int tax percent = 18;

const float acceleration = 9.80665;

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 26: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

A program using constants

#include <iostream >using namespace std;int main(){

float netto_price = 123.40;const float vat = 0.22;float brutto_price;

brutto_price = netto_price + netto_price * vat;cout << "netto price is " << netto_price << ", but

due to VAT you shall pay " << brutto_price <<endl;

return 0;}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 27: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Arithmetic expressions

Expressions are built from literals, variables, constants andarithmetic operators, using parentheses ( ) if neededExpressions are used to compute values which are then printed orassigned to variables or constantsa single variable or a literal is also an expression

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 28: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Operators used in arithmetic expressions

The basic operators are + - * / and % (modulo, for integervalues)the priority of operators corresponds to this in mathematics:

the contents of parentheses is evaluated first; if theparentheses are nested, the expression in the innermost pair isevaluated first,multiplication, division and modulo are evaluated next; if thereare many they are evaluated left to right,addition and subtraction are evaluated last; if there are severalthey are evaluated left to right .

the type of the result of an arthmetic operation is the same asthat of a “most precise” value used as its argument; the typeof the final result of an expression can be deduced applyingthis rule “step by step”

therefore, dividing two integers gives an integer result, even if the expression

is e.g. 12/5 + 7.2

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 29: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Operators used in arithmetic expressions

The basic operators are + - * / and % (modulo, for integervalues)the priority of operators corresponds to this in mathematics:

the contents of parentheses is evaluated first; if theparentheses are nested, the expression in the innermost pair isevaluated first,multiplication, division and modulo are evaluated next; if thereare many they are evaluated left to right,addition and subtraction are evaluated last; if there are severalthey are evaluated left to right .

the type of the result of an arthmetic operation is the same asthat of a “most precise” value used as its argument; the typeof the final result of an expression can be deduced applyingthis rule “step by step”therefore, dividing two integers gives an integer result, even if the expression

is e.g. 12/5 + 7.2Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 30: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

A “compressed form” of assignments

a = a + x can be abbreviated to a += xsimilarly:a = a - x can be abbreviated to a -= xa = a * x can be abbreviated to a *= xa = a / x can be abbreviated to a /= x

where a is a variable, and x is an arbitrary expression(examples: a +=10, a-=3*x)

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 31: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Other operators: incrementation and decrementation

Adding and subtracting 1 is an often operation; due to this thereare special operators for it:

++ - incrementation

-- - decrementation

Usage (a is variable):

a++;a--;(post-incrementation)

++a;--a;(pre-incrementation)

The details will be explained later

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 32: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Example (An example of incrementations)

#include <iostream >using namespace std;int main() {

int i, x;

i=2; x=4;i = x++;cout << " i = " << i << " x = " << x << endl;// i=4, x=5 ("x first used in the assignment ,then incremented ")

i=2; x=4;i = ++x;cout << " i = " << i << " x = " << x << endl;// i=5, x=5 ("x first incremented , then usedin the assignment ")

return 0; }Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 33: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Type conversion (type casting)

Type conversion (type casting) changes a value of one type toa corresponding value of another typethere exist two syntaxes:

functional: type name (object to be converted) -e.g., y = int(x) ;C-like: (type name) object to be converted -e.g., y = (int) x ;

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 34: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Input and output for numerical variables

Input stream std::cin

In order to assign a variable with a value given by the user, wecan use the input stream std::cin

the stream can be seen as associated with the standard inputdevice (usually a keyboard)

the instruction std::cin >> a variable; “redirects” thedata from the keyboard to the variable given after theoperator >>

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 35: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

#include <iostream >using namespace std;int main(){

int eggs1 , eggs2 , totaleggs , people;

cout << "Give the number of eggs of the 1stperson : ";

cin >> eggs1;cout << "Give the number of eggs of the 2ndperson : ";

cin >> eggs2;totaleggs = eggs1 + eggs2;cout << "They have together " << totaleggs <<" eggs " << endl;

cout << "How many people do you want to feed?"; cin >> people;

cout << "Eggs for person: " << totaleggs /float(people) << endl; // conversion tofloat

return 0;}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 36: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Formatting output: manipulators

Manipulators are special values which can be “inserted” intothe output stream, changing the way the data are displayed.

most manipulators work until are “removed” (replaced byanother setting)

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 37: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Manipulators - cont’d

Parameterless manipulators:

hex, dec, oct - work only for integers, specifying the systemin which the numbers are displayed / read (dec is the default)

showpoint, noshowpoint - toggle showing trailing zeroesand decimal dot if not significant (default: showpoint)

showpos, noshowpos - toggle printing the sign of positivevalued (default: noshowpos)

showbase, noshowbase - toggle showing the symbol of thebase when a non-decimal system value is printed (default:noshowbase)

fixed, scientific - toggle between the decimal and theexponential form of presenting floating-point values

endl- new line

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 38: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

#include <iostream >using namespace std;int main() {

cout << "12 in octal: " << oct << 12 << endl;cout << "another form " << showbase << oct << 12<< endl;

cout << noshowbase; // returnig to the defaultsetting

cout << "12 in hex: " << hex << 12 << endl;cout << 123 << endl; // hex is stil activecout << dec; // returning to defultcout << 123 << endl; // now in decimalcout << showpoint << 12.00 << noshowpoint << " vs. " << 12.00 << endl;

cout << fixed << 12e02 << " vs. " << scientific <<12e02 << endl;

cout << 4.4 << endl;// printed scientificcout << showpos << 4 << " vs. " << noshowpos << 4<< endl;

return 0; }

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 39: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Manipulators - cont’d

Manipulators with parameters, require including iomanip library

setw(n), where n is an integer - sets to n the width ofprinting numbers. Applies only to the nearest input/outputoperation

setfill(n), where n is a character - sets the character fillingempty spaces printed (default: space)

setprecision(n), where n is an integer - sets the precisionof displaying floating-point values (either the total number ofdigits, or the number of digits after the decimal dot whencombined with fixed)

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 40: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

#include <iostream >#include <iomanip >using namespace std;

int main(){

cout << std:: setprecision (5) << 100.1234567899 <<endl << "vs. " << endl;

cout << fixed << std:: setprecision (5) <<100.1234567899 << endl;

cout << 12 << endl;cout << setw (10) << 12 << endl;cout << setw (10) << setfill(’.’) << 12 << endl;

return 0;}

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 41: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Mathematical functions

The cmath library

Including the library cmath enables using various mathematicalfunctions. Some of them are:

sin(x), cos(x), tan(x), where x is a real value - computesine, cosine and tangent of an angle x (given in radians); theresult type is the same as that of x

exp(x), where x is a real value - returns ex (of the type asthat of x)

log(x), log10(x), where x is a real value - computerespectively: natural logarithm and commmon logarithm of x,the result type is as that of x

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 42: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

The cmath llibrary - cont’d

ceil(x) - returns “the ceiling” - the smallest integer notsmaller than x

floor(x) - returns “the floor” - the greatest integer notgreater than x

trunc(x) - returns an integer which is the rounding of xtowards zero

round(x) - returns an integer which is nearest to x, withhalfway cases rounded away from zero

abs(x) - returns |x | (the absolute value)

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 43: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

The cmath llibrary - cont’d

pow(a,b) - computes ab; where a - a real, b - a real (fora < 0 must be an integer value); the type of the result is thesame as that of a

sqrt(x), cbrt(x) - compute respectivelythe square root andthe cubic root of x (x is a real, the result type is like that ofx) [cbrtexists from the standard C++11]

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 44: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Example (Using mathematical functions)

#include <iostream >#include <cmath >using namespace std;int main(){

float a = 5.1, b = 2;

cout << a << "^" << b << " to " << pow(a,b) <<endl;

cout << "square root of " << a << " is " << sqrt(a) << endl;

cout << "cubic root of " << a << " is " << cbrt(a)<< endl;

cout << "absolute value of " << a << " is " <<abs(a) << endl;

cout << "natural logarithm of " << a << " is " <<log(a) << endl;

return 0; }

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 45: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

The cmath llibrary - cont’d

One can get an access to mathematical constants. Some of themare:

M PI - the constant π

M E - the constant e

M PI 2, M PI 4, M 1 PI, M 2 PI, M 2 SQRTPI - π2 , π

4 , 1π , 2

π , 2√π

M LOG2E M LOG10E, M LN2, M LN10 - log2 e, log10 e, ln 2, ln 10

M SQRT2, M SQRT1 2 -√

2, 1√2

the above constants are not part of the standard of C++ – usingthem can require defining an appropriate symbolic constant( USE MATH DEFINES) before including cmath, and possiblyundefining STRICT ANSI (see the next slide), or running thecompiler with an appropriate switch

Agata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming

Page 46: Introduction to Programming - math.uni.lodz.plpolrola/strony/1617z-wdprog/wdprog1617-w0… · Introduction to Programming. NumbersNumerical typesVariables and constantsArithmetic

Numbers Numerical types Variables and constants Arithmetic expressions I/O for numerical variables Mathematical functions

Example (Using mathematical functions and constants)

#define _USE_MATH_DEFINES // not always necessary#undef __STRICT_ANSI__ //not always necessary#include <iostream >#include <cmath >#include <iomanip >using namespace std;int main(){

cout << "Value of pi = " << fixed <<setprecision (10) << M_PI << endl;

cout << "Vale of pi/2 = " << M_PI_2 <<endl;

cout << "Value of sin(pi/2) = " << sin(M_PI_2)<< endl;

return 0;}

#define is a preprocessor directive used here to define a symbolic constant; #undef

does the oppositeAgata Półrola Faculty of Mathematics and Computer Science, Łódź University

Introduction to Programming