Introduction to Algorithmic Processes CMPSC 201C Fall 2000.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
CS 117 Spring 2002 Basic Program Elements Chapter 2.
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.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Data Types, Expressions and Functions (part I)
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
COMPUTER SCIENCE I C++ INTRODUCTION
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
M.T.Stanhope Oct Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 1 C++ Basics u Data types. u Variables and Constants.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Control Structures (B) Topics to cover here: Sequencing in C++ language.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
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
Data Types Declarations Expressions Data storage C++ Basics.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
Programming Fundamentals
Recap……Last Time [Variables, Data Types and Constants]
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 2. Program Construction in Java. 01 Java basics.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output Samples.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 1.2 Introduction to C++ Programming
TK1913 C++ Programming Basic Elements of C++.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Completing the Problem-Solving Process
Basic Elements of C++.
Introduction to C++ October 2, 2017.
Basic Elements of C++ Chapter 2.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Programming Funamental slides
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Presentation transcript:

Introduction to Algorithmic Processes CMPSC 201C Fall 2000

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

Integers  short to  unsigned short0 to  int to  unsigned int0 to  long to  unsigned long0 to

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

Floating Point  Float  to  with 6 significant digits  double  to  with 15 significant digits  long double  to  with 19 significant digits

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

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)

Questions????

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

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

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.

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)

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

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)

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

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 ( ) is 13 while  int ( ) is 12

Questions?

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

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

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?

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

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

// 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 #include int main ( ) { // variable and constant declarations float rad; float vol; const float pi = ;

// 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; }

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.

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.

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.

Questions????