1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
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.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS150 Introduction to Computer Science 1
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CS150 Introduction to Computer Science 1
Introduction to C Programming
Introduction to C++ Programming
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 01: Introduction to Computer Programming
Introduction to C++ Programming
COMPUTER SCIENCE I C++ INTRODUCTION
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
First steps Jordi Cortadella Department of Computer Science.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
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++
Chapter 02 (Part II) Introduction to C++ Programming.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 2 of C++ How to Program, 10/e © 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
Computing Fundamentals
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Variables Kingdom of Saudi Arabia
Programming Funamental slides
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
CS150 Introduction to Computer Science 1
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
Capitolo 1 – Introduction C++ Programming
Life is Full of Alternatives
Presentation transcript:

1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2

2 09/10/04CS150 Introduction to Computer Science 1 Today  Last time we learnt about two types of executable statements o Assignment statements o Input/output statements  Today we will o Cover arithmetic statements

3 09/10/04CS150 Introduction to Computer Science 1 Arithmetic Expressions  Arithmetic expressions manipulate numeric data  We’ve already seen simple ones  The main arithmetic operators are o +addition o -subtraction o *multiplication o /division o %modulus

4 09/10/04CS150 Introduction to Computer Science 1 +, -, and *  Addition, subtraction, and multiplication behave in C++ in the same way that they behave in algebra int num1, num2, num3, num4, sum, mul; num1 = 3; num2 = 5; num3 = 2; num4 = 6; sum = num1 + num2; mul = num3 * num4;

5 09/10/04CS150 Introduction to Computer Science 1 Division /  The division operator can be used with both integers and doubles  If the operands are both doubles, the result is a double o Example: 7.0/2.0 is 3.5  If the operands are both int s, the result is an int o Any fractional part in integer division is discarded o Example: 7/2 is 3  If mixed, the int operand is converted to a double and the result is a double o Example: 5/2.5 is 2.0

6 09/10/04CS150 Introduction to Computer Science 1 Division  Divisor (second operand) cannot be 0  Division with negative integers may or may not be allowed o It depends on the compiler

7 09/10/04CS150 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 =

8 09/10/04CS150 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 o Instead, you should use: x = 3 / 4 * 2;  If we wanted to evaluate, then we would use parenthesis o x = 3 / (4 * 2);

9 09/10/04CS150 Introduction to Computer Science 1 Expressions with Multiple Operators  Example: x = * 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

10 09/10/04CS150 Introduction to Computer Science 1 Unary Operators  The examples that we have seen so far all use binary operators o 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 o i.e. operators that take one operand to the right of the operator  Positive and negative are examples of unary operators o x = -5; o y = -x;

11 09/10/04CS150 Introduction to Computer Science 1 Operator Precedence  Anything in parentheses is evaluated first o Innermost first  Operator precedence o Parenthesis () o Unary operators +, - o Binary operators *,/,% o Binary operators+, -  Why is operator precedence important?

12 09/10/04CS150 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

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

14 09/10/04CS150 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 o X = 5.0 * 3.0 / 2.0; o X = ;

15 09/10/04CS150 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 o The library you need to include is #include  The string data type is used to create variables that can hold multiple characters  string name = “shereen”;

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

17 09/10/04CS150 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

18 09/10/04CS150 Introduction to Computer Science 1 Summary  In today’s lecture we learnt o How to write complex arithmetic expressions o About operator precedence and operator associativity o About the modulus operator o About string variables  We have covered p of your textbook