1 / 25

Informatik I (D-ITET) Group 7, 15:00-17:00, ETZ H91 Assistant: Ercan Ucan

Informatik I (D-ITET) Group 7, 15:00-17:00, ETZ H91 Assistant: Ercan Ucan. Slides at http://people.inf.ethz.ch/eucan/inf-1 /. Exercise 1 (1/10/2012) . Administratives (I). Exercises Session takes place every Monday 15:00-17:00 @ ETZ H91

taffy
Download Presentation

Informatik I (D-ITET) Group 7, 15:00-17:00, ETZ H91 Assistant: Ercan Ucan

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Informatik I (D-ITET)Group 7, 15:00-17:00, ETZ H91Assistant: Ercan Ucan Slides at http://people.inf.ethz.ch/eucan/inf-1/ Exercise 1 (1/10/2012)

  2. Administratives (I) Exercises • Session takes place every Monday 15:00-17:00 @ ETZ H91 • Both theoretical and practical exercises to be solved and handed in • You can deliver exercises in groups of 2 persons. How to hand in your (hopefully correct) exercises • By e-mail before the given deadline. (eucan@inf.ethz.ch) • C++ code for the practical exercises you have to write code for. • PDF file with all parts of your solutions (both theoretical and practical). • If the practical exercises require some kind of output from your code please include it in the final .pdf • Your email title should be ‘Informatics 1 Exercise 1 submission’ • Email body should contain your full name and legi-nr. Section - Subsection

  3. Administratives (II) Grading scheme • 1 point for a complete solution, or a report that shows significant effort. • ½ points for dealing with ~50% of the assignment. You need 9 points to get a Testat Questions • If you have a subject you would like to discuss in the exercise session please send me a mail before in order to prepare it. • You can (of course) ask questions on-the-fly while having the tutorial. Please do so. • I cannot provide you with office hours but send me an email to set up an appointment to ask questions. If you pass by my office and I am there, also feel free to ask me your questions. Section - Subsection

  4. Identifiers (I) An identifier is a sequence of characters used to denote one of the following: • Object or variable name • Class, structure, or union name • Enumerated type name • Member of a class, structure, union, or enumeration • Function or class-member function • typedef name • Label name • Macro name • Macro parameter • A valid identifier is a sequence of one or more letters, digits or underscore characters (_). • Neither spaces nor punctuation marks or symbols can be part of an identifier. Section - Subsection

  5. Identifiers (II) • In addition, variable identifiers always have to begin with a letter. • They can also begin with an underline character (_ ), but in some cases these may be reserved for compiler specific keywords or external identifiers, as well as identifiers containing two successive underscore characters anywhere. • In no case they can begin with a digit. • Your own identifiers cannot match any keyword of the C++ language nor your compiler's specific ones, which are called reserved keywords. The standard reserved keywords are: asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while • Very important: The C++ language is a "case sensitive" language. That means that an identifier written in capital letters is not equivalent to another one with the same name but written in small letters. Thus, for example, the RESULT variable is not the same as the result variable or the Result variable. Section - Subsection

  6. Data Types Section - Subsection

  7. Declaration of variables • A variable is a “place to store a piece of information/data” • In order to use a variable in C++, we must first declare it specifying which data type we want it to be. • First write the specifier of the desired data type (like int, bool, float...) and then a valid variable identifier, e.g: • int a; • float mynumber; • If you are going to declare more than one variable of the same type, you can declare all of them in a single statement by separating their identifiers with commas, e.g: • int a, b, c; • Initializing variables, e.g: • int a = 0; Section - Subsection

  8. Variable scope Global variables Local variables Instructions Section - Subsection

  9. Type casting Type casting is the conversion of an expression of a given type into another type and it can be done in two ways: • Implicit conversion • Implicit conversions do not require any operator. They are automatically performed when a value is copied to a compatible type. • short a=2000; int b; b=a; • Explicit conversion • short a=2000; int b; b = (int) a; Section - Subsection

  10. Operators in C++ • An operator is a symbol that causes the compiler to take an action. • Operators act on operands, and in C++ all operands are expressions. • Types of operators: • Assignment • Arithmetic Section - Subsection

  11. Assignment operators • The assignment operator (=) causes the operand on the left side of the assignment operator to have its value changed to the value on the right side of the assignment operator • x = a + b; x = 35; • 35 = x; // Wrong • Addition (+), subtraction (-), multiplication (*), division (/), and modulus (%) • Integer Division : 21 / 5 = 4 • Modulus : Gives the remainder : 21 % 4 = 1 Arithmetic operators Section - Subsection

  12. Compound assignments (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) • Examples: • value += increment → value = value + increment; • a -= 5 → a = a - 5 • a /= b → a = a / b • c++; • c+=1; equivalent • c=c+1; Increase and decrease (++, --) Section - Subsection

  13. Logical operators • NOT (!) • !(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true. • !(6 <= 4) // evaluates to true because (6 <= 4) would be false. • !true // evaluates to false • AND (&&) • ( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false ). • OR (||) • ( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ). Short – Circuit Evaluation: C++ evaluates from left to right ONLY what is necessary, e.g. for the OR if the left-hand expression is true, the right-one is not evaluated Section - Subsection

  14. Comparison operators • ==, !=, <, >, <=, >= • Compare two operands and the result is boolean: true or false. • e.g. if a=2, b=3, c=6 • (a == 5)// evaluates to false since a is not equal to 5. • (a*b >= c) // evaluates to true since (2*3 >= 6) is true. • (b+4 > a*c) // evaluates to false since (3+4 > 2*6) is false. • ((b=2) == a) // evaluates to true. • If you compare chars, you compare their respective ASCII codes. • e.g: 'A' >= 'b' evaluates to false since • ‘A’ = 65 and ‘b’=98 in ASCII code ?? ?? ?? ?? ?? Section - Subsection

  15. Programming Environment Console (Linux/Unix/MacOSX) • You need a text editor of your choice • Write the code and save it as .cpp , e.g. mycode.cpp • Run the C++ compiler, e.g. • g++ -g -Wall -O -o prog mycode.cpp • If everything finishes smoothly you can run your executable program, e.g. • ./prog

  16. Example Program – How to add two numbers • // To be able to use cin, cout • #include <iostream> • using namespace std; • int main() { • double a,b,sum; • cout << "Please give the 2 numbers to be added" << endl; • cout << "Please give a: "; • cin >> a; • cout << "Please give b: "; • cin >> b; • sum = a + b; • cout << "The sum a+b is : " << sum << endl; • return 0; • }

  17. Exercise 1 – Valid Identifier Names. • You are asked to identify which of the provided declarations are valid identifier names: • float x3; • boolnot_available; e.g. this is a valid identifier • char my Name; • int 42; • double position; • int _555; ??

  18. Exercise 2 – Evaluation of Algebraic Operators • You are asked to calculate the result and the data type of the result of the following expressions: • 1 + 3 * 2 • 13 / 7 • 9 * 3 / 2.0 e.g= (double) 13.5 • 30 % 7 • 15.0 / 5 • 9/ 5 * 5 • 2.0 * 5 / 2 • 11.0 / 4.0 ??

  19. Exercise 2 – Evaluation of Algebraic Operators • You have to take into account the priority order of the operators ! • For example: 1 + 7 / 3 = (int) 3 • 9.0 * 4 / 3 = (double) 12.0 Priority order • Source: http://www.cplusplus.com/doc/tutorial/operators/

  20. Exercise 3 – Explicit Type Conversion • You are asked to calculate the result and the data type of the result of the following explicit type conversions: • a) (double)(9/5) • b) (int) 14.0 / 4 • c) 7 / (double) 2 • d) (int) (15.0/6) • For example: 14 / (int) 5.0 = (int) 2 ??

  21. Exercise 4 – Evaluation of comparison operators • You are asked to calculate the boolean return value of the following expressions involving comparison operators: • a) 3 >= 3 • b) (7 > 6) == false • c) 4.5 > -0.1 • d) ’n’ >= ’Q’; (Attention: ASCII-Code) • e) ’y’ == 120; (Attention: ASCII-Code) • f) 2 < 6 < 4 (Achtung: Stolperfalle) • e.g. 1<8<2 -> true since 1<8 is true = 1 ??

  22. Exercise 4 – Evaluation of logical operators • You are asked to calculate the boolean return value of the following expressions involving logical operators: • a) true && !false • b) true && (true || false) • c) (5.1 < 3.1) && (3.1 < 12.3) • d) !(9 % 2) || 9 % 3 • e.g. !(9 % 3) && (3 < 4) -> 1 && 1 -> true ??

  23. Exercise 6 – Evaluation of Complex Instructions a = ? b = ? c = ? d = ? e = ? • char a = ’5’; • double b = 0.0; • double c = 0.0; • int d = 3; • int e = 0; b = d = 81.0 / a + 1; c = 12.3 <= 3-2 || (e = 7); a = (char)((int)’M’/2);

  24. Exercise 7 – Program Analysis • 01 #include <iostream> • 02 using namespace std; • 03 • 04 int main() • 05 { • 06 int s1, s2 = 3; • 07 double d1, d2 = 5; • 08 • 09 s1 = 1; • 10 d1 = 4.0; • 11 • 12 d2 = d2 + 10 / d1; • 13 s1 = 12 * s2 + 15; • 14 • 15 cout << '1' + '2' << endl; • 16 cout << 1 + 2 + 4 << endl; • 17 cout << "1" << "2" << endl; • 18 cout << d2 << " " << d1*2+1 << endl; • 19 return 0; • 20 } • s1=?, s2=?, d1=?, d2=? • s1=?, s2=?, d1=?, d2=? • s1=?, s2=?, d1=?, d2=? • ? • ? • ? • ?????

  25. Exercise 8 – Calculate the value of a resistor • You are asked to write a small program that prints the equivalent resistance of two serial resistors connected in parallel, i.e. R1 and R2 are connected in series, and R3 and R4 are connected in series. The equivalent resistances R12 and R34 to R1234 are connected in parallel. R12 R2 R1 R34 R4 R3 R1234 http://en.wikipedia.org/wiki/Series_and_parallel_circuits#Resistors_2

More Related