1 Lab Session-VIII CSIT-121 Spring 2002 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises.

Slides:



Advertisements
Similar presentations
CS110 Programming Language I
Advertisements

Programming Functions: Passing Parameters by Reference.
INFSY 307 C++ Powerpoint 6 File I/O. Stream and File I/O Function Overloading.
File streams Chapter , ,
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
1 Session-12 CSIT 121 Spring 2006 Test-1 is on March 9 th ; Demo-5 due date extended to March 7 Test-1 is on March 9 th ; Demo-5 due date extended to March.
1 Session-16 CSIT 121 Spring 2006 Demo for switch-case due now Demo for switch-case due now String comparison String comparison Slide 23; sample case.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems.
1 Lab Session-XI CSIT121 Fall 2000 b Arrays and Their Usage b Finding the largest element b Lab Exercise 11-A b Searching for some value b Lab Exercise.
1 Lecture 19 Chapter 4 Program Input and the Software Design Process Dale/Weems/Headington.
1 Lab Session-VI CSIT-120 Spring 2001 Let us look at C++ syntax rules in brief Exercise VI-A (Demo Required) Lab Assignment#4 Due May 1st, 2001 (No Lab.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 Lab Session-VIII CSIT-121 Fall 2000 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises.
1 Lab Session-11 CSIT 121 Fall 2003 Using arrays in functions Programming Exercise.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
1 Chapter 9 Scope, Lifetime, and More on Functions.
1 Lab Session-8 CSIT-121 Spring 2005 Call by Reference Lab Exercise for Demo Practice Problems.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
File I/O ifstreams and ofstreams Sections 11.1 &
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
LESSON 2 Basic of C++.
A Sample Program #include using namespace std; int main(void) { cout
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Chapter 9 Scope, Lifetime, and More on Functions
CSCE 206 Lab Structured Programming in C
Today’s Lecture I/O Streams Tools for File I/O
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Formatted Input, Output & File Input, Output
Reading from and Writing to Files
CSCE 206 Lab Structured Programming in C
File I/O in C++ II.
Reading from and Writing to Files Part 2
Reading from and Writing to Files
Presentation transcript:

1 Lab Session-VIII CSIT-121 Spring 2002 w Formatted Output w Call by Reference w Lab Exercises w File Handling w Lab Exercises

2 Formatted Output w Include to format output w You can control w width cout<<setw(12) for next value only w precision cout<<setprecision(4) w showpoint cout<<showpoint w fixed/scientific cout<<fixed w So that your floating point numbers are displayed as desired.

3 Call By Reference w Call by reference means passing the original variable to the function for changing w For example compute_sum_and_mean(int k, int j, int l, int& sum, float& mean) w It has three INPUT arguments and two REFERENCE arguments that are used to output the sum and mean

4 Lab Exercise 8-1 (Shown) w Using formatted output, try printing out the constants as below, with 3 digits to the right of decimal point and total width of 9 w const float VAL1= w const float VAL2= w const float VAL3= w Remember to close all workspaces and start fresh for each exercise

5 Lab Exercise 8-2 w For reference arguments exercise, develop a program that calls a function to order two integer values in the increasing numerical order. w Function name order_values(int& num1, int&num2) w when passed values (x,y), it orders the same as (y,x) if it finds y to be less than x. w The function is void as it returns the values through reference arguments

6 File Handling w Files are handled easily by including fstream and opening files w If the input file is not there, it will be created by the myfile.open() call w The standard error checking function calls are now given. Please include the same in your exercises

7 File Handling Sample Program w #include w using namespace std; w int main() w {

8 File Handling Sample Program w ifstream my_infile; w ofstream my_outfile; w string filename; w cout<<"Enter a file name to open: "; w cin>>filename; w my_infile.open (filename.c_str()); w

9 File Handling Sample Program w if (!my_infile) w { cerr << "*** ERROR: Cannot open " << filename << " for input." << endl; w return EXIT_FAILURE; w } w else cerr<<"success"<<endl; w return 0; w }

10 File Handling for Output Files w my_outfile.open (“out.dat”); w if (!my_outfile) w { w cerr << "ERROR: Cannot open " << “out.dat” w << " for output." << endl; w return EXIT_FAILURE; w }

11 Lab Exercise 8-3 w For a simple program, let us get each character from a file and display it in uppercase on the screen. w For this purpose, we need to develop a small program w A code segment is here to give you sufficient start-up. Include cctype file

12 Lab Exercise 8-3 w char next; w my_infile.get(next); w while (!my_infile.eof()) w { w cout.put(char(toupper(next))); w my_infile.get(next) w }

13 Lab Exercise 8.4 Demo Required w Develop a program that reads a text file and counts all occurrences of letter ‘E’ in it. You get bonus points if you can count all vowels with switch-case statement

14 Practice Exercises for File Handling w Write a program that reads two text files one after another and produces an output file that combines the content of both w Q10 page195 (Old book page 205) w Q8 page 197 (Old book page 207)