Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations. Practical 1.

Similar documents
Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

Summary of Lecture 4. Computer Programming: Skills & Concepts (INF-1-CP1) Variables; scanf; Conditional Execution. Tutorials.

COMP 208 Computers in Engineering

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Expressions. Eric McCreath

C Programming

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

CS111: PROGRAMMING LANGUAGE II

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS DEPARTMENT OF SCIENCE AND HUMANITIES EVEN SEMESTER FEB 2017

Programming Language A

Data Types and Variables in C language

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Computers in Engineering. Moving From Fortran to C Part 2 Michael A. Hawker

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

Computers in Engineering COMP 208. Roots of a Quadratic in C 10/25/2007. Moving From Fortran to C Part 2 Michael A. Hawker

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

C++ Programming: From Problem Analysis to Program Design, Third Edition

Operators and Expressions:

Fundamentals of Programming CS-110. Lecture 3

Getting started in C

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

sends the formatted data to the standard output stream (stdout) int printf ( format_string, argument_1, argument_2,... ) ;

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Computer Science & Engineering 150A Problem Solving Using Computers

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

CS313D: ADVANCED PROGRAMMING LANGUAGE

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

16.216: ECE Application Programming Fall 2013

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

EECE.2160: ECE Application Programming Spring 2018

Goals of this Lecture

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Data types, variables, constants

The Further Mathematics Support Programme

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

CIS133J. Working with Numbers in Java

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Informatica e Sistemi in Tempo Reale

Special PRG Lecture No. 2. Professor David Brailsford Special PRG Lecture: Arrays

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

ECE 122 Engineering Problem Solving with Java

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

At the end of this lecture you should be able to have a basic overview of fundamental structures in C and be ready to go into details.

CITS 1401 Problem Solving & Programming

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

2. Numbers In, Numbers Out

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Floating-point numbers. Phys 420/580 Lecture 6

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

n Group of statements that are executed repeatedly while some condition remains true

Engineering 12 - Spring, 1999

CSCI 171 Chapter Outlines

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

Iosif Ignat, Marius Joldoș Laboratory Guide 4. Statements. STATEMENTS in C

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Information Science 1

Computer Programming

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Fundamentals of Programming

Structures, Operators

Flow Chart. The diagrammatic representation shows a solution to a given problem.

Chapter 4: Basic C Operators

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Continued from previous lecture

HW DUE Floating point

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Basic Assignment and Arithmetic Operators

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

2. Numbers In, Numbers Out

ANSI C Programming Simple Programs

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

H192 Midterm 1 Review. Tom Zajdel

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities

for Loop Lesson 2 Outline

GCSE-AS Mathematics Bridging Course. Chellaston School. Dr P. Leary (KS5 Coordinator) Monday Objectives. The Equation of a Line.

Number Theory and Proof Methods

Computer Programming: Skills & Concepts (CP) Variables and ints

Indian Institute of Technology, Kharagpur

Visual C# Instructor s Manual Table of Contents

Unit 3. Operators. School of Science and Technology INTRODUCTION

Structured Programming. Dr. Mohamed Khedr Lecture 4

CIS 110: Introduction to Computer Programming

Programming Project #2: Solving Quadratic Equations Date Due: Monday 25 September 2017

Beginning C Programming for Engineers

Transcription:

Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations 4th October, 2010 Integer arithmetic in C. Converting pre-decimal money to decimal. The int type and its operators. Variables. The swap problem. Assigning and re-assigning variables; The if-statement. Conditional expressions. Fixing the lsd program. Input using scanf. CP1 6 slide 1 4th October, 2010 CP1 6 slide 3 4th October, 2010 Practical 1 Practical 1 is out today. :-) Pick up a copy before leaving the lecture. due by 2pm, Monday 18 October. 4 Tasks: Part A on Imperial-to-Metric distance conversion. Parts B-D are basic geometric tasks, when input is given through an interactive graphics tool. Should be able to attempt Parts A-C right away! We discuss Parts B-D in detail on Tuesday 5 October. Today s Lecture More types: float and double. The marathon.c program. Quadratic Equations. General form of if-statement. Developing quadratic.c via nested if-statements. Boolean operators. CP1 6 slide 2 4th October, 2010 CP1 6 slide 4 4th October, 2010

A tiny problem Calculate the number of kilometres in a marathon We know: The number of miles (26) and yards (385) that make up the marathon distance; How many kilometres correspond to a mile ( 1.609); How many yards in a mile (1760). Types: double A float with double precision. Accurate to about 15 significant digits: Max value is 1.7976931348623157 10 308 ; Requires twice the storage space as float; The computer has to work harder when computing with doubles; Values may depend on your computer. Print with printf("%lf", doublevariable); The %lf meams long float How to compute the marathon distance in kilometres? CP1 6 slide 5 4th October, 2010 CP1 6 slide 7 4th October, 2010 Types: float A signed floating-point number: for example, 1.5, -2.337, 6 10 23, 0.0 (note the decimal points); for example, a number in a pocket calculator. Accurate to about 7 significant digits: Max value is 3.40282347 10 38 ; Requires the same amount of storage as int. Contrast with real numbers in mathematics? Print with printf("%f", floatvariable). %f means float float Choosing a Type For engineering calculations: eg, 3.0/2.0 = 1.5; When small inaccuracies is acceptable: 0.9999999 may be 1.0; When speed is important. double int When more precision is required. For indexing, status codes, etc. When inputting/outputting values which are naturally integer. Speed depends on hardware - int math is not necessarily faster! CP1 6 slide 6 4th October, 2010 CP1 6 slide 8 4th October, 2010

marathon.c const float KILOMETRES_PER_MILE = 1.609; const float YARDS_PER_MILE = 1760.0; int miles, yards; float kilometres; miles = 26; yards = 385; kilometres = (miles + yards/yards_per_mile)* KILOMETRES_PER_MILE; printf("%d miles and %d yards ", miles, yards); printf("equals %f kilometres.\n", kilometres); marathon1.c (explicit casting) const float KILOMETRES_PER_MILE = 1.609; const float YARDS_PER_MILE = 1760.0; int miles, yards; float kiloms; miles = 26; yards = 385; kiloms = ((float)miles + (float)yards/yards_per_mile)* KILOMETRES_PER_MILE; printf("%d miles and %d yards ", miles, yards); printf("equals %f kilometres.\n", kilometres); CP1 6 slide 9 4th October, 2010 CP1 6 slide 11 4th October, 2010 Mixing Types and casting What happens when we divide a float by an int? 3.0/2 =? Sometimes this will work, sometimes not. Safest option is to cast the integer into a float: 3.0/(float)2 = 1.50 + Addition. Mathematical Operators in C Subtraction or negation. * Multiplication (don t use x ). / Division - order is important here! % Integer remainder (eg, 5 % 3 = 2). % is an overloaded symbol). ++ Increment (x++ means x = x+1). sqrt Decrement (x-- means x = x-1). Computes the square-root of its argument, returning a double - eg sqrt(64.0) returns 8.0. CP1 6 slide 10 4th October, 2010 CP1 6 slide 12 4th October, 2010

Quadratic equations Consider any quadratic polynomial of the form ax 2 + bx + c. We know this equation has exactly two complex roots (solutions to ax 2 + bx + c = 0) given by: x = b ± b 2 4ac. 2a Suppose we want real roots ONLY. Three cases: If b 2 < 4ac, there are no real solutions. If b 2 = 4ac, there is one real solution: b/(2a). If b 2 > 4ac, there are two different real solutions. Assumptions :-( We made some HUGE assumptions for quadratic.c (A1) We assumed that sqrt((double)(b*b +4*a*c)) would return a value! But b 2 4ac is complex if b 2 < 4ac, and hence C s sqrt function is UNDEFINED in this case. (A2) By solving a quadratic, we (implicitly) assumed a is non-zero. SOLUTION - use the (general) if statement. CP1 6 slide 13 4th October, 2010 CP1 6 slide 15 4th October, 2010 quadratic.c - attempt 1 /* Compute the two roots of a quadratic. */ #include <math.h> // Need to include math.h to use sqrt. /* Vars for the 3 co-efficients, and for the roots we ll find.*/ int a, b, c; double x1, x2; printf("input the x^2 co-efficient a: "); scanf("%d", &a); printf("input the x co-efficient b: "); scanf("%d", &b); printf("input the constant term c: "); scanf("%d", &c); x1 = (-(double)b - sqrt((double)(b*b - 4*a*c)))/((double)(2*a)); x2 = (-(double)b + sqrt((double)(b*b - 4*a*c)))/((double)(2*a)); printf("the solutions to %dx^2 +%dx +%d = 0 are ", a, b, c); printf("%lf and %lf.\n", x1, x2 ); CP1 6 slide 14 4th October, 2010 if statement - general form if (<condition-1>) <statement-sequence-1>; else if (<condition-2>) <statement-sequence-2>;... else <statement-sequence-n>; <condition-1>,..., <condition-(n-1)> are all boolean expressions. <statement-sequence-1>,..., <statement-sequence-n> are all sequences of C-programming statements. CP1 6 slide 16 4th October, 2010

Boolean operators Assume e1 and e2 are (usually arithmetic) expressions... We can apply boolean operators to form a boolean expression. e1 == e2 e1 equal to e2 e1!= e2 e1 not equal to e2 e1 < e2 e1 less than e2 e1 <= e2 e1 less than or equal to e2 e1 > e2 e1 greater than e2 e1 >= e2 e1 greater than or equal to e2. note: We can compare float expressions in this way - but int comparisons are most reliable. quadratic1.c - general if statement if (b*b > 4*a*c) { x1 = (-(double)b - sqrt((double)(b*b -4*a*c)))/((double)(2*a)); x2 = (-(double)b + sqrt((double)(b*b -4*a*c)))/((double)(2*a)); else if (b*b == 4*a*c) { x1 = -((double)b)/((double)(2*a)); else { printf("no real solns to %dx^2 + %dx +%d = 0.\n", a, b, c); CP1 6 slide 17 4th October, 2010 CP1 6 slide 19 4th October, 2010 More complicated Boolean expressions Assume e1 and e2 are boolean expressions... Can build more complicated boolean expressions iteratively. 0 false (always) non-zero true (always)!e1 true if e1 is false e1 && e2 true if (e1 is true and e2 is true) e1 e2 true if (e1 is true or e2 is true) Nested if-statements The <statement-sequence> place-holder in the general if-statement allows other if-statements to be part of the program fragment. This is a nested use of the if-statement. Example - refine our quadratic.c program further. The expressions e1, e2 are (formally) integer expressions. Can think of integers as (informally) acting as boolean type. CP1 6 slide 18 4th October, 2010 CP1 6 slide 20 4th October, 2010

quadratic.c - header and input code #include <math.h> // Need to include math.h to use sqrt. int a, b, c; double x1, x2; printf("input the x^2 co-efficient a: "); scanf("%d", &a); printf("input the x co-efficient b: "); scanf("%d", &b); printf("input the constant term c: "); scanf("%d", &c); quadratic.c - what if a=0 If ax 2 + bx + c is a quadratic, and a is 0, then we have a linear equation: bx + c. This has... Exactly one root of value c/(b), if b 0. No root at all, if b = 0 Now incorporate this case into our code: CP1 6 slide 21 4th October, 2010 CP1 6 slide 23 4th October, 2010 quadratic.c - a 0 case if (b*b > 4*a*c) { x1 = (-(double)b - sqrt((double)(b*b -4*a*c)))/((double)(2*a)); x2 = (-(double)b + sqrt((double)(b*b -4*a*c)))/((double)(2*a)); else if (b*b == 4*a*c) { x1 = -((double)b)/((double)(2*a)); else { printf("no real solns to %dx^2 + %dx +%d = 0.\n", a, b, c); CP1 6 slide 22 4th October, 2010 quadratic2.c - all cases if (a!= 0) { if (b*b > 4*a*c) { x1 = (-(double)b - sqrt((double)(b*b -4*a*c)))/((double)(2*a)); x2 = (-(double)b + sqrt((double)(b*b -4*a*c)))/((double)(2*a));... else if (b*b == 4*a*c) { x1 = -((double)b)/((double)(2*a)); else { printf("no real solns to %dx^2 + %dx +%d = 0.\n", a, b, c) ; else if (b!= 0) { x1 = -((double)c)/((double)(b)); printf("1 real soln to %dx^2 +%dx +%d = 0.\n", a, b, c); printf("it is %lf.\n", x1); CP1 6 slide 24 4th October, 2010