File Review Declare the File Stream Object Name

Slides:



Advertisements
Similar presentations
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Advertisements

1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
1 CS 105 Lecture 9 Files Version of Mon, Mar 28, 2011, 3:13 pm.
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Functions l Control structures l every C++ program must have a function called main l program execution.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 CS 105 Lecture 10 Functions Version of Mon, Mar 28, 2011, 3:13 pm.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
Chapter 7 Functions.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 8 Functions.
Test 2 Review Outline.
Chapter Topics The Basics of a C++ Program Data Types
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Basic Elements of C++.
Arrays Part-1 Armen Keshishian.
Introduction to C++ October 2, 2017.
Multi-dimensional Array
Quiz Next Monday.
Basic Elements of C++ Chapter 2.
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
Variables with Memory Diagram
One-Dimensional Array Introduction Lesson xx
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Counting Loops.
Know for Quiz Everything through Last Week and Lab 7
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade
Arrays Syntax: type variableName[size];
Standard Input/Output Stream
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 3 Input output.
CS150 Introduction to Computer Science 1
Formatted Input, Output & File Input, Output
7 Arrays.
Functions.
CHAPTER 4 File Processing.
Arrays Arrays A few types Structures of related data items
Fundamental Programming
COMS 261 Computer Science I
Functions Imran Rashid CTO at ManiWeber Technologies.
(Dreaded) Quiz 2 Next Monday.
Chapter 8 Functions.
CS1201: Programming Language 2
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
Announcements Exam 2 Lecture Grades Posted on blackboard
Presentation transcript:

File Review Declare the File Stream Object Name ofstream for output to file ifstream for input from file Associate a File Name with the File Stream Object by Opening the File Read or Write to the File using << or >> Operators Close the File

mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream input; int loops, integer, i; float decimal; string name; input.open("mydata.txt"); input >> loops; for(i= 0 ; i < loops; i++) input >> integer; input >> decimal; input >> name; cout << integer << " "; cout << decimal << " "; cout << name << endl; } return(0); mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary -23 -4.55 Smith -3 -4e3 xyz Output: 8 9.3 Jon 6 14.335 Bill 0 3.567e+010 Mary -23 -4.55 Smith -3 -4000 xyz

Functions Lines of code with a Name() Name()can be executed over and over again in the same program.

Functions displayVals() { int main() cout << …; { … return; /*back to where we left off */ } int main() { … displayVals(); return(0); }

Function Attributes Function Name: Identifier Used to Call Function Function Parameter(s) or Argument(s): Value(s) Passed into Function for Use by Function Code Function Return Value: Value Returned by Function Back to Calling Function

Function Parameters (Arguments) May Pass as Many Parameters as Necessary to Function A Copy of the Value of the Parameter Is Passed to the Function Changing the Value of the Parameter in the Function Does Not Affect the Value of the Original Variable This Is Called Pass-by-Value

Declaring a Function Function Prototype: Declaring a Function and How It Is Called Syntactically Used by the Compiler to Signal Syntax Errors Pseudocode Example: return_type function_name(parameter_type parameter_name); Example: double calculate_avg(int totOfItems, int numItems);

Functions – Return Values Functions Are Typed According to Their Return Values: void, int, double, etc. Functions Are Declared by Function Prototypes (Declarations) Found Above main() Function Returns a Value to Calling Function via return Statement Standard Functions (e.g., Those in STL) Have Function Prototypes in Header Files

Variable Scope Scope: Area of a Program within which a Variable Can Be Referenced Variable Definitions Are Recognized in the Curly Braces in which They Were Defined Variables Declared Outside of Functions Are Recognized from the Point Declaration through the Rest of the Program

Function Attributes #include <iostream> using namespace std; void printNum(int); // function prototype int main() { int myNumber = 7; printNum(myNumber); // function call printNum(9); return 0; } // begin function definition void printNum(int numToPrint) cout << numToPrint;

Another Function Example #include <iostream> using namespace std; double calcAverage(int total, int numItems); int main() { int allGrades = 974, numStudents = 10; double avgGrade = 0.0; avgGrade = calcAverage(allGrades,numStudents); cout << avgGrade << endl; return 0; } double calcAverage(int total, int numItems) return (double) total / numItems;

What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.

Program for Previous string empName1, empName2, empName3,… string empLocation1, empLocation2, … cout << “Enter employee name 1:”; cin >> empName1; cout << “Enter Employee name 2:”; cin >> empName2; … //Can we use a loop?

Arrays Syntax: type variableName[size]; Memory Is Set Aside for size Items of type Each Variable Location in Array Is Accessed by Offsetting into the Array with an Integer Expression Legitimate Offsets Are 0 to size-1 Example: lastname[0] = ‘H’;

Array Example char lastname[100]; lastname[0] = ‘H’; lastname[1] = ‘a’; lastname[2] = ‘\0’; cout << lastname[0]; cout << lastname;

Array Example int values[15], i = 0; values[i] = 150; cout << values[0]; values[3] = values[0] + 6;

Array Example const int ARRAY_SIZE = 100; int offset; int numArray[ARRAY_SIZE]; for(offset = 0; offset < ARRAY_SIZE; offset++) { numArray[offset] = 0; }

Array Example const int ARRAY_SIZE = 10; int offset, sum = 0, average; int numArray[ARRAY_SIZE]; for(offset = 0; offset < ARRAY_SIZE; offset++) { cout << “Enter Score ” << offset << “ : “; cin >> numArray[offset]; } sum = sum + numArray[offset]; average = sum / ARRAY_SIZE;

Exam 2 25% of Final Grade Know: loops, switch/case Files (.open(), >> and <<, .close() ) Input Failure (cin, cin.clear(), cin.ignore()) Functions Arrays

Initializing Arrays int main() { char cArray3[5] = {'a', 'b', 'c'}; int iArray[] = {1, 2, 3, 4}; int iArray2[10] = {10, 13, 15, 17}; double dArray[] = {3.4, 5.67e4}; double dArray1[5] = {6.7, 7.8, 9.5}; }

Initializing string Arrays #include <string> using namespace std; int main() { string sArray[] = {"one", "two", "three"}; cout << sArray[0]; }

Two Dimensional Arrays char cArray[10][20]; int iArray[100][50]; cArray[0][0] = ‘a’; cArray[0][1] = ‘b’; cArray[9][19] = ‘x’; iArray[0][0] = 99; iArray[1][5] = 135; iArray[99][49] = 0;