PHY 107 – Programming For Science. The Week’s Goal.

Slides:



Advertisements
Similar presentations
CSC Programming for Science Lecture 5: Actual Programming.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Introduction to C Language
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
CSC 107 – Programming For Science. Today’s Goal  Learn C functions to input and output data  Using printf to print out variables and strings  Read.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
EPSII 59:006 Spring Introduction In this lecture  Formatted Input/Output scanf and printf  Streams (input and output) gets, puts, getchar, putchar.
Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
CSC 107 – Programming For Science. Announcements.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Program Development Cycle 1.Edit program 2.Compile program - translates it from C to machine language 3. Run/execute your program. 4. If not satisfied,
PHY 107 – Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
CSCI 130 Basic Input and Output Chapter 9. The printf ( ) function Printf(“\nThe value of x is %d”, x); Displayed to screen (assume x = 12): –The value.
PHY 107 – Programming For Science. Today’s Goal  Understand what cStrings are  Relationship to arrays & primitives  What it is and why null character.
CSC Programming for Science Lecture 18: More Data Files.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
PHY 107 – Programming For Science. Final Exam  Wed., Dec. 16 th from 2:45PM – 4:45PM in OM110  For exam, plan on using full 2 hours  If major problem,
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.
PHY 107 – Programming For Science. Today’s Goal  Get familiar with opening & closing files  Declaring variables for use with files  Using variables.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Chapter 9 - Formatted Input/Output
C Formatted Input/Output
Formatted Input and Output
Input and Output: I/O Finish reading chapters 1 and 2 of the text
ECE Application Programming
CPS120: Introduction to Computer Science
TMF1414 Introduction to Programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
Intro to PHP & Variables
OUTPUT STATEMENTS GC 201.
Input and Output Lecture 4.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 9 - Formatted Input/Output
EECE.2160 ECE Application Programming
User Input Keyboard input.
Introduction to C Programming
Presentation transcript:

PHY 107 – Programming For Science

The Week’s Goal

Announcements  Weekly assignment #2 available on to D2L

Program Outline  Once upon a time…  … some stuff happens…  … and they all lived happily ever after

Program Outline  Once upon a time…  All programs must begin somewhere  Defines what is worked upon during rest of program  For non-trivial programs, requires receiving input  When starting program, first steps always same: 1. What is the input? 2. What will the input look like? 3. How will the data be entered?

Reading From The Keyboard  Easiest to get input from the keyboard  Reading from files possible; discussed later in term  C lacks standard, so writing GUI program harder  C defines scanf to get user’s input  As easy to use as delivering food to Granny’s house  When scanf hit, program waits until it has input  User must press enter for line to be able to be read  Editing not seen by program; only receives final line

Even Better Magic  Must tell scanf types it will be reading read Variable TypeSpecifier int%i, %d long int%li, %ld unsigned int%u unsigned long%lu float%f, %e, %E, %g, %G double%lf, %le, %lE, %lg, %lG long double%Lf, %Le, %LE, %Lg, %LG char%c

Programming Using scanf

Warning #1 will not  Incorrect scanf specifier will not trigger an error  Specifier, not variable, determines data read

Warning #2 will not  Incorrect scanf specifier will not trigger an error  Specifier, not variable, determines data read  Illegal assignments allowed, but result is ugly

Warning #3 will not  Incorrect scanf specifier will not trigger an error  Specifier, not variable, determines data read  Illegal assignments allowed, but result is ugly  YOUR RESPONSIBILITY to check in C/C++

scanf Example

Program Outline  Once upon a time…  Get the input using scanf from the keyboard  … some stuff happens…  … and they all lived happily ever after

Some Stuff Happens  This really depends on specific project details  Focus of most of term, we will skip this today

Program Outline  … and they all lived happily ever after  All good processing comes to end & report results  Shows program worked and provides feedback  Results takes many forms, focus on printing today  When starting program, second steps ask: 1. What must be output? 2. How can output be presented best? 3. Will it be pretty?

Using printf to Print  Already seen how to print text using printf printf(“Hello World\n”);  Prints out whatever is placed between quotes  Use escape sequences for fancier text output \n  newline (move to start of next line) \t  tab (go to next column that is multiple of 8) \\  \ (backslash character) \”  “ (quotation mark)

Can Print Out Multiple Items  printf can also print out value of variables int bob = 2; double j = 4.5; char var = ‘a’; printf(“Hello ”); printf(“%d\n”, bob); printf(“%lf\n”, j); printf(“%c\n”, var); printf(“%lf is not %d”, j, bob); printf(“%d equals bob\n”, bob); printf(“%c%d%lf”, var, bob, j);

But Can It Be Used?  printf built to provide basics needed to work  Prints out as many digits as needed  No extra spaces or tabs used  Scientific notation cannot be used  Often want to format results  Significant digits can matter  Tables make reading faster

Formatting Output #include #include int main() { int k = 4, bigK = 100; double stuff = ; printf(“%d\n”, k); printf(“%3d%d\n”, k, k); printf(“%-4d%2d\n”, k, bigK); printf(“%05d\n”, bigK); printf(“%lf\n”, stuff); printf(“%06lf\n”, stuff); printf(“%.2lf\n”, stuff); printf(“%10.2lf\n”, stuff); }

Your Turn  Get in groups & work on following activity

For Next Lecture