1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.

Slides:



Advertisements
Similar presentations
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
Advertisements

1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS150 Introduction to Computer Science 1
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
Data types and variables
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CS150 Introduction to Computer Science 1
The If/Else Statement, Boolean Flags, and Menus Page 180
Chapter 2 Data Types, Declarations, and Displays
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.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Input & Output: Console
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
Instructor - C. BoyleFall Semester
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
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
COMP Primitive and Class Types Yi Hong May 14, 2015.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Recap……Last Time [Variables, Data Types and Constants]
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Type Conversion, Constants, and the String Object
Basic Elements of C++ Chapter 2.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
Primitive Types and Expressions
Presentation transcript:

1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1

2 9/20/06CS150 Introduction to Computer Science 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja 4 // Date: 08/23/ // Purpose: This program displays a welcome message to 6 // the user 7 //*********************************************************** 8 #include 9 #include “stdafx.h” using namespace std; int main() 14 { 15 string name; cout << “Type your name, then press enter” << endl; cin >> name; /* read in the user’s name */ cout << “Hello “ << name << “!” << endl; return 0; 24 } What are the important parts of this program? Keywords, variables, libraries, main function, input, output, comments.

3 9/20/06CS150 Introduction to Computer Science 1 Declaration Statements  Tells the compiler o The variable’s type o The variable’s name  Examples of declaration statements const double PI = 3.14; double area; char character = ‘a’; int count; bool bIsItASquare; unsigned int bigNumber = ;

4 9/20/06CS150 Introduction to Computer Science 1 Identifiers  C++ does place limits on what names you can call your variables  Rules 1. Identifiers must begin with a letter or an underscore 2. Identifiers must consist of letters, numbers and underscore, nothing else 3. Identifiers cannot be a reserved keyword

5 9/20/06CS150 Introduction to Computer Science 1 How to Choose a Data Type  Ask yourself the following questions o Is the data a number or character?  Is the data a list of characters? o What are the largest and smallest numbers that may be stored? o How much memory does the variable use? o Is the variable signed (positive and negative)? o How many decimal places of precision does the variable need?

6 9/20/06CS150 Introduction to Computer Science 1 Variable Ranges TypeSizeValues int 4 bytes–2,147,483,648 to 2,147,483,647 short int 2 bytes–32,768 to 32,767 long int 4 bytes–2,147,483,648 to 2,147,483,647 unsigned int 4 bytes0 to 4,294,967,295 char 1 byte256 character values float 4 bytes1.2e–38 to 3.4e38 double 8 bytes2.2e–308 to 1.8e308

7 9/20/06CS150 Introduction to Computer Science 1 Arithmetic Operators  Operators allow us to manipulate data o Unary: operator operand o Binary: operand operator operand OperatorMeaningTypeExample -NegationUnary - 5 =AssignmentBinary rate = 0.05 *MultiplicationBinary cost * rate /DivisionBinary cost / 2 %ModulusBinary cost % 2 +AdditionBinary cost + tax -SubtractionBinary total - tax (left hand side) (right hand side)

8 9/20/06CS150 Introduction to Computer Science 1 Division  grade = 100 / 40; grade is 2 o If both operands of the division operator are integers, then integer division is performed. o We say the integer is truncated. Everything after the decimal point is dropped. No rounding.  grade = / 40; o grade is 2.5 o What data type should grade be declared as?

9 9/20/06CS150 Introduction to Computer Science 1 Modulus  Modulus is the remainder after integer division  grade = 100 % 20; o grade = ?  grade = 100 % 30; o grade = ?  rem = x % n; o What are the possible values for rem ?

10 9/20/06CS150 Introduction to Computer Science 1 Exponents  The exponent operator was missing from the list!x 2 y n  C++ does not provide an exponent operator as part of the language  Use pow() in the cmath library #include double area; area = pow(4, 2); // area = 4 2

11 9/20/06CS150 Introduction to Computer Science 1 Implicit Type Conversion (3.3)  What happens when we mix the data types of operands during mathematical operations o What happens when we save a double as an int? o What happens when an int is multiplied by a float?  Data types are ranked  A data type outranks another if it can hold a larger number

12 9/20/06CS150 Introduction to Computer Science 1 long double double float unsigned long long unsigned int int Highest Lowest

13 9/20/06CS150 Introduction to Computer Science 1 Rules for Type Conversion  Rule 1: char, short, and unsigned short are automatically promoted to int  Rule 2: When an operator works with values of different types, the lower ranking value is promoted to the higher ranking  Rule 3: When the value of an expression is assigned to a variable, it is converted to the data type of that variable

14 9/20/06CS150 Introduction to Computer Science 1 Uses of Type Casting  Preventing integer division int books = 30, months = 7; double booksPerMonth; booksPerMonth = static_cast (books) / months; o What about this statement? booksPerMonth = static_cast (books / months);  Displaying a char from its ASCII value int number = 65; cout (number)

15 9/20/06CS150 Introduction to Computer Science 1 Examples of Combined Assignments OperatorExample UsageEquivalent To +=x += 5;x = x + 5; -=y -= 2;y = y – 2; *=z *= 10;z = z * 10; /=a /= b;a = a / b; %=c %= 3;c = c % 3;

16 9/20/06CS150 Introduction to Computer Science 1 Relational Operators, Explained! OperatorMeaning > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to o All are binary operators o Left to right associativity

17 9/20/06CS150 Introduction to Computer Science 1 Relational Expression  An expression is a statement that has value  Relational expression: an expression that uses a Relational Operator o its value is a Boolean value (True or False) int x=9, y=42; x > y y == x // y = x; is the assignment operator x <= (x * y + 99)

18 9/20/06CS150 Introduction to Computer Science 1 Precedence (page 1125) Precedence Operators (Highest to Lowest) (unary negation) - * / % + - > >= < <= == != = += -= *= /= %= Relational Operators Assignment Operators Arithmetic Operators

19 9/20/06CS150 Introduction to Computer Science 1 Grouping!  To override precedence we use grouping symbols, ( ) o average = ( a + b +c ) / 3;  (3 + 12) *  % (3 + 9)  * 9 / ((3 * 4) – 9) o Work from the inside ( ) outward

20 9/20/06CS150 Introduction to Computer Science 1 Boolean value (True or False)  How does the computer represent True and False?  New data type: bool bool tValue = true; // 1 bool fValue = false; // 0

21 9/20/06CS150 Introduction to Computer Science 1 Formally defined  What is an expression? if( expression ) { statement 1; statement 2;... statement n; } Just like a function, start at the top and execute in order to the bottom

22 9/20/06CS150 Introduction to Computer Science 1 Boolean Flags  We have seen how to store the value of a relational expression to a bool variable. bool bIsSquare = ( length == width); if ( bIsSquare ) { }  Why would you want to do this?  Why not use the relational expression directly?

23 9/20/06CS150 Introduction to Computer Science 1 If/Else  C++ provides a shortcut to combine two if statements: if( expression ) { // do stuff } else { // do other stuff }  The statements in the else clause are executed only when the expression is false.