Physics 2660: Fundamentals of Scientific Computing. Lecture 3 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 5 Instructor: Prof. Chris Neu

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

Introduction to C++ with content from

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

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

Variables and Operators 2/20/01 Lecture #

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

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

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

Computer System and programming in C

Fundamentals of Programming

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

Pointers and Arrays 1

Work relative to other classes

BLM2031 Structured Programming. Zeyneb KURT

Lecture 10. Daily Puzzle

Fundamental of Programming (C)

CS 61C: Great Ideas in Computer Architecture Introduction to C

CS3157: Advanced Programming. Outline

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

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

Fundamentals of Structured Programming

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

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

C-LANGUAGE CURRICULAM

Introduction to C. Systems Programming Concepts

Course Outline Introduction to C-Programming

CS Programming In C

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

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

CSC 1107: Structured Programming

CpSc 1111 Lab 9 2-D Arrays

CSCI 171 Chapter Outlines

BSM540 Basics of C Language

CIS133J. Working with Numbers in Java

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

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

ET156 Introduction to C Programming

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

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

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

Preview from Notesale.co.uk Page 6 of 52

OBJECT ORIENTED PROGRAMMING

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS )

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

Announcements. 1. Forms to return today after class:

Topics so far. Review. scanf/fscanf. How data is read 1/20/2011. All code handin sare at /afs/andrew/course/15/123/handin

HW1 due Monday by 9:30am Assignment online, submission details to come

Should you know scanf and printf?

ET156 Introduction to C Programming

Fundamental of C programming. - Ompal Singh

Introduction to Scientific Computing and Problem Solving

Lecture Set 4: More About Methods and More About Operators

UNIT- 3 Introduction to C++

Basic Types and Formatted I/O

Structured Programming. Dr. Mohamed Khedr Lecture 4

Information Science 1


CS61C : Machine Structures

Computers Programming Course 5. Iulian Năstac

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

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

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

A Fast Review of C Essentials Part I

Elementary Programming

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

Visual C# Instructor s Manual Table of Contents

First of all, it is a variable, just like other variables you studied

File Handling in C. EECS 2031 Fall October 27, 2014

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Continued from previous lecture

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

2. Numbers In, Numbers Out

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

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

ECE 15B COMPUTER ORGANIZATION

C Programming

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

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

Lecture 02 C FUNDAMENTALS

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

CS349/SE382 A1 C Programming Tutorial

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

Variables and literals

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

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

Data Types. Data Types. Integer Types. Signed Integers

Transcription:

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

Announcements Weekly readings will be assigned and available through the class wiki home page HW02 was assigned Due Thursday 8 February Electronic submission: by 12:00 noon EST This week: Lab03 Will be posted on Thursday 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

Variable Definition in C, Storage in Memory 3

Variable Types in C All variables in C must be declared as a particular type Note: a string of characters in C is an array of single chars 4

Defining Variables in C * In C, variables should be defined at the beginning of the function in which they are being used: 5

Storing Variables When you run your program, an area in your computer s memory is set up to store the value for each of the variables you define. Different variable types require a different amount of space. int main(){ double velocity = 0.0; float x = 0.0; int number = 0; char a = a ; float y = 0.0; } Hence, one must be careful to match variable types with the actual use one envisions cannot put a decimal number in an int, for instance 6

Storage of an integer: Example For an integer we have up to 4 bytes = 32 bits of storage So the variable num defined as this: int num = 909337759; is stored this way: Comments: One bit is reserved for the sign +/- (leading bit): leading bit = 1 leading bit = 0 So max value we can store is 2 d 31-1 d = 2,147,483,647 d Implication: If your OS uses only 32-bit words for memory addresses, the max memory address is 4,294,967,296 meaning up to 4.3 GB of memory can be handled hence 32-bit systems cannot have much more than 4 GB of memory. 7

Storage of a non-integer: Example hhps://www.youtube.com/watch?v=n-xozgu1vim 8

Variable Storage: Examples 4 bytes 4 bytes 1 byte 9

Variables: How to know their size Note the last two lines: compound types. 5/2 is rightfully evaluated as a decimal number but has two int s as args hence it is assigned the size of an integer. So don t try to divide integers and expect the right answer unless you define things properly! And the last one the size of the result is given by the highest precision argument. 10

Using sizeof() 11

Defining Variables: Defaults? * What if I do not initialize (give an initial value to) a defined variable? 12

Defining Variables: Defaults? * What if I do not initialize (give an initial value to) a defined variable? In C, this could be 0.0, could be anything, MEANING IT HAS THE CHANCE TO BE GARBAGE. C does not initialize variables for you! It reuses memory space when defining a new variable though and this memory space is not flushed before the assignment: stale contents It is best to always initialize your defined variables to something appropriate 13

Casting Variables Casting is the conversion from one variable type to another can either increase or decrease the precision of a stored value Example: These are known as implicit casts the compiler knows how to increase/decrease precision for you! Implicit casts are generally not a good idea anything done for you by the compiler is generally not ideal. 14

Avoid Implicit Casts 15

Explicit Casts The preferred way to do this is the following: Virtues: no warning messages you are more aware of the precision of the stored variables / values 16

Preprocessor Directives 17

Preprocessor Directives 18

Variables vs. Preprocessor Definitions You can define oft-used constants (think the speed of light, etc.) using preprocessor definitions Use the #define command Comparison: #include <stdio.h> int main(){ } double radiusearth = 6378.1; double pi = 3.14159; printf( The circumference of the Earth = %f\n, 2.0*pi*radiusEarth); return(0); With preprocessor definitions, the compiler sees all instances of PI and replaces with 3.14159, etc 19

Preprocessor Definitions vs Constants The virtue of using preprocessor definitions is speed can speed up your program at run time, since fewer vars are stored in memory, meaning fewer accesses. But there are dangers: universal replace might go poorly: What if you had a function called SAPInterestCalc()? other preprocessor definitions are hidden from you hard to see if yours clobbers another say in a header file Instead, one can use const : Using const is the best coding practice compiler won t let it be altered! 20

FormaYed Input / Output 21

I/O Format Specifications Recall from lab00, you used printf() and scanf() to print/read values to/from screen/keyboard: % is the control key indicating some I/O formahed data followed by an I/O specifier: Read Brooks (Physics library reserves!) page 33 re: the difference between i and d and leading zeroes 22

Format Mismatches * 23

Format Mismatches * 24

Format Mismatches * 25

Format Mismatches * 26

Format Mismatches * 27

Format Mismatches * 28

Format Mismatches * 29

Controlling the Appearance of Output: Display * 30

Controlling the Appearance of Output: Escape Characters 31

Operators 32

Operator Types What kind of operations do we need to be able to do in a computer program? Here are some: Arithmetic (eg., +, -, *, / ) Assignment (eg., = ) Increment / decrement (eg., ++, --) used in counters Logical / Conditional (eg., &&, ) Bitwise operations Pointer operations 33

Arithmetic Operators 34

Arithmetic 35

Assignment Operators 36

Increment/Decrement Operators 37

Logical / Conditional Operators 38

Logical / Conditional Operators we ll see much more of these when we discuss control sequences 39

Operator Precedence: Which Comes First? or 40

Bad Coding Example: I 41

Bad Coding Example: II 42

We ll pick up from here next time. Don t forget homework and lab on Thursday! See you then. 43