450 likes | 459 Vues
Learn how to calculate the square root using the Babylonian method, including variables, data types, constants, and special characters. Understand the role of different tools in the program compilation and execution process.
E N D
Cairo University, Faculty of Computers and Information CS112 – 2017 / 2018 2nd TermProgramming ILecture 3: C++ Basics II By Dr. Mohamed El-Ramly
Lecture Objectives • Pb #3 – Calculating the square root • Variables • Reserved words • Data Types • Constants • Special characters
Question ? • What is the role of each of the following tools in the program compilation and execution process? • Pre-processor • Compiler • Linker • Loader • IDE
I. Pb#3: Square Root Calculation • Babylonian method. • Begin with an arbitrary positive starting value x0 (the closer to the actual square root of S, the better). • Let xn+1 be the average of xn and S / xn • Repeat step 2 until the desired accuracy is achieved.
Problem Analysis & Solution Design • Understand ALL what is required and needed in the program. • What do we do with negative input ? • When do we stop ? • Develop the algorithm for solving it.
Solution Implementation • … we will do it together. • … debug any errors • … see code in acadox • First version // fixed number of trials • Second Version // reject wrong input • Third Version // try until desired accuracy
Solution Testing • … we will do it together. • … write some test cases ….. • 1. … • 2. … • 3 …
Elements of a Program • Variables: in which you can store the pieces of data that a program is working on. (global or local) • Types: indicating the nature of the expected values of variables. • Expressions: which compute new values from old ones. • Control flow: constructs which determine what order statements are performed in. • Function: which represents an entire set of statements and can be called as a unit by another piece of code
2. Variable • Name • Type • Value • Scope • Lifetime 0000 00000 num 0000 00004
2.1 Variable Name (Identifier) • The name must start with a letter or the underscore character (_). • All other characters in the name must be letters, digits, or the underscore. • No spaces or other special characters are permitted in names. • The name must not be one of the reserved keywords. • C++ Coding Standard / Programming Style Guide helps us choose good names. • Code Complete – Steve McConnell
Code Complete • Very Important Book
Which variable name are … • Valid Total_Sales_L.E Total_Sales_L.E total_sales T_S Total Sales Total Sales Total Sales totalSales1 $totalSales $totalSales ts
Which variable name are … • Valid Total_Sales_L.E Total_Sales_L.E • Good total_sales T_S T_S Total Sales Total Sales Total Sales totalSales1 $totalSales $totalSales ts ts
Variables Names are Case Sensitive • All these are different variables and • NOT THE SAME ONE ToTaLsAlEs totalsales TotalSales TOTALSALES totalSales totalSALES
2.2 Variable Type • A data type defines a domain, i.e. a set of possible values. • A data type defines a behavior, i.e. a set of operations that can be performed on data of this type. • A data type defines how to interpret memory locations of this type. • Much of C++ power comes from ability to define new data types from existing ones.
Example Type int • Domain • Max, Min • Behavior • + - / * % • How to interpret memory locations • 4 bytes -> Two’s Complement 1.4 2.3 -9.1 0.11111111 1.32 e-15 1 0 -3 -20000
auto Keyword (C++11) • Since 2011 • It supports type inference • The compiler finds the type that should be used from the data assigned to it. • Example • for (auto i = 1; i < 5; i++) • for (autoi = 0.1;i < 0.5;i+=0.1) int double
Variable Declaration • int num; • int num = 10; • float age, salary; • float age = 10, salary = 1000;
2.3 Variable Value • char c; • c = 'A'; • c = 'd'; • c = '\n'; • c = '\\'; • bool x = true; • x = false; • Make sure that you enter the right type of data to a variable because the compiler will not object! ?? A d d
2.4 Variable Scope • Scope means where this variable is knownand where you can use it. • What is wrong with this program? #include <iostream> using namespace std; int main () { cout << value; int value = 5; }
2.4 Variable Scope • Scope means where this variable is known and where you can use it. • What is wrong with this program? #include <iostream> using namespace std; int main () { for (inti = 1; i <= 5; i++) cout << i << endl; cout<< endl << "At end i = " << i; }
3. Data Type • A data type is a way to tell the computer how to store a variable or represent the result of an expression. • Example • float salary; // take 4-bytes for float • 3 + 4; // return int value • 3.0 + 4.0; // return double value;
Basic Data Types • int, long, short, unsigned • inthas no standard limits, but usually 32,767 on 32-bit machines. This hinders portability. • 040 (octal), 0x40 (hexa), 408L (long) • float, double, long double • Uses IEEE 754 format • char • Stores one character, char letter = 'c';
Thebool Data Type • Only in C++ not in C • Takes the two values of trueor false. • trueis stored as 1 and false is stored as 0 • boolisBig = true; • boolisSmall = false; • cout << isBig << endl; • cout << isSmall<< endl;
Automatic Type Conversion • When two operand of different types are used in the same expression, the lower-type variable is converted into the higher -type variable.
Type Casting • The programmer asks the compiler explicitly to convert to a specific type.
Escape Sequences • \n means new line character • \\ means backslash \ character • \t means tab character • \t means alert sound • \" means " character
5. Constants • Some values are constants that should not change. • For some values, I need to freeze them in the program and if changed, they need to change everywhere. • const float PI = 3.1415; • const float taxRate = 0.08; • taxRate = 0.1; // ERROR
Readings • Problem Solving with C++, Walter Savitch • Chapter 2: Basics of C++
Challenges • Pb #4 – Calculate the square root of a number S using Bakhshali’s method. • x0 is the initial guess and next guesses are xn.
Challenges • Pb #5 – Calculate the square root of a given binary number using the algorithm described in the video • https://www.youtube.com/watch?v=G_4HE3ek4c4
Challenges • Pb #6 – Write a program that reads in ten whole numbers and that outputs • the sum of all the numbers greater than zero, • the sum of all the numbers less than zero (which will be a negative number or zero), and • the sum of all the numbers, whether positive, negative, or zero. • The user enters the ten numbers just once each and the user can enter them in any order.