Exercise 1 – Datentypen & Variablen

Slides:



Advertisements
Similar presentations
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Advertisements

CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
Chapter 2 Data Types, Declarations, and Displays
Basic Elements of C++ Chapter 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Fundamental Programming: Fundamental Programming Introduction to C++
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
CSC 107 – Programming For Science. Announcements.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
A Sample Program #include using namespace std; int main(void) { cout
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 2: Basic Elements of C++
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
User Interaction and Variables
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Programming Fundamentals
Documentation Need to have documentation in all programs
Computing Fundamentals
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Object Oriented Programming
Data Types, Identifiers, and Expressions
Revision Lecture
Introduction to C++ October 2, 2017.
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Variables Kingdom of Saudi Arabia
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Data Types and Expressions
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Summary of what we learned yesterday
Data Types and Expressions
Programming Fundamental-1
Data Types and Expressions
Presentation transcript:

Exercise 1 – Datentypen & Variablen Informatik I / D-ITET Informatik I / D-ITET Exercise 1 – Datentypen & Variablen 1

Agenda Informatik I / D-ITET Variables Declaration & Assignment Allowed names for variables Type Conversion Operators & Arithmetic operations cin & cout Functions Scope 2 2

Data-types Informatik I / D-ITET C++ has 5 built-in basic Datatypes Function Memory int Saves integer numbers 2 Byte float Saves real numbers 4 Byte double Saves real numbers with higher precision than the float data-type 8 Byte bool Saves logical expressions (True/False or respectively 0/1) 1 Byte char Saves characters Memory size may vary, because they depend on the implementation and Hardware. 3 3

Data-types Informatik I / D-ITET The memory is binary: Larger Numbers need many bits: Overflow: int i = MAX_INT; //highest possible value i++; // overflow! Lowest negative value! 5 -> 101 4 -> 100 12 -> 1100 32,767 -> 0111111111111111 s 01111111 11111111 16 bits = 2 bytes 4 4

Declaration & Assignment Informatik I / D-ITET Informatik I / D-ITET Declaration & Assignment Declaration of a variable The syntax for declaring a variable is as follows: <type> <name>; <type> <name> = <Number/variable>; For example: int My_first_Variable; Declares a integer Variable with the Names „My_first_Variable“. 5 5

Declaration & Assignment Informatik I / D-ITET Declaration & Assignment More Examples: double My_first_Variable; My_first_Variable = 5.994; This declares a variable and assignes a value to it one line later. This can also be done like this: double My_first_Variable = 5.994; Thus declaring and assigning the Variable in one step 6

Specifiers Informatik I / D-ITET Specify a modifier to the variable type Optional <specifier> <type> <name>; Example: unsigned int ui; Specifier Function long Variable gets double the memory short Variable gets half the memory unsigned Variable will NOT be able to have a sign (only positives can be stored), thus is able to save numbers with more accuracy/ more range const Variable value cannot be changed 7 7

Allowed names for variables Informatik I / D-ITET Informatik I / D-ITET Allowed names for variables Not all names are allowed for variables! Variable names may only contain alphabetic characters, numeric digits and the underscore character (_) The first character cannot be a numeric digit You cannot use C++ reserved words as names (such as main, int or for) Names should not begin with underscore Characters Uppercase characters are considered to be different from lowercase characters 8 8

Allowed names for variables Informatik I / D-ITET Informatik I / D-ITET Allowed names for variables Examples: int firstname; int Firstname; int leet1337; int 1337leet; int what_a_great_name; int var!; int _thisvar; \\Valid \\Valid, variable differs from firstname \\Not Valid, begins with number \\Not Valid, contains special character \\Valid, but shouldn’t be used, starts with _ 9 9

Arithmetic operations Informatik I / D-ITET Informatik I / D-ITET Arithmetic operations There are 5 types of different Arithmetic Operators: plus (+), minus (-), times(*), divided(/) and Modulo(%) The first 4 are intuitive Modulo returns the remainder of a division For example: 5 % 3; //Result = 2 because 3 goes into the 5 one time with a rest of 2 21 % 5; //Result = 1 10 10

Arithmetic operations Informatik I / D-ITET Informatik I / D-ITET Arithmetic operations Arithmetic Operations are evaluated as follows: Brackets before operators Division, Multiplication and Modulo before Minus and Plus If two arguments have the same precedence level, expression is evaluated left to right 11 11

Arithmetic operations Informatik I / D-ITET Arithmetic operations Examples 5 * 4 + 1; //21 5 * (4 + 1); //25 2 * 3 + 3 * 4 / 2; //12 9 % 4 + 1; //2 9 % (4 + 1); //4 5 * 3 % 4 * 2; //6 5 * (3 % 4) * 2; //30 12

Operators Informatik I / D-ITET But other operators also exist: Assignment: int x,y; x = y; x *= y; // x = x * y; x /= y; x += y; x -= y; x %= y; y = x++; // y = x; x = x + 1; y = ++x; // x = x + 1; y = x; y = x--; // y = x; x = x – 1; y = --x; // x = x -1; y = x; 13

Operators Informatik I / D-ITET But other operators also exist (will be discussed next week): Comparisons int x,y; bool a,b; a == b, x == y, a != b, x != y x > y, x < y, x >= y, x <= y Logical operators !a, a && b, a || b a | b, a & b, a ^ b 14

Type Conversion Informatik I / D-ITET Consider the following code: float ft_var = 5.453; int it_var = ft_var; The Variable „it_var“ cannot save a real number. So what happens when this code is executed? The Answer is, the value which will be assigned to „it_var“ will be converted. This process is called Conversion. 15

Type Conversion Informatik I / D-ITET The assigned value will be converted to type of the variable to which it is assigned to At our Example: float ft_var = 5.453; int it_var = ft_var; This means that the value 5.453 will be converted to an integer. C++ does that by truncating the fractional part of the value Thus it_var has the value 5 after the assignment 16

Type Conversion Informatik I / D-ITET Assignments: Whenever a numerical value is assigned to a variable of another type, the value will be converted to the type of the receiving variable. 17 17

Type Conversion Informatik I / D-ITET Conversion in Expression: A second time of the conversion of variables, is the conversion in Arithmetic expressions in which several types of variables are included like the following: 5.453f * 3 * 9.86 double float int 18

Type Conversion Informatik I / D-ITET A rule of thumb in these cases is that C++ converts the expression into the more general type of the two variables. Thus at our example 5.453f(float) * 3(int) * 9.86(double) = 16.359(float) * 9.86(double) = 161.29974(double) 19

Type Conversion Informatik I / D-ITET A special note: divison of int variables The resulting type of such a division is still an integer variable Rational parts of the division will be truncated Thus: 5 / 2 == 2 //The .5 is truncated 1 / 2 == 0 //Same reason here 20

Type Conversion Informatik I / D-ITET Even more examples 13.0f * 4 = 52.0 (float) 7.0 * (3 / 7) = 0.0 (double) 20 / (10 / 6) = 20 (int) 20 / (10.0 / 6) = 12 (double) 21 21

cin & cout Informatik I / D-ITET So lets get back to some programming Cin and cout are your way to talk to the user With these „objects“ you can print messages on the console For example the program „Hello World“ But you can also print variables on it You can even read in variables 22

cin & cout Informatik I / D-ITET Enable use of cin and cout by: #include <iostream> using namespace std; Use cin and cout with the shift operator ( << or >> ): cout << “using cout“; // printing int i; cin >> i; // reading 23

cout Informatik I / D-ITET The order is important: cout << “using cout“; // valid cout >> “using cout“; // invalid “using cout“ >> cout; // invalid Concatenation is valid: cout << “pri“ << “nting“; // valid Use of variables and numbers is also valid: cout << “var: “ << i << “ num: “ << 3; Note the differenence: cout << “i“; cout << i; 24

cout Informatik I / D-ITET New line: cout << “line1\nline2“; // 2 lines cout << “line1“; cout << “line2“; // just 1 line! cout << “line1“ << “\n“; cout << “line2“; // ok cout << “line1“ << endl << “line2“; 25

cin Informatik I / D-ITET Usage: int i; cin >> i; Can also concatenate: int a,b; cin >> a >> b; Converts to relavent types: int a; cin >> a; // waits for user input // if user inputs a string then a=0 26

cin Informatik I / D-ITET Parses by spaces and new line, but waits for new line: int a,b; cin >> a; // waits for user input // if user input is “65 73“ and Enter // then a = 65; cin >> b; // b = 73 without waiting cin >> a; // waits for user input again 27 27

Structure of a function Informatik I / D-ITET Structure of a function return type function name argument type argument int main ( int argc, char* argv[] ) { cout << “Hello, World!” << endl; return 0; } argument list function body return value function name: name of the function function body: statements to be executed argument: variable whose value is passed into function body from the outside argument type: type of the argument return value: value that is passed to the outside after function call return type: type of the return value (void if there is no return value) 28

Functions Informatik I / D-ITET Advantages Readability int main() { cout << “Please enter two integers: "; int a,b; cin >> a >> b; int res = (a + b) * (a + b); cout << “The result is: " << res << endl; return 0; } 29

Functions Informatik I / D-ITET Advantages Readability int square_of_sum(int i1, int i2) { int r = i1 + i2; return r*r; } int main() cout << “Please enter two integers: "; int a,b; cin >> a >> b; int res = square_of_sum(a,b); cout << “The result is: " << res << endl; return 0; 30

Functions Informatik I / D-ITET Advantages Code re-use int square_of_sum(int i1, int i2) { int r = i1 + i2; return r*r; } int main() cout << “Please enter four integers: "; int a,b,c,d; cin >> a >> b >> c >> d; int res1 = square_of_sum(a,b); int res2 = square_of_sum(c,d); cout << “the results are: " << res1 << “,” << res2 << endl; return 0; 31

Functions Informatik I / D-ITET Arguments and return value Both are optional Invocation of a function must have () Decleration must be before invocation int square_of_sum(int i1, int i2) res1 = square_of_sum( a, b); void print_message() { cout << “This is also a function“ << endl; } Function name Variable name Should be declared first print_message(); 32 32

Scope Informatik I / D-ITET Errors? Scope = validity/visibility of a variable int nonsense(int input) { return input * x; } int main() int x = 3; if (x == 3) int y = 15; int z = nonsense(y); cout << z << endl; return 0; Errors? 33

Scope Informatik I / D-ITET int nonsense(int input) { return input * x; } int main() int x = 3; if (x == 3) int y = 15; int z = nonsense(y); cout << z << endl; return 0; x is not visible here; only visible inside «main» function y is not valid outside { } we cannot use y as an argument for function «nonsense». Scope of x 34