1 CS 1430: Programming in C++. 2 Literal Values Literal values of int 5 0 -100 Literal values of float 3.1415926 0.0 -12.5.

Slides:



Advertisements
Similar presentations
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Advertisements

CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
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.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
The If/Else Statement, Boolean Flags, and Menus Page 180
Chapter 2: Introduction to C++.
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Basic Elements of C++ Chapter 2.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
Chapter 4 Selection Structures: Making Decisions.
Chapter 2 Overview of C++. A Sample Program // This is my first program. It calculates and outputs // how many fingers I have. #include using namespace.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Fundamental Programming: Fundamental Programming Introduction to C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 2- 1 June 3, June 3, 2016June 3, 2016June 3, 2016 Azusa, CA.
Chapter 05 (Part III) Control Statements: Part II.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
1 CS161 Introduction to Computer Science Topic #8.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Exception Handling How to handle the runtime errors.
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
CS 1430: Programming in C++.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
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 3 Selection Statements
Chapter Topics The Basics of a C++ Program Data Types
Chapter 3 Control Statements
Data Types and Expressions
Data Types and Expressions
CS 1430: Programming in C++ No time to cover HiC.
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
2.1 Parts of a C++ Program.
Wednesday 09/23/13.
CS150 Introduction to Computer Science 1
2.6 The if/else Selection Structure
Chapter 2: Introduction to C++.
Subject:Object oriented programming
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

1 CS 1430: Programming in C++

2 Literal Values Literal values of int Literal values of float

3 Positive, Negative, and Zero Is 0 positive or negative? 0 is neither positive nor negative

4 Literal Values (II) Literal values of char ‘C’ ‘A’ ‘D’ Literal values of string “Enter an integer: ” “Your Grade is ” “A”

5 Variables // Variables can have initial values int num1, total = 0; // Variables can have different values cin >> num1; num1 = 58; total = total + num1;

6 Symbolic Constants // Symbolic constant const int MAX_SCORE = 60; // Must give a value // Store in memory // Can we do this? MAX_SCORE = 100; cin >> MAX_SCORE; // Error: constant cannot change its value

7 No Magic Numbers // Define symbolic constant const int MAX_SCORE = 60; int myScore; myScore = 60; // Is this good? // NO! // Magic number! // Half points off! myScore = MAX_SCORE; // Very good!

8 The Cast Functions int num1, num2; float quotient; cin >> num1 >> num2; //No input prompt! // 3 5 quotient = num1 / num2; // what’s the value of quotient? // How to get float quotient? quotient = float(num1) / num2; // Use cast function. // coercion quotient = float(num1 / num2); // Integer division or float division? // Integer division!

9 The IF statement int num1, num2; float quotient; cin >> num1 >> num2; quotient = float(num1) / num2; // Any possible issues? // What if num2 is zero? if (num2 != 0) quotient = float(num1) / num2;

10 The IF statement int num1, num2; float quotient;... if (num2 != 0) quotient = float(num1) / num2; // Semantics // do computation only when num2 is not zero // the statement is skipped when num2 is zero // Syntax // condition inside () // != for not equal // Style // on separate lines // indent 3 spaces

11 If-Else if (num2 == 0) cout << endl << "Cannot divide by zero!"; else { quotient = float(num1) / num2; cout << endl << "The quotient is " << quotient << ‘.’; } // Semantics // do different things when num2 is zero or not // Syntax // braces for multiple statements (statement block) // == for equal // Why use “==” instead of “=”? // Style // braces on separate lines

12 If-Else if (num2 == 0) { cout << endl << "Cannot divide by zero!"; } else { quotient = float(num1) / num2; cout << endl << "The quotient is " << quotient << ‘.’; } // Syntax // braces are optional for single statement

13 Statement Block if (num2 == 0) cout << endl << "Cannot divide by zero!"; else quotient = float(num1) / num2; cout << endl << "The quotient is " << quotient; // Will the program do output when num2 is 0? // YES! // Same as the following: if (num2 == 0) cout << endl << "Cannot divide by zero!"; else quotient = float(num1) / num2; cout << endl << "The quotient is " << quotient; // Indentation and blank line is a style issue // It does not make a block

14 If-Else if (num2 == 0) { cout << endl << "Cannot divide by zero!"; } else { quotient = float(num1) / num2; cout << endl << "The quotient is " << quotient << ‘.’; } // Syntax // braces are optional for blocks of single statement // braces are required for blocks of multiple statements

15 Comparison Operators == != > >= < <= No Space between!

16 Logical Operators && // And || // Or ! // Not

17 Comparison Operators != !> // NO! <= // Yes! ≤ // NO! <= // Yes!

18 Comparing characters const char UPPER_A = 'A'; char inputChar; cout << endl << "Enter a char: ”; cin >> inputChar; if (inputChar == UPPER_A) cout << endl << “It's an A!”;

19 Examples if (inputChar >= UPPER_A && inputChar <= 'Z') cout << endl // What should be the message? << "It's an upper case letter."; inputChar = ‘C'; inputChar = inputChar + 1; cout << endl << "The next char is " << inputChar; // The next char is D inputChar = ‘C'; cout << endl << "The next value is " << inputChar + 1; // The next value is 68 cout << endl << "The next char is " << char(inputChar + 1); // The next char is D

20 Multiple Conditions int num1, num2; cin >> num1 >> num2; if (num1 > 0 && num2 > 0) cout << “Both numbers are positive.” << endl; if (num1 > 0 || num2 > 0) cout << “At least one number is positive.” << endl; if (num1 && num2 == 0) cout << “\nBoth numbers are zero.”; // NO! if (num1 == 0 && num2 == 0) cout << “\nBoth numbers are zero.”; // Good! if (num1 = 0 && num2 = 0) cout << “\nBoth numbers are zero.”; // NO! // “=” is assignment!

21 Checking Input int score1, score2; Cout << “Enter two scores between 0 and 60, inclusive.”; cin >> score1 >> score2; if (score1 >= 0 && score1 = 0 && score2 <= 60) cout << “Both numbers are in the range.” << endl; // Magic numbers! const int MAX_SCORE = 60; const int MIN_SCORE = 0; if (score1 >= MIN_SCORE && <= MAX_SCORE && score2 >= MIN_SCORE && score2 <= MAX_SCORE) cout << “Both numbers are in the range.” << endl;

22 Example: Find the MAX of two numbers int num1, num2, max; cin >> num1 >> num2; if (num1 > num2) max = num1; else max = num2; cout << “The max value is ” << max << endl; // Another way if (num1 >= num2) max = num1; else max = num2; cout << “The max value is ” << max << endl;

23 Positive or Non-Positive int x = 10, y = 7; float z = -5.0; z = y / x; z = y / float(x); if (z > 0) cout << “Positive."; else cout << "Non-Positive."; // what is the output?

24 Even and Odd Numbers const int EVEN_NUMBER_DIVIDER = 2; int theNum; cout << "Enter an integer: "; cin >> theNum; // How do we know an integer is an even number? // The remainder operator %! if ((theNum % EVEN_NUMBER_DIVIDER) == 0) cout << theNum << " is an even number."; else cout << theNum << " is an odd number.";

25 Square Root Function float theNum; cout << "Enter a number: "; cin >> theNum; // How do we compute the square root of a number? // Call function sqrt()! if (theNum >= 0) cout << “The square root of ” << theNum << “ is ” << sqrt(theNum); else cout << theNum << " is a negative number."; // Need to include header file

26 Power Function #include using namespace std; int main() { float base, expo; cout << "Enter two numbers: "; cin >> base >> expo; // How do we compute base expo // Call function pow()! if (base > 0) cout << “Base ” << base << “ raised to power ” << expo << “ is ” << pow(base, expo); else cout << “Base is not positive."; return 0; }

27 Quiz point Submit to the Grader Differences: NONE Due 5 PM, today

28 Quiz point Submit to the Grader Differences: NONE Due 5 PM, Monday

29 Where Are the Materials? Labs/Progs K:\Courses\CSSE\CourseFiles\CS Notes/Quizzes Section Home Page: Your Scores Desire2Learn

30 Getting Help Student Helpers Come to me for help Name your file: UserName_Labx Drop your program in K:\Courses\CSSE\yangq\Dropbox