Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: 2.7-3.4, 8.11 Homework #3.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Overview creating your own functions calling your own functions.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
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.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Programming is instructing a computer to perform a task for you with the help of a programming language.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Introduction to Python
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Agenda Review Unix Review Algorithms Your first program Compiling programs What are functions? What is Object Oriented Programming? Variables Data Types.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
Functions Why we use functions C library functions Creating our own functions.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Variables and Expressions CMSC 201 Chang (rev )
C++ Programming: Basic Elements of C++.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
1 C++ Programming Basics Chapter 1 Lecture CSIS 10A.
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)
Unit 3 Lesson 11 Passing Data and Using Library Functions Textbook Authors: Knowlton, Barksdale, Turner, & Collings PowerPoint Lecture by Dave Clausen.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Expressions and Interactivity. 3.1 The cin Object.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
LESSON 2 Basic of C++.
Completing the Problem-Solving Process
Computing Fundamentals
Variables, Expressions, and IO
Introduction to C++ October 2, 2017.
Introduction to C++ Programming
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
CS150 Introduction to Computer Science 1
Elementary Programming (C++)
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
Summary of what we learned yesterday
C++ Programming Basics
Unit 3: Variables in Java
Data Types and Maths Programming Guides.
Presentation transcript:

Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: , 8.11 Homework #3

Review: Compiling C++ What is a.cpp file? C++ Source Code What is source code? Human readable instructions for the computer to follow. What is a.o file? A binary object, or executable, of the program you wrote source code for. What is a compiler? A compiler translates source code into machine code (binary object, executable) that the computer can understand and execute. How do you compile a C++ program? Use the g++ command: g++ SourceCode.cpp -o Executable.o

Review: Data Types What is the difference between declaring and assigning a variable? How do you declare more than one variable at a time (without assigning a value)?

Review: Data Types What is a... Character? Integer? Double? Float? String? Boolean?

Review: Data Types What is a... Character? A single letter, number, or symbol Integer? A whole number, positive or negative Double? A number with a decimal (large) Float? A number with a decimal (smaller than a double) String? A series of characters Boolean? True or False, 1 or 0

Review: Data Types How do you declare and assign a: Character? char someCharacter = 'A'; Integer? int someInteger = 15; Double? double someDouble = 1.25; Boolean? bool thisIsTrue = true; String? string someString = “This is my string”;

Integer Division Division can be tricky due to different data types. If you divide two integers, the result is an integer. 2/3 = 0 3/2 = 1 3/3 = 1 double quotient = 2/3; quotient will be 0.00 double quotient = 2.0/3.0; quotient will be 0.66 Code Example: IntegerDivision.cpp

What is wrong with this? int numHomeworks = 3; int maxHwGrade = 25; int hw1 = 18; int hw2 = 23; int hw3 = 15; int avg = (hw1 + hw2 + hw3) / (numHomeworks * maxHwGrade) * 100; Code Example: HomeworkGradeDiv.cpp

Grade is 0? This is what happens when you use integer division: (hw1 + hw2 + hw3) / (numHomeworks * maxHwGrade) * 100 ( ) / (3 * 25) * 100 ( ) / (75) * 100 (56) / (75) * * If we change the dividend or divisor to a double: (hw1 + hw2 + hw3) / (numHomeworks * maxHwGrade) * 100 ( ) / (3.0 * 25) * 100 ( ) / (75.0) * 100 (56) / (75.0) * * Then if we stick into our integer avg, it will cut off the decimals and give us 74. Code Example: HomeworkGradeDivFixed.cpp

Order of operations C++ uses the same order of operations you would expect from any mathematical equation. Division and Multiplcation first, left to right Followed by Addition and Subtraction, left to right You can also use parenthesis, just as you do in math.

Composition In a program, you can replace any value or expression with another expression. Let's say you have declared and assigned the following variables: int a = 10; int b = 5; int sum = a + b; int multiplier = 2; Now, you can calculate the total (some arbitrary equation) in any of these ways: int total = (10 + 5) * 2; int total = (a + b) * multiplier; int total = sum * multiplier;

Functions Recall our discussion on functions. If you have a square root function called sqrt() that accepts one parameter (an integer) and returns an integer, you write the definition like this: int sqrt (int num); Function definitions are what you look at to remind yourself what data types and parameters the function requires and what data type it returns. If you look up C++ functions online, you will see function definitions. To actually use the function, you type: int root = sqrt(94786);

Functions What about a function called multiplyThree that accepts 3 parameters, all integers, and returns the product of all 3? Definition: int multiplyThree (int num1, int num2, int num3); Use the function: int product = multiplyThree(94786, 3, 12);

math.h library Some extra math functions are included in the math.h header. To use them, we put #include at the top of our source code. log log10 sin cos acos You can look up more functions online: Learn to read formal documentation – it will help you. Code Example: MathFunctions.cpp, Math.cpp

ctype library The ctype library has functions for determining if a character meets certain type conditions: isalnum isalpha isdigit islower isupper You can look up more functions online: Code Example: CTypeFunctions.cpp

Practice Write a program that... 1) Calculates the hypotenuse of a right triangle with sides of length 9 and 5 (pythagorean theorem) 2) Converts a Celsius temperature to Fahrenheit (F = 9/5 C + 32) 3) Calculates the volume of a sphere (V = 4/3 pi r 3 ) 4) Converts kilometers to miles (miles = kilometers * )

What is User Input? - Keyboard input - Mouse movement - Finger movement (touch screen) - Voice (speech recognition, think Siri) - Joystick / Game controller *For this class, we use only keyboard

Backspace ^? ^H what?? Before we start writing programs with user input, we need to fix something. In UNIX, depending on your terminal settings, pressing the backspace key may result in funny characters. It's easy enough to change these settings: nano ~/.login.user Type in: stty erase ^H stty erase ^\? Save the file. Close your terminal and open a new one.

cin The iostream header file provides cout and cin (among others). We know cout handles output to the screen. cout << “Hello!”; cin handles user input from the keyboard string firstName; cout << “What is your first name?”; cin >> firstName; cout << “Hello, “ << firstName << “!”;

cin / cout operators Note that cout uses the << operator cin uses the >> operator I like to think of them as pointing to where the input/output is going to. Either the screen (cout > myVar). Examples: CinNumbersExample.cpp, CinGreeting.cpp

Input Validation What if a user types in an alphabetical character when I ask for a number? The value that ends up in your variable will be junk. The program will not error, but you will not get your desired result. For now, we won't worry about this. We will assume that the user always types in the correct value.

User Input: Strings This program will not work as expected: int main () { cout << “What is your full name?”; string fullName; cin >> fullName; cout << “Hello, “ << fullName << “!” << endl; return 0; } Example: CinString.cpp

User Input: Strings cin stops recording user input as soon as a space or new line (enter) is encountered. This program will only print the first name to the screen. Instead, we can use a function called getline() from the header file string. getline(cin, fullName); Example: GetLineString.cpp

User Input: Strings So why don't we use getline() all the time? getline() will only save the input as a string. cin will determine, based on the variable you are putting the input into, what data type to use. If you want a user to input a number, use cin. If you want a user to input a string, use getline().

Practice 1) Add user input to the programs we did earlier 2) Write a program that asks the user 3 questions (of your choice). Then, print those questions with their answers back to the screen. Example: What is your name? Jenn What is your favorite color? Green What is your favorite season? Fall Jenn's favorite color is Green and favorite season is Fall 2) Write a program that calculates the surface area of a sphere. The user provides the radius. 3) Ask the user to type in a keyboard character. Print to the screen the results of the functions that will tell them if it is a letter, if it is a number, and if it is lowercase.

Practice 4) Ask the user for hours and minutes. Print out the total number of minutes. 5) Do the reverse of #4 6) Ask the user for the number of hours they worked and what their hourly wage is. Tell them how much money they have made. Later, when we learn conditionals, we can consider overtime.

HW #3 Posted online