Arithmetic Operations

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
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.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
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/17/07CS150 Introduction to Computer Science 1 Type Casting.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
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.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
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 8/31/05CS150 Introduction to Computer Science 1 Hello World!
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Doing math In java.
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++
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
CS 31 Discussion, Week 2 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Bill Tucker Austin Community College COSC 1315
Introduction to Computer Programming
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1: Introduction to computers and C++ Programming
What Actions Do We Have Part 1
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Introduction to C++ Programming Language
Chapter 2 - Introduction to C Programming
for Repetition Structures
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Variables Kingdom of Saudi Arabia
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Summary Two basic concepts: variables and assignments Basic types:
Alternate Version of STARTING OUT WITH C++ 4th Edition
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
CS150 Introduction to Computer Science 1
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
Let’s all Repeat Together
What Actions Do We Have Part 1
Life is Full of Alternatives
Capitolo 1 – Introduction C++ Programming
Chapter 2 - Introduction to C Programming
Reading from and Writing to Files
Life is Full of Alternatives
Introduction to C Programming
Chapter 2, Part III Arithmetic Operators and Decision Making
Reading from and Writing to Files
Presentation transcript:

Arithmetic Operations 9/12/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Today Last time we covered Input statements Arithmetic statements + - * / Today we will cover Modulus operations % Operator precedence Operator associativity string data type 9/12/05 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 Examples 3%5 = 5%3 = 4%5 = 5%4 = 5%5 = 15%5 = 6%5 = 15%6 = 7%5 = 8%0 = 15%-7 = 9/12/05 CS150 Introduction to Computer Science 1

Arithmetic Operations Arithmetic operations in C++ must be entered in straight-line form Algebraic notation is not accepted by the compiler Is not acceptable Instead, you should use: x = 3 / 4 * 2; If we wanted to evaluate , then we would use parenthesis x = 3 / (4 * 2); 9/12/05 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 9/12/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Unary Operators The examples that we have seen so far all use binary operators i.e. they take two operands, one on the left of the operator and one on the right of the operator C++ also supports unary operators i.e. operators that take one operand to the right of the operator Positive and negative are examples of unary operators x = -5; y = -x; 9/12/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Operator Precedence Anything in parentheses is evaluated first Innermost first Operator precedence Parenthesis () Unary operators +, - Binary operators *,/,% Binary operators+, - Why is operator precedence important? 9/12/05 CS150 Introduction to Computer Science 1

Operator Associativity Operator associativity refers to the order in which expressions of the same level are evaluated Binary operators are evaluated left to right Unary operators are evaluated right to left Give a numerical example that illustrates the importance of operator associativity 9/12/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Example In what order is the following expression evaluated? Num = x * y * z + a / b - c * d What is the value of num if X = 2, y=3, z=2, a=4, b=2, c=5, d=2 9/12/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem How would you write the expression y=x^3 + 7 in C++? What is the value of x in the following expressions, assume x is a double X = 5.0 * 3.0 / 2.0; X = 3.0 + 2.0 - 5.0; 9/12/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 string Data Type Before we move on, I want to introduce you to a useful data type This is not a primitive data type, but you can use it by including a C++ library as a preprocessor directive The library you need to include is #include <string> The string data type is used to create variables that can hold multiple characters string name = “shereen”; 9/12/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 string Data Type #include “stdafx.h” #include <iostream> #include <string> using namespace std; int main() { string name; cout << “what is your name?”; cin >> name; cout << “Hello “ << name <<endl; return 0; } 9/12/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Problem Write a C++ program that allows the user the ability to enter their name and the number of nickels and pennies they have. You are then to print the number of dollars and change that corresponds to 9/12/05 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Summary In today’s lecture we learnt How to write complex arithmetic expressions About operator precedence and operator associativity About the modulus operator About string variables We have covered p. 31 - 34 of your textbook 9/12/05 CS150 Introduction to Computer Science 1