Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Algorithmic Processes CMPSC 201C Fall 2000.

Similar presentations


Presentation on theme: "Introduction to Algorithmic Processes CMPSC 201C Fall 2000."— Presentation transcript:

1 Introduction to Algorithmic Processes CMPSC 201C Fall 2000

2 Data Types  Numbers  Integers - whole numbers  Floating Point - contain fractional parts  Characters

3 Integers  short -32766 to 32767  unsigned short0 to 65535  int-32766 to 32767  unsigned int0 to 65535  long-2147483647 to 2147483646  unsigned long0 to 4294967295

4 Integers  Long integers require more memory space than int or short.  However, errors may occur if int value is larger than 32767, e.g. 32768 may be represented as 1 or -1 depending on the compiler.

5 Floating Point  Float  10 -37 to 10 38  with 6 significant digits  double  10 -307 to 10 308  with 15 significant digits  long double  10 -4931 to 10 4932  with 19 significant digits

6 Floating Point  Required memory increases with increased range and significant digits.  Required time for calculations increase with significant digits.

7 Character  Used to represent characters - letters, spaces, numbers not used for arithmetic operations  char variable can store a single character or an escape sequence.  ‘A’, ‘b’, ‘ ‘,  ‘\n’new line  ‘\t’tab  ‘\f’form feed (new page)

8 Questions????

9 Arithmetic Operations  Unary - one operand  positive and negative  +a or -zebra  Binary - two operands  addition+var1 + var2  subtraction-var1 - var2  multiplication*var1 * var2  division/var1 / var2  remainder (modulus)%var1 % var2

10 Precedence  Parenthesis  Unary operations  Multiplication, Division, Remainder  Addition, Subtraction

11 Arithmetic Operations  In general expression involving only integers will result in an integer and expressions involving only floating point numbers will result in a floating point number.  Mixed expressions involving both floating point and integer numbers will result in a floating point number. However this result may be unexpected.

12 Integer Division  Remember that integers are whole numbers.  Integer division may have unintended results depending on the data type of the variables involved.  int var1 = 10  int var2 = var1 / 8(1)  float var3 = var1 / 8(1.0)  float var4 = var1 / 8.0(1.25)

13 Integer Division (Cont.)  The expression  5.5 * 7 /2  is not the same as the expression  7 / 2 *5.5

14 Data Type Casting  Casting is the explicit conversion of data type within an expression.  Suppose you have declared two int variables distance and time and the float variable speed. The expression speed = distance/time would lose the fractional component of speed. To avoid this loss you could do one the following.  speed = float(distance) / time  speed = distance / float (time)  speed = float (distance) / float (time)

15 Data Type Casting (Cont.)  Note that the expression  speed = float (distance / time)  would still lose the fractional part of speed.

16 Data Type Casting (Cont.)  When a float variable is casted (converted) to an integer value, value is truncated not rounded.  int (12.6) is 12 not 13  To round add 0.5 before converting.  int (12.6 + 0.5) is 13 while  int (12.1 + 0.5) is 12

17 Questions?

18 General Syntax Rules  Statements that should be executed should end in a semicolon (;)  Comments are designated with // or /* & */  Library header files require a line similar to #include

19 Input/Output  cin >> variable name;  cout<< “text”, variables, expressions;  require the library header file iostream.h (the line #include must be included in your source code.)

20 Example Program Suppose we want to develop a program that calculates the volume of a sphere given a radius that the user enters. What do we need to do?

21 Example Program (cont.) ßDefine the problem ßDetermine what objects we need ßvariable for input of radius ßvariable for output of volume ßconstant for storing pi ßDevelop algorithm ßask user for radius  calculate volume (v=4/3  r 3 ) ßoutput volume

22 Example Program (cont.) ßTranslate into C++ ßReview ßUpgrade and maintain

23 // This program is to calculate the volume of a sphere // using the formula Volume = 4/3 * pi * rad*rad*rad. // Input parameter is the radius // Output parameter is the volume // Constant pi as 3.14159 #include int main ( ) { // variable and constant declarations float rad; float vol; const float pi =3.14159;

24 // ask user to input radius cout << "Please enter the volume of the sphere."<<endl; cin >>rad; //calculate volume vol = 4.0/3*pi*rad*rad*rad; // output volume cout << endl<<"The volume of the sphere of radius "<<rad << " is "<<vol; return 0; }

25 Items to note ßThe lines containing “include and “int main ( )” do not end with a semicolon. ßUsed comments to designate what sections of the code do. ßDeclared all variables at the beginning--you do not have to. ßUse braces ( { } ) to designate all statements that belong to the function main.

26 Items to note (cont.) ßUsed multiple output items ßcout << "Please enter the volume of the sphere."<<endl; ßuse of “endl” -- could also use “\n” ßOutput statement continued on a second line. ßcout << endl<<"The volume of the sphere of radius "<<rad << " is "<<vol; ßused indentation to show continuation of same statement.

27 Items to note (cont.) ßUsed expanded arithmetic expression to calculate the cube of the radius. ßvol = 4.0/3*pi*rad*rad*rad; ßwhy did I use 4.0 rather than 4 ßCould use the built in power function ßvol = 4.0/3*pi*pow( rad, 3 ) ßneed to include math library file (math.h) ßsee Table 2.5, pg. 59, for additional functions and the associated library files.

28 Questions????


Download ppt "Introduction to Algorithmic Processes CMPSC 201C Fall 2000."

Similar presentations


Ads by Google