Docsity

Exercise IV: Formats, Files, and Input/Output, Slides of Computer Science

Toggle sidebar
bg1
Exercise four – formats and files February 21, 2006
1
Exercise IV
Exercise IV –
–Formats and File
Formats and File
Input and Output
Input and Output
Larry Caretto
Computer Science 106
Computing in Engineering
and Science
February 21, 2006
2
Outline
• Review exercise three
• Exercise four goals
• Summarize lecture material on formats
and file input and output
• Outline tasks for exercise four
• Review for quiz in laboratory on
Thursday covering exercises 1-3 (pp 1-
106 of text; homework to February 21)
3
Review Exercise Three
• Saw various ways in which type
conversion occurs and use of mod (%)
operator
• Learned how to translate mathematical
equations into C++ statements
• Wrote programs using mathematical
functions atan and sqrt
• Used symbolic constants
4
More on Exercise Three
• Use meaningful variable names
– R, r, or radius, but not x
– pi, or PI but not y (p?)
zy
xw
a−
+
=y
xw
x
z
b2
−
=
wy
x
zy
x
w
c
+
−
=
a = ( w + x ) / ( y – z )
b = z / x * ( w – x ) / ( 2 * y )
c = ( w – x / y ) / ( z / x + w * y )
5
Exercise Four Goals
• As a result of this exercise you should
be able to accomplish the following:
– write output commands using format
manipulators fixed, scientific,
setw(w), and setprecision(p) to print
an aligned table
– write statements to get input from and write
data to files
– write statements that combine file input
and output with formatting
6
Formatting Output
• Default output
– up to six significant figures
– fixed or scientific determined automatically
• Format requires #include <iomanip>
• Have manipulators in output commands
– fixed/scientific set appearance
– setprecision(p) for number of digits
– setw(w) for width of output
pf3
pf4

Partial preview of the text

Download Exercise IV: Formats, Files, and Input/Output and more Slides Computer Science in PDF only on Docsity!

Exercise IVExercise IV^ – –^ Formats and FileFormats and File

Input and OutputInput and Output

Larry Caretto Computer Science 106

Computing in Engineering

and Science

February 21, 2006

2

Outline

  • Review exercise three
  • Exercise four goals
  • Summarize lecture material on formats and file input and output
  • Outline tasks for exercise four
  • Review for quiz in laboratory on Thursday covering exercises 1-3 (pp 1- 106 of text; homework to February 21)

3

Review Exercise Three

  • Saw various ways in which type conversion occurs and use of mod (%) operator
  • Learned how to translate mathematical equations into C++ statements
  • Wrote programs using mathematical functions atan and sqrt
  • Used symbolic constants

4

More on Exercise Three

  • Use meaningful variable names
    • R, r, or radius, but not x
    • pi, or PI but not y (p?)

y z

w x

a

y

w x x

b z 2

=^ −

wy

x

z

y

w x

c

a = ( w + x ) / ( y – z ) b = z / x * ( w – x ) / ( 2 * y ) c = ( w – x / y ) / ( z / x + w * y )

5

Exercise Four Goals

  • As a result of this exercise you should be able to accomplish the following: - write output commands using format manipulators fixed, scientific, setw(w), and setprecision(p) to print an aligned table - write statements to get input from and write data to files - write statements that combine file input and output with formatting 6

Formatting Output

  • Default output
    • up to six significant figures
    • fixed or scientific determined automatically
  • Format requires #include
  • Have manipulators in output commands
    • fixed/scientific set appearance
    • setprecision(p) for number of digits
    • setw(w) for width of output

7

More Formatting

  • See text for other manipulators
  • Format manipulators appear in output statements using the << operator - cout << setw(10) << x; - cout << setprecision(6) << setw(8) << x; - cout << setw(10) << x << setw(10) << y;
  • setw(w) is in effect for one variable only
  • Other manipulators in effect until changed 8

setprecision(p)

  • For default output (i.e. only using setprecision) p specifies number of significant figures
  • If the fixed or scientific manipulators are in effect, p specifies the number of places after the decimal point - double z = 123.45678; - cout << setprecision(5) << z; - cout << fixed << setprecision(3) << z; - First gives 123.46; second gives 123.

9

Input and Output Files

  • Requires #include
  • Files have a name on the operating system and a program variable name
  • Associate the two file names as follows
    • ofstream outFile(“asm4.out”); // for output
    • ifstream inFile(“mydata.dat”); // for input
  • Use program variable name to read from or write to file 10

File Example

ifstream iFile(“myInput.txt”); ofstream out(“prog.1”); double x, y; iFile >> x >> y; double z = sqrt( x * x + y * y ); out << “ x y z”; out << fixed << setprecision(4) << setw(10) << x << setw(10) << y << setw(10) << z;

Program variable names

Operating system names

11

Accessing Files

  • Access file from any windows program using operating system file name
  • Use Visual C++ to create input files and view output files
  • Input files must be in project folder
  • To display available output files, select “All Files” from the pulldown menu next to files of type in the Open menu 12

Tasks for Exercise Four

  • One – copy and paste code with various formats; run and study results
  • Two – Modify task one code to get different output formats
  • Three – copy and paste code with file input and output; run and study results
  • Four – Modify task three data file and code for new input data and output formats

19

Using Excel to Modify Data

  • See instructions in notes
  • Demonstration following presentation
  • General process
    • Copy from C++ input or output file to Excel
    • Select T e xt to columns command from D ata menu and follow wizard instructions
    • Manipulate data as required in Excel
    • Copy from Excel and paste into Visual C++

20

Reminder on Basics

  • Start Visual C++.Net with new project
  • Remember to select empty project!
  • Define source file for programs (4.cpp)
  • Define submission file (4.txt)
  • For tasks three and four define input data files and open output files after program is complete
  • For file output screen will only display Press any key to continue

21

Quiz Review

  • Thirty minutes – no computer use
  • Use of cin and cout
  • Basic program structure
  • Writing correct equations
  • Results of operations on data types
  • Sequential program execution
    • Get input before doing calculations
    • Complete calculations before output