Fundamental Programming 310201 1 Fundamental Programming Data Processing and Expressions.

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

Types and Arithmetic Operators
True or false A variable of type char can hold the value 301. ( F )
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
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.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Chapter 2: Introduction to C++.
Introduction to C++ Programming
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Operaciones y Variables
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
CH Programming An introduction to programming concepts.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Fundamental Programming Fundamental Programming More on Selection.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
ORDER OF OPERATIONS x 2 Evaluate the following arithmetic expression: x 2 Each student interpreted the problem differently, resulting in.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 4 Selection Structures: Making Decisions.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Fundamental Programming: Fundamental Programming Introduction to C++
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Fundamental Programming Fundamental Programming for Loops.
Program Development C# Programming January 30, 2007 Professor J. Sciame.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 If’s – The Basic Idea “Program” for hubby: take out garbage.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
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.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Fundamental Programming Fundamental Programming Introduction to Functions.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
CSE202: Lecture 5The Ohio State University1 Selection Structures.
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.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 3 Selection Statements
Chapter 7: Expressions and Assignment Statements
Completing the Problem-Solving Process
WARM UP Page 9 “Check Skills You’ll Need” # 1 – 12.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 7: Expressions and Assignment Statements
Introduction to C++ Programming
Programming Funamental slides
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
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:
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Boolean Expressions to Make Comparisons
Arithmetic Operations
Fundamental Programming
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

Fundamental Programming Fundamental Programming Data Processing and Expressions

Fundamental Programming Status  this week we are starting to:  developing familiarity with the C++ language  developing skill in software development (in lab)  providing an introduction to some fundamental programming concepts  in the last class we started to look at some C++ syntax – the grammar rules  today we look at some fundamental concepts common to all programming languages

Fundamental Programming Review  a program performs a sequence of input, output and assignment statements  selection and repetition statements control which of the program’s input, output and assignment statements are performed  selection (if-then-else) statements provide alternate pathways through the program  repetition (while) statements control the number of times a block of statements is performed

Fundamental Programming Data Processing  we said that computers are data processing devices – they convert input data into output data  where does all the data processing occur in a program?  recall our sample program...

Fundamental Programming Sample Program write “Number of marks: “ read NbrMarks write “Student’s mark: “ read StudentMark set ProportionOfMarks to StudentMark / NbrMarks set PercentageOfMarks to ProportionOfMarks * 100 write “ Student’s percentage: “ write PercentageOfMarks  in this program, all the data processing occurs in assignment statements

Fundamental Programming Anatomy of an Assignment  let’s analyse the assignment statement…  here, the two assignment statements are: set ProportionOfMarks to StudentMark / NbrMarks set PercentageOfMarks to ProportionOfMarks * 100  that is: set to  here, the values of interest are: StudentMark / NbrMarks ProportionOfMarks * 100

Fundamental Programming Expressions  we use the term expression to mean:  the description of a value of interest  we describe the value that we wish to assign to a data object in an expression  so: StudentMark / NbrMarks ProportionOfMarks * 100  are two expressions

Fundamental Programming Data Processing  so, where does the data processing happen?  answer: some of it happens in assignment statements  it can also happen in output statements…

Fundamental Programming Alternative Design write “Number of marks: “ read NbrMarks write “Student’s mark: “ read StudentMark set ProportionOfMarks to StudentMark / NbrMarks write “ Percentage: “ write ProportionOfMarks * 100

Fundamental Programming Anatomy of an Output  the anatomy of our assignment statement is: set to  the anatomy of our output statement is: write  so, where does all the data processing happen? Expressions !

Fundamental Programming Expressions  clearly, expressions are important - that’s where the data processing happens  let’s take a closer look at expressions  previously, we said that data was numbers and text -for now, we just deal with expressions to process numbers  the anatomy of an expression is one we’ve seen before...

Fundamental Programming Expressions as a Black Box  we can think of an expression as a black box  expressions have one or more input values and produce one output value - the input- process-output model again  example: StudentMark / NbrMarks input process output StudentMark ? NbrMarks (a single value - depends on inputs)

Fundamental Programming Operators  we use the term operator to mean:  a symbol, or name, used to represent an operation that can be performed on data  in the two expressions: StudentMark / NbrMarks ProportionOfMarks * 100  the operators are:  / for division  * for multiplication  + and - are used for addition and subtraction  +, -, *, / all work in C++ as you would expect

Fundamental Programming Operands  we use the term operand to mean:  an input to an expression  in the two expressions: StudentMark / NbrMarks ProportionOfMarks * 100  the operands are:  StudentMark and NbrMarks  ProportionOfMarks and 100

Fundamental Programming Binary Operators  in the following examples: StudentMark / NbrMarks ProportionOfMarks * 100 NbrMarks - StudentMark StudentMark + 10  each operator is used with two operands  so /, *, - and + are binary operators – they can all be used with two operands

Fundamental Programming Unary Operators  the + and - operators are also unary operators (they can be used with just one operand)  examples: as in set AbsoluteZero to as in set BoilingPointOfWater to +100 expression operand operator

Fundamental Programming Numeric Expressions  expressions that evaluate to a number are called numeric expressions  numeric expression come in all shapes and sizes:  a number by itself – a literal: set NbrTimesTold to 0  the name of a variable: write Percentage  expressions that use operators: set NbrTimesTold to NbrTimesTold + 1

Fundamental Programming Power of Expressions  the arithmetic operators +, -, * and / give us a powerful language to process numbers  the power comes from the ability to nest little expressions inside bigger expressions  instead of: set ProportionOfMarks to StudentMark / NbrMarks write ProportionOfMarks * 100  we can write: write StudentMark / NbrMarks * 100  question: which operator is applied first here? and, does it matter?

Fundamental Programming Nested Expressions  which operator is applied first here?  is the division first? StudentMark / NbrMarks * 100 divide StudentMark by NbrMarks, then multiply by 100  or is the multiplication first? StudentMark / NbrMarks * 100 multiply NbrMarks by 100, then divide StudentMark by result of multiplication  Activity: does it matter?

Fundamental Programming Activity Break

Fundamental Programming Activity Feedback  using StudentMark = 50, NbrMarks = 100…  division first: (StudentMark / NbrMarks) * 100 =(50 / 100) * 100 = 50  multiplication first: StudentMark / (NbrMarks * 100) =50 / (100 * 100) =  will a C++ program do it in the correct order?

Fundamental Programming Order of Use  there are rules to decide the order in which operators in an expression are applied  unary operators before binary operators  multiplication (*) and division (/) before addition (+) and subtraction (-)  otherwise, left to right  evaluate the following: 4 * / 4 * 3  will the following be evaluated correctly? StudentMark / NbrMarks * 100

Fundamental Programming Activity Break

Fundamental Programming Activity Feedback  evaluate: 4 *  unary operator first (- applies to 2)  * multiplication before addition ( 4 * -2 ) + 3 = = -5

Fundamental Programming Activity Feedback  evaluate the following: / 4 * 3  multiplication and division before addition  left to right otherwise – so division before multiplication here 2 + ( 12 / 4 ) * 3 =2 + 3 * 3  multiplication before addition =2 + ( 3 * 3 ) =2 + 9 = 11

Fundamental Programming Activity Feedback  will the following be evaluated correctly? StudentMark / NbrMarks * 100  yes it will – since the division occurs before the multiplication, this is the same as: (StudentMark / NbrMarks) * 100

Fundamental Programming Order of Use  avoid errors by using parentheses: ( 4 * -2 ) ( ( 12 / 4 ) * 3 )  sometimes you can rewrite an expression to make it easier to read – instead of: StudentMark / NbrMarks * 100  you can write: 100 * StudentMark / NbrMarks  is this easier to understand? if so, why?

Fundamental Programming Activity Break

Fundamental Programming Activity Feedback  the expression: 100 * StudentMark / NbrMarks  may seem easier to read than: StudentMark / NbrMarks * 100  possibly because, in the first expression above, the order in which operators are applied doesn’t matter – left for student to check  always keep you code as simple as possible

Fundamental Programming Activity  the following program is designed to convert temperatures between Fahrenheit and Centigrade  it has a logic error – fix it…

Fundamental Programming #include using namespace std; int main (void) { int ConversionType = 0; float Temperature = 0; cout "; cin >> ConversionType; cout "; cin >> Temperature; if (ConversionType == 1) { cout << 32 + Temperature * 1.8; cout << " degrees Fahrenheit"; } else { cout << Temperature - 32 / 1.8; cout << " degrees Centigrade"; } getchar(); return 0; }

Fundamental Programming Activity Break

Fundamental Programming #include using namespace std; int main (void) { int ConversionType = 0; float Temperature = 0; cout "; cin >> ConversionType; cout "; cin >> Temperature; if (ConversionType == 1) { cout << 32 + Temperature * 1.8; cout << " degrees Fahrenheit"; } else { cout << Temperature - 32 / 1.8; cout << " degrees Centigrade"; } getchar(); return 0; } problem here: division occurs before subtraction Feedback

Fundamental Programming #include void main (void) { int ConversionType = 0; float Temperature = 0; cout "; cin >> ConversionType; cout "; cin >> Temperature; if (ConversionType == 1) { cout << 32 + (Temperature * 1.8); cout << " degrees Fahrenheit"; } else { cout << (Temperature – 32) / 1.8; cout << " degrees Centigrade"; } a solution: enclose subtraction in parentheses clarification: parentheses make intention clear Feedback

Fundamental Programming C++ Syntax Summary  input : cin >> ;  output : cout ;  assignment : = ;  a selection statement: if ( ) { } else { }  a repetition statement: while ( ) { }

Fundamental Programming Summary  data processing happens in expressions  expressions appear in assignment and output statements  different types of expressions – literals, variables names, ones that use operators…  arithmetic operators are: +, -, *, /  rules control order of application  parentheses are used to impose ordering  computing has a lot of jargon!