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

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

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

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

Computer Science & Engineering 150A Problem Solving Using Computers

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

COMP 208 Computers in Engineering

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Programming Language A

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

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

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

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

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

Conditional Expressions

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Algorithms and Programming I. Lecture#12 Spring 2015

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CpSc 1111 Lab 4 Formatting and Flow Control

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

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

Programming and Data Structure

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

The C standard library

CS111: PROGRAMMING LANGUAGE II

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

Expressions. Eric McCreath

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

CpSc 1111 Lab 5 Formatting and Flow Control

Learning to Program with Haiku

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

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

printf( Please enter another number: ); scanf( %d, &num2);

Computational Physics - Fortran February 1997

Software Testing Prof. Rajib Mall Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Getting started in C

CSE P 501 Exam 8/5/04

CS1 Lecture 3 Jan. 18, 2019

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

CS3157: Advanced Programming. Outline

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

Computer Programming: Skills & Concepts (INF-1-CP1) Intro to Practical 1. 5th October, 2010

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

today cs3157-fall2002-sklar-lect05 1

C Tutorial: Part 1. Dr. Charalampos C. Tsimenidis. Newcastle University School of Electrical and Electronic Engineering.

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

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

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

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

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

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Structured Programming. Dr. Mohamed Khedr Lecture 4

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

EL2310 Scientific Programming

This Lecture. Computer Programming: Skills & Concepts (INF-1-CP1) Intro to Practical 1; more on scanf. Summary of Lecture 6. descartes.

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

Beginning C Programming for Engineers

Lecture 9 - C Functions

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

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

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

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

CMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32

2. Numbers In, Numbers Out

Programming in C++ PART 2

Trombone players produce different pitches partly by varying the length of a tube.

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

CS1 Lecture 3 Jan. 22, 2018

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

Signals, Instruments, and Systems W3. C Programming & Memory Management in C

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

Tail recursion. Decision. Assignment. Iteration

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Bristol Institute of Technology

CP Lab 5: Functions, pointers, some arrays

CP Lab 5: Functions, pointers, some arrays

Engineering 12 - Spring, 1999

Program Organization and Comments

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

Introduction to C. CS 2060 Week 1. Prof. Jonathan Ventura. Outline Introduction Introduction to C Homework 1 C Coding Style Wrap-up

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.

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Fundamentals of Programming Session 8

C Program Structures

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

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

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

The SPL Programming Language Reference Manual

AQA Decision 1 Algorithms. Section 1: Communicating an algorithm

Transcription:

CP Lect 5 slide 1 Monday 2 October 2017 Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont) Cristina Alexandru Monday 2 October 2017

Last Lecture Arithmetic Quadratic equation problem: ax 2 + bx + c = 0 Floating point data CP Lect 5 slide 2 Monday 2 October 2017

Today s lecture Solving quadratic with if-statements General form of the if-statement Boolean tests (using relational operators) What about degenerate quadratics? Refining quadratic.c CP Lect 5 slide 3 Monday 2 October 2017

Choice of variables for quadratic.c We will need to compute the square-root of b 2 4ac. The sqrt function available in the math library for C is of the type double sqrt (double x); For this simple reason we use double variables for our real roots. Precision is not really important to us, at least not now. CP Lect 5 slide 4 Monday 2 October 2017

CP Lect 5 slide 5 Monday 2 October 2017 C program to Solve Quadratic Equations Steps of our program: x = b ± b 2 4ac. 2a Take in the inputs a, b and c from the user (scanf). Need three int variables to store these values: a, b, c say; test whether b 2 4ac is non-negative. What we do will depend on the result of the test If negative, output a message about No real roots. If exactly 0, a repeated root. Otherwise, two differing roots as per formula. Get the square root of b 2 4ac (if non-negative). Output both roots (or one if repeated). return EXIT_SUCCESS;

int s = (b*b - 4*a*c); body of quadratic.c if (s < 0) { printf("no real roots to this quadratic.\n"); else if (s == 0) { printf("eq. has the repeated root %f.\n", -(double)b/(2.0 else { x1 = (-(double)b - sqrt(s))/(2.0*a); x2 = (-(double)b + sqrt(s))/(2.0*a); printf("the sols to %dx^2 +%dx +%d = 0 are ", a, b, c); printf("%lf and %lf.\n", x1, x2 ); Note: sqrt() takes a double argument, so the int expression s is automatically promoted to a double in sqrt(s). Question: is the cast (double)b necessary? CP Lect 5 slide 6 Monday 2 October 2017

Running quadratic.c quadratic.c (and the refinements of this program) uses the sqrt function from the math library. note: Not enough to include <math.h> in the code. <math.h> is just a header file for the math library (it explains the shape of the sqrt function, and other math functions). To run our program, we need to link to executable code for the math functions. Link by adding -lm to gcc command when compiling: gcc -Wall quadratic.c -lm -lm is minus ell m, NOT minus one m. CP Lect 5 slide 7 Monday 2 October 2017

Assumptions :-( We made some assumptions for quadratic.c By solving a quadratic, we (implicitly) assumed a is non-zero. Same for b. We might have had a linear or constant equation. SOLUTION - use the (general) if statement. CP Lect 5 slide 8 Monday 2 October 2017

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: either true or false. statement-sequence 1,..., statement-sequence n are all sequences of C-programming statements. Note that it is possible to use if alone without any else branch. CP Lect 5 slide 9 Monday 2 October 2017

Warning about if If you look in the textbooks, you will see that when the statement-sequence has only one statement, you can miss out the curly brackets round it: if ( t > 0 ) x = x + 1; else x = x - 1; We recommend that you don t do this (at least until you re experienced enough to understand when you can ignore our advice!). You will see it in other people s programs though. Actually, we lied about the general form of the if statement. In truth, the general form is if ( condition ) statement 1 else statement 2 and a statement can have the form { statement-sequence or can be an if-statement itself. However, the form on the previous slide is the way we typically use it, so best to learn that. CP Lect 5 slide 10 Monday 2 October 2017

CP Lect 5 slide 11 Monday 2 October 2017 Relational operators What kind of conditions can we use in if statements? Assume e 1 and e 2 are (usually arithmetic) expressions... We can apply relational operators to form a boolean expression. e 1 == e 2 e 1 equal to e 2 e 1!= e 2 e 1 not equal to e 2 e 1 < e 2 e 1 less than e 2 e 1 <= e 2 e 1 less than or equal to e 2 e 1 > e 2 e 1 greater than e 2 e 1 >= e 2 e 1 greater than or equal to e 2. Never write e 1 < e 2 < e 3 it is legal C, but it doesn t mean anything like what you think it means if you remember to -Wall, the compiler will warn you if you do this! note: We can compare float and double expressions in this way - but only int comparisons are fully reliable. Why is this?

CP Lect 5 slide 12 Monday 2 October 2017 More complicated Boolean expressions Assume e 1 and e 2 are boolean expressions... Can build more complicated boolean expressions iteratively, using boolean operators. 0 false (always) non-zero true (always)!e 1 true if e 1 is false e 1 && e 2 true if (e 1 is true and e 2 is true) e 1 e 2 true if (e 1 is true or e 2 is true) For example, can write (e 1 < e 2 ) && (e 2 < e 3 ). The expressions e 1, e 2 are (formally) integer expressions. We define boolean variables as int. Think of integers as (informally) acting as boolean type.

Boolean expressions in if-else statements We have seem lots of simple and complex boolean expressions: whenever any of these are used as tests in a if-else statement, they must be enclosed in parentheses. For example The simple Boolean expression x < 5+z, when being used as a test for an else if-branch of an if-else statement, would appear as else if (x < 5+z) {... The complex Boolean expression (a!= 0) && (b*b > 4*a*c), when being used as the test for the if branch of an if-else statement, would appear as if ( (a!= 0) && (b*b > 4*a*c) ) {... CP Lect 5 slide 13 Monday 2 October 2017

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 the quadratic.c program further to include a solution for the a = 0 case (given a linear equation). CP Lect 5 slide 14 Monday 2 October 2017

Issues for improving quadratic: Improving quadratic.c (i) make our quadratic solver cope with a = 0 (quadratic2.c)... and with a = 0, b = 0. (ii) complex roots (homework). CP Lect 5 slide 15 Monday 2 October 2017

quadratic equations what if a = 0 If ax 2 + bx + c = 0 is a quadratic, and a is 0, then we have a linear equation: bx + c = 0 This has... Exactly one root of value c/b, if b 0. No root at all, if b = 0 and c 0. Everything is a root, if b = c = 0. Can incorporate this case into our code: CP Lect 5 slide 16 Monday 2 October 2017

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); s = b*b - 4*a*c; CP Lect 5 slide 17 Monday 2 October 2017 quadratic2.c header and input code Start off as before... #include <stdio.h> #include <stdlib.h> #include <math.h> // Need to include math.h to use sqrt. int main(void) { int a, b, c, s; double x1, x2;

quadratic2.c a!= 0 versus a == 0 if (a!= 0) { if (s < 0) { /* THIS (a!= 0) */... /* BRANCH IS */ /* EXACTLY WHAT */ else if (s == 0) { /* WE PUT FOR THE */... /* BODY OF */ /* quadratic.c */ else {... else { /* THIS WILL BE THE */... /* SOLUTION FOR a==0 */ /* (linear equations) */ return EXIT_SUCCESS; We need to complete the else (a being 0) branch. CP Lect 5 slide 18 Monday 2 October 2017

quadratic2.c all cases if (a!= 0) { if (s < 0) {... // code from quadratic.c else if (b!= 0) { /* a==0 WITH b NON-ZERO */ x1 = -((double)c)/((double)b); printf("1 sol to %dx^2 +%dx +%d = 0.\n", a, b, c); printf("it is %lf.\n", x1); else if (c!= 0) { /* a AND b BOTH ZERO, c NON-ZERO */ printf("no sols to %dx^2 + %dx +%d = 0.\n", a, b, c); else { /* a, b, c ALL ZERO */ printf("degenerate equation - everything is a solution!\n"); return EXIT_SUCCESS; CP Lect 5 slide 19 Monday 2 October 2017

CP Lect 5 slide 20 Monday 2 October 2017 Reading and Working Relevant sections of A book on C are Sections 4.1, 4.2, 4.3, 4.4 (on Boolean expressions, Relational operators, etc) and Section 4.7 (on the if and the if-then-else statements). You already have the week 3 Tutorial sheet. Please attempt all Questions before your tutorial group. Also please think of one question about the CP material so far, and bring that question to the tutorial. How about coding up quadratic2.c? Could make quadratic3.c by also doing complex roots.