C++ For Science and Engineering Lecture 14

Similar documents
C++ For Science and Engineering Lecture 10

C++ For Science and Engineering Lecture 17

C++ For Science and Engineering Lecture 13

C++ For Science and Engineering Lecture 8

2. Write a recursive function to add the first n terms of the alternating harmonic series:

Numerical solution of the time-independent 1-D Schrödinger equation. Matthias E. Möbius. September 24, 2010

CS 7B - Fall Final Exam (take-home portion). Due Wednesday, 12/13 at 2 pm

Congratulations you're done with CS103 problem sets! Please evaluate this course on Axess!

import java. u t i l. ;... Scanner sc = new Scanner ( System. in ) ;

In this approach, the ground state of the system is found by modeling a diffusion process.

ENS Lyon Camp. Day 5. Basic group. C October

CS 7B - Fall Assignment 4: Exploring Wumpus (Linked Rooms). Due 12/13/18

Molecular Dynamics Simulation of Argon

Numerical differentiation

The Monte Carlo Algorithm

Root Tutorial: Plotting the Z mass

Yod field statistics for tachyon interactions

An (incomplete) Introduction to FreeFem++

CS 7B - Spring Assignment: Ramsey Theory with Matrices and SFML. due 5/23/18

Congratulations you're done with CS103 problem sets! Please evaluate this course on Axess!

Programsystemkonstruktion med C++: Övning 1. Karl Palmskog Translated by: Siavash Soleimanifard. September 2011

Computational Physics (6810): Session 2

Cluster Algorithms to Reduce Critical Slowing Down

Data Structure and Algorithm Homework #1 Due: 2:20pm, Tuesday, March 12, 2013 TA === Homework submission instructions ===

Laboratory Exercise #10 An Introduction to High-Speed Addition

Computational Simulations of Magnets at High Temperatures Utilizing the Ising Model and the Monte Carlo Technique

2 2 mω2 x 2 ψ(x) = Eψ(x),

Introduction to 3D Game Programming with DirectX 9.0c: A Shader Approach

Slides from FYS3150/FYS4150 Lectures

Example problem: Adaptive solution of the 2D advection diffusion equation

Machine Learning 9/2/2015. What is machine learning. Advertise a customer s favorite products. Search the web to find pictures of dogs

Introduction to Language Theory and Compilation: Exercises. Session 2: Regular expressions

Lecture 44. Better and successive approximations x2, x3,, xn to the root are obtained from

APPENDIX A FASTFLOW A.1 DESIGN PRINCIPLES

Applied C Fri

Switch-Case & Break. Example 24. case 1 : p r i n t f ( January \n ) ; break ; case 2 : p r i n t f ( F e b r u a r y \n ) ; break ;

Discrete Random Variables (cont.) Discrete Distributions the Geometric pmf

Simulating Gravity. AiS Challenge Final Report April 7 th, Team 28 Farmington High School

The Euler Method for the Initial Value Problem

MA204/MA284 : Discrete Mathematics Week 2: Counting with sets; The Principle of Inclusion and Exclusion (PIE) 19 & 21 September 2018

INTRODUCTION. This is not a full c-programming course. It is not even a full 'Java to c' programming course.

Introduction to ANTLR (ANother Tool for Language Recognition)

THEORY OF COMPILATION

Tensor calculus with Lorene

Lecture 4: September 19

CS 4110 Programming Languages & Logics. Lecture 16 Programming in the λ-calculus

Tools for Feature Extraction: Exploring essentia

DAILY QUESTIONS 28 TH JUNE 18 REASONING - CALENDAR

Notater: INF3331. Veronika Heimsbakk December 4, Introduction 3

Project 4/5 - Molecular dynamics part II: advanced study of Lennard-Jones fluids, deadline December 1st (noon)

Introduction to Computer Programming, Spring Term 2018 Practice Assignment 5 Discussion: power(m,n) = m n

Gascoigne 3D High Performance Adaptive Finite Element Toolkit

ECEN 449: Microprocessor System Design Department of Electrical and Computer Engineering Texas A&M University

Supplemental Materials. In the main text, we recommend graphing physiological values for individual dyad

Multimedia. Multimedia Data Compression (Lossless Compression Algorithms)

Lecture 5: The Shift-And Method

DISCRETE RANDOM VARIABLES EXCEL LAB #3

Lecture 3: Finite Automata

NP Completeness. CS 374: Algorithms & Models of Computation, Spring Lecture 23. November 19, 2015

Mechanized Operational Semantics

Bfh Ti Control F Ws 2008/2009 Lab Matlab-1

T Reactive Systems: Temporal Logic LTL

A NEW ALGORITHM FOR FINDING HAMILTONIAN CIRCUITS

AGEC 621 Lecture 16 David Bessler

Measuring lateness. Definition of Measurement (Fenton)

CS 7B - Spring Assignment: Adapting the calculator for bitwise expressions. due 2/21/18

CS 151 Complexity Theory Spring Solution Set 5

CHAPTER 2. The Derivative

CS 311 Sample Final Examination

15 Monte Carlo methods

SAMPLE ANSWERS MARKER COPY

More Example Using Loops

CS 591, Lecture 9 Data Analytics: Theory and Applications Boston University

Comp 11 Lectures. Mike Shah. July 12, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 12, / 33

ACM-ICPC South Western European Regional SWERC 2008

Mimir NIR Spectroscopy Data Processing Cookbook V2.0 DPC

Accumulators. A Trivial Example in Oz. A Trivial Example in Prolog. MergeSort Example. Accumulators. Declarative Programming Techniques

How to write proofs - II

G54FOP: Lecture 17 & 18 Denotational Semantics and Domain Theory III & IV

ENEE 459E/CMSC 498R In-class exercise February 10, 2015

8: Statistical Distributions

CS415 Compilers Syntax Analysis Top-down Parsing

Intelligent Agents. Formal Characteristics of Planning. Ute Schmid. Cognitive Systems, Applied Computer Science, Bamberg University

The file may not actually come from a bank, but could be given by the customer (typically very large customers). File Layout

Propositional Logic: Semantics and an Example

Mass Transfer Operations I Prof. Bishnupada Mandal Department of Chemical Engineering Indian Institute of Technology, Guwahati

Mobile Video Identification

Session 5 (last revised: January 24, 2010) 5 1

Electric Fields and Equipotentials

Instructions for use of molly to prepare files for Doppler or Modmap

Introduction to Turing Machines. Reading: Chapters 8 & 9

Complete problems for classes in PH, The Polynomial-Time Hierarchy (PH) oracle is like a subroutine, or function in

COSE312: Compilers. Lecture 2 Lexical Analysis (1)

Compiling Techniques

Intro (Stefan

Computability, Undeciability and the Halting Problem

Introduction to Cryptology Dr. Sugata Gangopadhyay Department of Computer Science and Engineering Indian Institute of Technology, Roorkee

15-451/651: Design & Analysis of Algorithms September 13, 2018 Lecture #6: Streaming Algorithms last changed: August 30, 2018

4.4 The Calendar program

Turing Machines Part III

Transcription:

C++ For Science and Engineering Lecture 14 John Chrispell Tulane University Monday September 27, 2010

File Input and Output Recall writing text to standard out You must include the iostream header file. The iostream header file declares an ostream variable, or object, called cout. You must account for the std namespace. (Use the using namespace directive, or the std:: prefix for elements such as cout and endl). You can use cout with the >> operator to read a variety of data types. John Chrispell, Monday September 27, 2010 slide 3/17

File Input and Output File output parallels this. You must include the fstream header file. The fstream header file defines an ofstream class for handling output. You need to declare one or more ofstream variables or objects, which you may name as you wish. You must account for the std namespace. This can be done using the namespace directive or using the std:: prefix for elements such as ofstream. You need to associate a specific ofstream object with a specific file. One way is to use the open() method. When your done you close the file using the close() method. You can use the >> operator with the ofstream object to output a variety of data types. John Chrispell, Monday September 27, 2010 slide 5/17

File Input and Output The syntax is as follows: o f s t r e a m o u t F i l e ; // o u t F i l e an o f s t r e a m o b j e c t o f s t r e a m f o u t ; // a second o f s t r e a m ; / a s s o c i a t e the stream with a f i l e / o u t F i l e. open ( M y f i l e. t x t ) ; // o u t F i l e can now be used l / a second way below / c i n >> f i l e n a m e ; f o u t. open ( f i l e n a m e ) ; / G i v e s a s p e c f i c out f i l e name / Consider the following example: John Chrispell, Monday September 27, 2010 slide 7/17

outfile.cpp #i n c lude <iostream > #i n c lude <fstream > // f o r f i l e I /O i n t main ( ) { using namespace std ; char automobile [ 5 0 ] ; i n t year ; double a p r i c e ; double d p r i c e ; ofstream o u t F i l e ; // create object for output o u t F i l e. open ( c a r i n f o. txt ) ; // a s s o c i a t e with a f i l e cout << Enter the make and model of automobile : ; c i n. g e t l i n e ( automobile, 5 0 ) ; cout << Enter the model year : ; c i n >> year ; cout << Enter the o r i g i n a l asking p r i c e : ; cin >> a p r i c e ; d p r i c e = 0.913 a p r i c e ; / ======================================= / / d i s p l a y i n f o r m a t i o n on s c r e e n with cout / / ======================================= / cout << f i x e d ; cout. p r e c i s i o n ( 2 ) ; cout. s e t f ( i o s b a s e : : showpoint ) ; cout << Make and model : << automobile << endl ; cout << Year : << y e a r << e n d l ; cout << Was asking $ << a p r i c e << endl ; cout << Now asking $ << d p r i c e << endl ; / ====================================================== / / now do e x a c t same t h i n g s u s i n g o u t F i l e i n s t e a d o f cout / / ====================================================== / out F ile << f i x e d ; o u t F i l e. p r e c i s i o n (4); o u t F i l e. s e t f ( i o s b a s e : : showpoint ) ; o u t F i l e << Make and model : << automobile << endl ; o u t F i l e << Year : << y e a r << e n d l ; o u t F i l e << Was asking $ << a p r i c e << endl ; o u t F i l e << Now asking $ << d p r i c e << endl ; o u t F i l e. c l o s e ( ) ; // done with f i l e return 0 ; John Chrispell, Monday September 27, 2010 slide 9/17

Reading Text from a file To read text from a file you must: Include the fstream header file defining an ifstream class for handeling input. You declare an ifstream variable. Name it as you please. You must account for the std namespace. Associate an ifstream object with the file. We will use the open() method to do this and close() when we are done. Now you can use the << operator to read a variety of data types. You may also use the get() method to read individual characters and the getline() method for a line of characters at a time. You may use eof() and fail() to monitor the success of input attempts. Note the ifstream object when used as a test condition is a Boolean type. Ture when reads are successful. John Chrispell, Monday September 27, 2010 slide 11/17

sumafile.cpp #i n c lude <iostream > #i n c lude <fstream> // f i l e I /O suppport #i n c lude <c s t d l i b > // s u p p o r t f o r e x i t ( ) const i n t SIZE = 6 0 ; i n t main ( ) { using namespace std ; char f i l e n a m e [ SIZE ] ; ifstream infile ; // object for handling f i l e input cout << Enter name o f data f i l e : ; cin. getline ( filename, SIZE ) ; infile. open ( filename ) ; // associate infile with a f i l e / Test to see i f it opens / i f (! i n F i l e. i s o p e n ( ) ) { cout << Could not open the f i l e << filename << endl ; cout << Program t e r m i n a t i n g. \ n ; e x i t ( EXIT FAILURE ) ; double v a l u e ; // Place h o l d e r f o r f a l u e read. double sum = 0. 0 ; // Sum o f v a l u e s read i n t count = 0 ; // c o u n t e r f o r number o f i t e m s read / Read f i l e v a l u e s / i n F i l e >> v a l u e ; // get f i r s t v a l u e while ( infile. good ( ) ) { // while input good and not at EOF count++; // one more item read sum += value ; // c a l c u l a t e running t o t a l i n F i l e >> value ; // get next value John Chrispell, Monday September 27, 2010 slide 13/17

sumafile.cpp count / End t h i n g s i n some manner / i f ( i n F i l e. e o f ( ) ) { cout << End o f f i l e r e a c h e d. \ n ; e l s e i f ( i n F i l e. f a i l ( ) ) { cout << Input terminated by data mismatch. \ n ; e l s e { cout << Input terminated f o r unknown reason. \ n ; / Make a c o n c l u s i o n / i f ( count == 0){ cout << No data p r o c e s s e d. \ n ; e l s e { cout << Items read : << count << endl ; cout << Sum : << sum << endl ; cout << Average : << sum / count << e n d l ; i n F i l e. c l o s e ( ) ; // f i n i s h e d with the f i l e return 0 ; John Chrispell, Monday September 27, 2010 slide 15/17

Plain Pixel Map Files One particular image format is the plain pixel map or.ppm file. P3 10 10 255 255 255 255 255 255 John Chrispell, Monday September 27, 2010 slide 17/17