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

Similar documents
Physics 2660: Fundamentals of Scientific Computing. Lecture 7 Instructor: Prof. Chris Neu

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

Decision Making and Loops

CS 241 Data Organization. August 21, 2018

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

CS1 Lecture 3 Jan. 18, 2019

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

Lecture 10. Daily Puzzle

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Structured Program Development in C

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

Structured Program Development

Lecture 04 FUNCTIONS AND ARRAYS

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

H192 Midterm 1 Review. Tom Zajdel

Announcements. 1. Forms to return today after class:

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Chapter 5: Control Structures

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

Introduction. C provides two styles of flow control:

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

CS1010 Programming Methodology A beginning in problem solving in Computer Science. Aaron Tan 24 July 2017

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

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

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Week 1 Introduction to Computer and Algorithm (Part1) UniMAP Sem II 11/12 DKT121: Basic Computer Programming 1

Lecture Transcript While and Do While Statements in C++

DECISION CONTROL AND LOOPING STATEMENTS

Introduction to Programming

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

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

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

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

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

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

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

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

Fundamental of Programming (C)

Fundamentals of Programming

Using the MIPS Calling Convention. Recursive Functions in Assembly. CS 64: Computer Organization and Design Logic Lecture #10 Fall 2018

Welcome to... CS113: Introduction to C

LECTURE 5 Control Structures Part 2

Lecture 8. Daily Puzzle

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

C Programming Lecture V

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

1. The programming language C is more than 30 years old. True or False? (Circle your choice.)

EK131 E5 Introduction to Engineering

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Compiling and Running a C Program in Unix

CSCI 136 Data Structures & Advanced Programming. Spring 2018 Instructors Bill Jannen & Jon Park

CMSC 201 Computer Science I for Majors

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

C: How to Program. Week /Mar/05

EC 413 Computer Organization

CS1 Lecture 3 Jan. 22, 2018

Theory of control structures

EECE.2160: ECE Application Programming Spring 2017

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

CprE 281: Digital Logic

Control Structure: Selection

Fundamentals of Programming Session 8

CSCE 206: Structured Programming in C

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

YEAH 2: Simple Java! Avery Wang Jared Bitz 7/6/2018

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Chapter 2 - Introduction to C Programming

CS3157: Advanced Programming. Outline

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

FOR Loops. Last Modified: 01 June / 1

1. The process of writing computer instructions using programming language is called _PROGRAMMING/ CODING

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

CS 221 Lecture. Tuesday, 13 September 2011

Variables and Operators 2/20/01 Lecture #

General course information

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 5. Aykut Erdem, Erkut Erdem, Fuat Akal

SAMS Programming A/B. Lecture #1 Introductions July 3, Mark Stehlik

Note: If only one statement is to be followed by the if or else condition then there is no need of parenthesis.

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

Introduction to C Programming

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

CS1 Lecture 2 Jan. 16, 2019

Continued from previous lecture

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

COMP 208 Computers in Engineering

CpSc 1111 Lab 4 Formatting and Flow Control

Lecture 6. Statements

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Introduction to Information Technology ITP 101x (4 Units)

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

Lesson 3 Introduction to Programming in C

What is Graphic Design?

Fundamentals of Digital System Design ECE 3700, CPSC 3700

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

Transcription:

Physics 2660: Fundamentals of Scientific Computing Lecture 5 Instructor: Prof. Chris Neu (chris.neu@virginia.edu)

Reminder I am back! HW04 due Thursday 22 Feb electronically by noon HW grades are coming. Please be patient. Office hours: all held in our computer lab, room 022-C of this bldg Me: After lecture 3:30-4:30 every Tuesday TAs: Mondays: 3-5pm and 6-8pm Wednesdays: 5-9pm 2

Last time: C program structure Intro to a basic C program Review and Outline Defining simple variables and doing arithmetic FormaWed input/output via printf() and scanf() How variables values are stored on a computer More on formawed I/O Today: Comments Functions Conditional structures: Loops if, if/else, if/elseif/else switch/case Count-controlled loops: for()more on loops Conditioned controlled loops: while(), do Random numbers 3

Comments Inside Your Code 4

Internally Documenting / Comments 5

Functions 6

Introduction to Functions C is a simple language Its utility is extended through the use of re-usable functions Some functions are found in standard libraries like stdio.h and math.h Users can write functions too for many good reasons! 7

C Standard Library Functions 8

C Standard Library Functions Common C Standard Libraries we will use: stdio.h math.h Best to get in the habit of including these in every program you write! You don t even know WHERE these files are on galileo in order to use them. 9

Functions from the C math library 10

User-Defined Functions Why would one want to write their own functions? Avoid duplicating the same code many times within a program: streamlined simplicity Re-usable functions are easier to maintain and modify Functions are portable can be used in other programs Simplicity: code something intricate once and call it via a simple single line rather than multi-line complications 11

User-Defined Functions: Example Use Case No functions repetitive! 12

User-Defined Functions: Use Case Example Three main parts: Prototype, Definition, and Calls 13

User-Defined Functions: Use Case Example 14

User-Defined Functions: Reusable? 15

Conditional Executions in C 16

Linear Code Execution in C Simple pieces of code have lines of instructions that are executed linearly, calling external functions like printf: #include <stdio.h> int main(void) { int num = 5; printf(hello, world. num = %d\n,num); return 0; } This is not typical however Most programs need to do more intricate things in their execution 17

Conditional Executions in C: if statements 18

Conditional Checks 19

Conditional Execution: if and else 20

Conditional Execution: if and else 21

Conditional Execution: switch and case 22

Conditional Execution: switch and case 23

if/else versus switch/case 24

The? Operator 25

DANGER: = versus == 26

DANGER: = versus == Take-away message: Be careful to use == in conditionals and not =. 27

Return Values and Tests Values are returned for all conditional checks: if false, the value is zero.. the value of the expression (5 < 3) is 0 if true, the value is not zero.. the value of the expression (5 > 3) is 1 We can use these characteristics in our code: 28

Return Values and Tests: From Functions One can call a function that performs some test Did it fail? 29

DANGER: Take Care with Floating-Point Numbers 30

DANGER: Take Care with Floating-Point Numbers Quiz time: 31

DANGER: Take Care with Floating-Point Numbers Quiz time: Lets say A = (10000000000.0 / 3.0) * 3.0 32

DANGER: Take Care with Floating-Point Numbers Quiz time: Lets say A = (10000000000.0 / 3.0) * 3.0 What is A? 33

DANGER: Take Care with Floating-Point Numbers Quiz time: Lets say A = (10000000000.0 / 3.0) * 3.0 What is A? Well, we all know the calculation should yield 10000000000.0 34

DANGER: Take Care with Floating-Point Numbers Quiz time: Lets say A = (10000000000.0 / 3.0) * 3.0 What is A? Well, we all know the calculation should yield 10000000000.0 The computer would evaluate things in this manner: float A = (10000000000.0/3.0)*3.0; if (A==10000000000.0 ){ printf( A=%lf\n,A); } else printf( A is not 10000000000.0 \n ); 35

DANGER: Take Care with Floating-Point Numbers Quiz time: Lets say A = (10000000000.0 / 3.0) * 3.0 What is A? Well, we all know the calculation should yield 10000000000.0 The computer would evaluate things in this manner: What will be printed? float A = (10000000000.0/3.0)*3.0; if (A==10000000000.0 ){ printf( A=%lf\n,A); } else printf( A is not 10000000000.0 \n ); 36

DANGER: Take Care with Floating-Point Numbers Quiz time: Lets say A = (10000000000.0 / 3.0) * 3.0 What is A? Well, we all know the calculation should yield 10000000000.0 * The computer would evaluate things in this manner: What will be printed? float A = (10000000000.0/3.0)*3.0; if (A==10000000000.0 ){ printf( A=%lf\n,A); } else printf( A is not 10000000000.0 \n ); A is not 10000000000.0 What? 37

Floats and Comparisons 38

Floats and Comparisons 39

Loops! 40

Loops Loops allow computers to do what computers do best: Execute repetive boring tasks efficiently and accruately over and over again Two types: 41

Count-Controlled Loops! 42

Count-controlled loops: for() 43

How a for() loop works 44

for() loop: Important Parts Every for() loop has four important parts: counter initialization test condition counter update body of execution 45

Example: Good for() loop usage 46

Example: BAD for() loop usage 47

for() loops and break statements 48

for() loops and continue statements 49

Nested Loops 50

Condition-Controlled Loops! 51

Count-controlled Loops vs. Condition-controlled Loops 52

Pre-test Conditional Loops: while() 53

Post-test Conditional Loops: do/while() 54

We ll pick up from here next time. See you Thursday in labs! 55