CS150 Introduction to Computer Science 1

Slides:



Advertisements
Similar presentations
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
Advertisements

1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Problem Solving and Program Design Programming. COMP102 Prog Fundamentals I : Problem Solving and Program Design/Slide 2 Problem Solving Process l Define.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Computer Science 1620 Arithmetic. C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data.
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.
CS150 Introduction to Computer Science 1
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS150 Introduction to Computer Science 1
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Correction of the Handout #include //Preprocessor using namespace std; int main (){ ………….. return 0; } A namespace is a named group of definitions. When.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Introduction to C++ Programming
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
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.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
First steps Jordi Cortadella Department of Computer Science.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Fundamental Programming Fundamental Programming Data Processing and Expressions.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Expressions.
Chapter 7: Expressions and Assignment Statements
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
What Actions Do We Have Part 1
Chapter 2 Introduction to C++ Programming
Expressions.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Computing Fundamentals
Basic Elements of C++.
Chapter 7: Expressions and Assignment Statements
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.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Basic Elements of C++ Chapter 2.
Arithmetic Operators in C
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
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
Arithmetic Operators in C
CS150 Introduction to Computer Science 1
Introduction to C++ Programming
Alternate Version of STARTING OUT WITH C++ 4th Edition
CS150 Introduction to Computer Science 1
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
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.
Chapter 2: Introduction to C++.
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
Life is Full of Alternatives
Arithmetic Operations
What Actions Do We Have Part 1
Life is Full of Alternatives
CS 101 First Exam Review.
Chapter 2: Overview of C++
Presentation transcript:

CS150 Introduction to Computer Science 1 Programs Write a program that reads in the user’s first and last names and prints out a greeting message Write a program that reads in last week’s and this week’s gas prices and prints out the difference 2/23/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 What’s the output? cout << “Enter two numbers: “; cin >> a >> b; a = a + 5.0; b = 3.0 * b; cout << “a = “ << a << endl; cout << “b = “ << b << endl; Assume 5.0 and 7.0 are entered for a & b 2/23/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 What’s the output? cout << “My name is: ”; cout << “Doe, Jane.” << endl; cout << “I live in “; cout << “Ann Arbor, MI “; cout << “and my zip code is “ << 48109 << “. “ << endl; How would we add a blank line between sentences? 2/23/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 What is the Output? Assume x = 2, y = 3 cout << x; cout << x + x; cout << “x=“; cout << x + y << “ = “ << y + x; z = x + y; cin >> x >> y; // cout << “x + y = “ << x + y; cout << “\n”; 2/23/2019 CS150 Introduction to Computer Science 1

General Form of a C++ Program // Programmer: John Doe // Instructor: Shereen Khoja // Date: Aug 30, 2003 // Purpose: converts distances from miles to // kilometers compiler directives using namespace std; int main() { declaration statements executable statements } 2/23/2019 CS150 Introduction to Computer Science 1

Arithmetic Expressions Arithmetic expressions manipulate numeric data We’ve seen simple ones We’ll learn all the rules for using expressions 2/23/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Arithmetic Operators + addition - subtraction * multiplication / division % remainder (modulus) 2/23/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Division The division operator can be used with both integers and floats If the operands are both floats, the result is a float Example: 7.0/2.0 is 3.5 If the operands are both ints, the result is an int Example: 7/2 is 3 If mixed, the int operand is converted to a float and the result is a float Example: 5/2.5 is 2.0 2/23/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Division Continued Divisor (second operand) cannot be 0 Division with negative integers may or may not be allowed 2/23/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Modulus % returns the integer remainder of integer division Both operands must be integers If second operand is negative, results will vary from system to system The value of m%n must be less than divisor n Examples 3%5 = 5%3 = 4%5 = 5%4 = 5%5 = 15%5 = 6%5 = 15%6 = 7%5 = 8%0 undefined 15%-7 system dependent 2/23/2019 CS150 Introduction to Computer Science 1

Assignment Statements and Expressions When assignment statement is executed, expression is evaluated and result is assigned to variable on left. Example: if a is a float a = 10; a = 10/3; What happens when types are mixed? 3.0 not 3.3333 2/23/2019 CS150 Introduction to Computer Science 1

Mixed-type assignments a is a float and n is an int N stores 14 not 13 2/23/2019 CS150 Introduction to Computer Science 1

Unary and Binary Operators Unary: One operand Unary + and - Example: x = -y; y = +x; Binary: Two operands Example: x = y+x; 2/23/2019 CS150 Introduction to Computer Science 1

Expressions with Multiple Operators Example: x = 5 + 3 * 2 - 1; What’s the value of x? There are rules for the order of evaluation so every computer will calculate the same expression the same way every time 2/23/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Order of Evaluation Anything in parentheses is evaluated first. Innermost first. Any with the same level are evaluated left to right. Operator precedence Unary + and - Operators *,/,% Binary +, - Binary operators evaluated left to right and unary right to left. 2/23/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example Put in parentheses to indicate order of evaluation x * y * z + a / b - c * d 2/23/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Program Design and write a program to calculate how much money your little sister has in nickels and pennies. 2/23/2019 CS150 Introduction to Computer Science 1