CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner CP1020 - Week 3 zArithmetic operations zOperator precedence  Simple QBasic functions.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 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
CP Week 10 Modularising programs using Procedures.
CP Week 2 zReserved words zVariables zInput and output zData types zTesting and Documentation.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
CP1020 Week 5 Selection Continued. CP1020 University of Wolverhampton - Steve Garner and Ian Coulson if then else zWe can use if then else statements.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CP1020 University of Wolverhampton - Steve Garner and Ian Coulson1 Week 1 - Principles of programming Welcome from the Presenters Steve Garner and Dr Ian.
Introduction to C Programming
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Basic Input/Output and Variables Ethan Cerami New York
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Spreadsheets Objective 6.02
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Eva Math Team 6 th -7 th Grade Math Pre-Algebra. 1. Relate and apply concepts associated with integers (real number line, additive inverse, absolute value,
E0001 Computers in Engineering Procedures: subprograms and functions.
7-1 Chapter 7.  Basic Arithmetic Verbs  Options Available with Arithmetic Verbs  COMPUTE Statement  Signed Numbers in Arithmetic Operations  Intrinsic.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
STROUD Worked examples and exercises are in the text 1 STROUD Worked examples and exercises are in the text Programme F2: Introduction to algebra PROGRAMME.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
COMPUTER LITERACY NOTES MS-EXCEL. SPREADSHEETS A spreadsheet is a computer equivalent of a paper ledger sheet. Excel allows you to create spreadsheets.
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.
Math operations 9/19/16.
Lec 3: Data Representation
Matrices Rules & Operations.
Chapter 2 Introduction to C++ Programming
Lecture 4: Expressions and Variables
Building Java Programs
Chapter 2 - Introduction to C Programming
Arithmetic operations & assignment statement
Chapter 2 - Introduction to C Programming
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Building Java Programs Chapter 2
Introduction to C++ Programming
Numbers.
Building Java Programs
CSI 101 Elements of Computing Spring 2009
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Building Java Programs Chapter 2
Spreadsheets 2 Explain advanced spreadsheet concepts and functions
Algorithms computer as the tool process – algorithm
Engineering Problem Solving with C++ An Object Based Approach
Building Java Programs
Building Java Programs
Lecture 4: Expressions and Variables
Spreadsheets Objective 6.02
Building Java Programs
Building Java Programs Chapter 2
Spreadsheets Objective 6.02
Building Java Programs
Introduction to Python
Presentation transcript:

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner CP Week 3 zArithmetic operations zOperator precedence  Simple QBasic functions zArranging output on the screen

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Arithmetic Operations zFundamental to computing as most computer programmes consist of either mathematics or manipulating text  We shall look at the operations available and the rules that govern their use

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Common Mathematical operations Arithmetic operationSymbol Example Exponentiation^ 2^3 = 8 (2*2*2 or 2 3 ) Mult. & division*, / 2/4 = 0.5 Addition & subtraction+, - 2+2=4

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Operator precedence What does this mean ? Which mathematical operation will take place first! E.g take the sum: iMyAnswer = / 4 could give the result iMyAnswer = 1.25 if we add then divide by 4 or = 2.75 if divide 3 by 4 then add it to 2 This obviously makes a big difference to the result

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner The order of calculations QBasic has a fixed set of rules on the order of how these operations are performed. Here is a simplified version Order Operation Symbol 1 BBrackets( ) 2 OExponentiation ^ 3 DMDivision/ Multiplication /,* 4 AS Addition/Subtraction+, - operations on same level performed left to right

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Operator precedence in action iMyAnswer = 6 / * / 6 Working left to right 6 / 2 evaluated first > 3 8 * 2 evaluated second > / 6 evaluated third > 2 then QBasic will work left to right to perform the next order of operations : that is so iMyAnswer = 15

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Examples iAnswer1 = 4^2 + 6 / iAnswer1 = = 16 iAnswer2 = 35 / / * 2 iAnswer2 = = 13 iAnswer3 = * 2 * 2 ^ 3 iAnswer3 = * 2 * 8 iAnswer3 = = -75

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Use of Brackets Brackets force the machine to perform the calculations in the order we want:- e.g iMyAns = ((1 + 4) / (7 - 2)) + ((16 + 4) * (3 - 2)) calculations in the inner most brackets are performed first - (1 + 4).. (7 - 2)..(16 + 4).. (3 - 2) the results of these are used in the next level of brackets (5 / 5).. (20 * 1) finally the next level is calculated = 21

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Some Concepts and Jargon KEY or RESERVED WORDS - these are words or groups of letters that are used by QBASIC to perform certain mathematical operations, procedures etc. And so can’t be used as variable names e.g. PRINT, CLS STATEMENTS -usually a line of code, for instance PRINT “Principles of Programming”

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner QBasic standard functions A function has the following format Answer function name value to be worked on It is easiest to explain what a QBasic standard function is by the use of an example:- iArea= 9 iSide = SQR(iArea) PRINT iSide Here we are working out the square root of 9 Variable = Function(variable)

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner QBasic Standard Functions The steps are :- iSide = SQR(iArea) 3) the returning value is assigned to variable iSide 2) the function somehow determines the answer 1) pass the number into the function using variable iArea

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner A selection of other functions available QBasic has a range of other available functions that are coded in a similar manner :- Functionreturnsexample INT(X) largest integer <= to x INT(9.13) gives 9 ABS(X) absolute value of X ABS(-11) gives 11 LEN(string)number of characterssMyString = “abc” in a string LEN(sMyString) gives 3

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner An example program REM A program to print the length of string REM Ian Coulson REM 24th September 1999 DIM sExample AS STRING DIM iStringLength AS INTEGER LET sExample = “abc” LET iStringLength = LEN(sExample) PRINT “The string abc has “; iStringLength; “ characters” END Statement Reserved word Function

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Printing on the screen zQbasic divides the screen into 80 columns and 25 rows. 25 rows 80 columns zQbasic has a number of mechanisms to arrange where on the screen you wish to print information zHave a look at the following examples

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Print formatting - using the TAB function PRINT TAB(20); “This is the first thing printed ” PRINT “Next thing printed” This is the first thing printed Next thing printed Start printing at a particular column - in this case the 20th

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Print formatting - the SPC function PRINT“First thing printed”; SPC(10);“Next thing printed” First thing printed Next thing printed Start printing 10 spaces after the last thing printed

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Print formatting - the locate statement LOCATE 10, 15 PRINT “This is printed here” this moves the cursor to start printing at the specified row and column number rowcolumn This is printed here Row 10 Column 15

CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Review questions 1) Using Qbasic rules of operator precedence determine what the following equates to 3 ^ * 2 ^ 2 2) Write a statement (a line of code) to obtain the square root of a value stored in fNumber, and store it in another variable called fRoot.