More C++ Basics October 4, 2017.

Slides:



Advertisements
Similar presentations
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
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.
CMT Programming Software Applications
PHP : Hypertext Preprocessor
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Week 1 Algorithmization and Programming Languages.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Chapter 2 Variables.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
COMP 2710 Software Construction C++ Basics 2 and Exercises Dr. Xiao Qin Auburn University These slides.
Constants, Data Types and Variables
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
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
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Bill Tucker Austin Community College COSC 1315
Numbers in ‘C’ Two general categories: Integers Floats
CS 106A, Lecture 4 Introduction to Java
Chapter 2 Variables.
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
Introduction to Java Applications
Data Types and Expressions
Chapter 2 C++ Basics. Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow.
Chapter 2 C++ Basics.
Data Types and Expressions
Variables Mr. Crone.
Chapter 7 – Arrays.
PowerPoint Presentation Authors of Exposure Java
Announcements Final Exam will be on August 19 at 16:00
Documentation Need to have documentation in all programs
Basic Elements of C++.
Multiple variables can be created in one declaration
Introduction to Scripting
Introduction to C++ October 2, 2017.
Basics (Variables, Assignments, I/O)
Basic Elements of C++ Chapter 2.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Announcements Midterm2 Grades to be announced NEXT Monday
Chapter 2 - Introduction to Java Applications
Chapter 2 Variables.
Data Types, Concatenation, PEMDAS, Shortcuts, and Comments
Introduction to C++ Programming
Chapter 3 – Introduction to C# Programming
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
JavaScript Part 2.
Expressions and Assignment
elementary programming
Chapter 2: Introduction to C++.
Introduction to Java Applications
WEEK-2.
Summary of what we learned yesterday
EECE.2160 ECE Application Programming
Other types of variables
Unit 3: Variables in Java
Chapter 2 Variables.
Lexical Elements & Operators
An Introduction to Programming
EECE.2160 ECE Application Programming
Presentation transcript:

More C++ Basics October 4, 2017

Division Recall from last time that division is more nuanced than the other arithmetic operations. The / command refers to integer division: how many times does one number go into another, ignoring the remainder? 10 / 3 == 3 5 / 2 == 2 20 / 3 == 6 You do not need to include spaces between the numbers—this is done to make the code easier to read.

Division To include the decimal part with the quotient, you MUST include a decimal part with at least one of the original numbers. 5 / 2 == 2 5.0 / 2 == 2.5 5 / 2.0 == 2.5 5.0 / 2.0 == 2.5

Division Pitfalls Consider the following: double a = 5, b = 2; cout << a / b; Be careful!!! The CS50 IDE will output 2.5, but some compilers (e.g., NetBeans) will output 2. Best to use the following declaration and initialization: double a = 5.0, b = 2.0;

Division Pitfalls Consider the following code: double c = 20; double f; f = (9 / 5) * c + 32.0; What value is assigned to f? How can this error be corrected?

Decimal Numbers Sometimes it is helpful to use a certain number of digits after the decimal place—for instance, to always have two when dealing with dollar amounts. The following three lines of code will set all variables of type double to display with exactly two decimal places: cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);

Characters A variable of type char (character) is a single symbol such as a letter, digit, or punctuation mark. A variable of type char is always contained inside of single quotes: char letter = 'A'; 'A' and "A" are not equivalent! "A" is a string, not a character. A blank space is considered a character: ' '.

Strings A string is a series of one or more text characters. string day = "Monday"; In order to use the string class, you must include the string library: #include <string>

Strings The + symbol can be used to concatenate strings (i.e., put two or more strings together). Consider: string day, day1, day2; day1 = "Monday"; day2 = "Tuesday"; day = day1 + day2; This results in the concatenated string MondayTuesday. Spaces must be explicitly added: day = day1 + " " + day2;

If-Else Statements Consider the following scenario: An employee receives his/her regular hourly salary for the first 40 hours worked in a week. If the employee works beyond 40 hours, (s)he receives 1.5 times the regular rate per hour. If the employee works 40 or more hours, we can model this using the following formula: (rate * 40) + (1.5 * rate * (hours – 40)) Note that if the employee works fewer than 40 hours, (s)he will receive a negative paycheck! In this case, we need the formula rate * hours.

If-Else Statements We need the program to do the following: Determine whether hours > 40. If hours > 40, use the following formula: gross_pay = (rate * 40) + (1.5 * rate * (hours – 40)); If not, use the following formula: gross_pay = rate * hours; We can accomplish this using an if-else statement.

If-Else Statements if(hours > 40) { gross_pay = (rate * 40) + (1.5 * rate * (hours – 40)); } else { gross_pay = rate * hours;