1 / 45

C++ Basics II: Square Root Calculation

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.

mascencio
Download Presentation

C++ Basics II: Square Root Calculation

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. Cairo University, Faculty of Computers and Information CS112 – 2017 / 2018 2nd TermProgramming ILecture 3: C++ Basics II By Dr. Mohamed El-Ramly

  2. Lecture Objectives • Pb #3 – Calculating the square root • Variables • Reserved words • Data Types • Constants • Special characters

  3. Question ? • What is the role of each of the following tools in the program compilation and execution process? • Pre-processor • Compiler • Linker • Loader • IDE

  4. 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.

  5. 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.

  6. 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

  7. Solution Testing • … we will do it together. • … write some test cases ….. • 1. … • 2. … • 3 …

  8. 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

  9. 2. Variable • Name • Type • Value • Scope • Lifetime 0000 00000 num 0000 00004

  10. 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

  11. Code Complete • Very Important Book

  12. C++ Reserved Words

  13. 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

  14. 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

  15. Variables Names are Case Sensitive • All these are different variables and • NOT THE SAME ONE ToTaLsAlEs totalsales TotalSales TOTALSALES totalSales totalSALES

  16. 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.

  17. 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

  18. 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

  19. Variable Declaration • int num; • int num = 10; • float age, salary; • float age = 10, salary = 1000;

  20. 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

  21. 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; }

  22. 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; }

  23. 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;

  24. 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';

  25. 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;

  26. C / C++ Main Data Types

  27. C++11 Fixed Width intData Types

  28. 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.

  29. Type Casting • The programmer asks the compiler explicitly to convert to a specific type.

  30. 4. Special Characters

  31. Escape Sequences • \n means new line character • \\ means backslash \ character • \t means tab character • \t means alert sound • \" means " character

  32. 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

  33. Readings • Problem Solving with C++, Walter Savitch • Chapter 2: Basics of C++

  34. 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.

  35. 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

  36. 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.

More Related