From lab 1 Using cs lab machines (including remote)

Slides:



Advertisements
Similar presentations
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Advertisements

1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
1 Chapter 4 Program Input and the Software Design Process.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
File I/O ifstreams and ofstreams Sections 11.1 &
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
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.
Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
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.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Chapter 4 Program Input and the Software Design Process
Chapter 1: Introduction to Computers and Programming
Chapter 14: Sequential Access Files
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
ifstreams and ofstreams
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
CISC/CMPE320 - Prof. McLeod
week 1 - Introduction Goals
Chapter 1 Software Engineering Principles
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
Chapter 2 part #3 C++ Input / Output
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 1: Introduction to Computers and Programming
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
understanding memory usage by a c++ program
Programming Funamental slides
when need to keep permanently
Chapter 3: Input/Output
Standard Input/Output Stream
No I/O is built into C++ instead, a library provides input stream and output stream Keyboard Screen executing program istream ostream 1.
Chapter 3 Input output.
CS150 Introduction to Computer Science 1
CS 144 Advanced C++ Programming February 5 Class Meeting
Reading from and Writing to Files
Chapter 2 part #3 C++ Input / Output
ifstreams and ofstreams
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
Chapter 1 c++ structure C++ Input / Output
Reading from and Writing to Files
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

From lab 1 Using cs lab machines (including remote) Function prototype vs function definition Submitting to mycourses Naming convention: Ass1_lastName_Firstname.tar.gz no unneeded files

Any problems using zylabs?

Week 2 - goals File i/o in c++ Decomposing a program using a structure/hierarchy chart Style standards for cs240 programs The run-time stack String and vector classes

http://www.cplusplus.com/reference/iolibrary/

<ios> <istream> <iostream> <fstream> <sstream> ifstream istream ios_base istringstream cin iostream ostream ostringstream ios cout

File I/O <fstream> contains definitions for 2 classes Ifstream and ofstream Programmer creates an object and connects the object to an OS file Ifstream myinfile (“inputDataFile”); - must be an existing file Ifstream myfile; //read the filename into a string variable named filename myfile.open(filename.c_str()); - why not just filename? Ofstream myoutfile(“out_put_file_name”); - will erase an existing file Making sure file was successfully open If(myfile.isopen()) ---- If(Myfile) ----- Myfile.close(); breaks the connection and frees resources being used Myfile.eof(); returns true if end_of_file has been reached

Extraction (>>) vs. getline() Both read characters from an input stream (terminal or file) >> skips over any white space characters and then extracts all characters up to but not including the next white space character Getline(cin, line); reads all characters up to the next newline character and disposes of the newline character

consider You have to read from a text file that contains information about a list of contacts Each line of the file contains A name (first name only) A cell phone number (10 digits) (optionally) a home phone number (10 digits) You can assume that the input file is correctly formatted

Use extraction (>>) or getline? Sample file: joe 6647385182 3482940582 idil 9948305721 can 7745839280 8834639632

Istringstream class Inherits from istream so has all the methods that cin or an ifstream object has Constructors Istringstream instream(); Istringstream instream(str); Some methods Void str(const string & s); s becomes the contents of the istringstream Bool eof(); returns true if end-of-file was reached by last input operation

Using the istringstream class you can: read a line from a file or cin using getline create an istringstream object from the string extract “tokens” from the istringstream

Zybook assignment Before 11:30 on Thursday Jan 25 complete non optional exercises from Ch. 8 Strings Ch. 9 arrays and vectors

Hierarchy chart documenting Functional decomposition of Lab 1 program main findAverage displayResult getNumber doubleLargest

Adding communication main findAverage displayResult getNumber int int int double double findAverage displayResult getNumber doubleLargest terminal terminal

a message is written to the standard error device and abort is called, terminating the program execution. Phases of development Specification what is the program supposed to do? Design decompose into functions and/or classes Implementation implement the components testing does the program meet its specification? Maintenance make changes after program is in use

A problem to solve Write a C++ program that prompts the user for the name of a text file, opens that file, reads the file and stores the lines of the file in a vector of strings.  The program should output the number of non-blank lines in the file, the average number of words per non-blank line and the average number of alphabetic characters per word.

The big problem text file program number of non-blank lines avg words per non-blank line avg alphas per word program

understanding the problem Any questions?

Some questions What is a word? What is a non-blank line? A sequence of non-white space characters containing at least one alpha character What is a non-blank line? Contains at least one non-white space character What is an alpha character? Upper or lower case A through Z

Create some test cases Helps to understand the problem Use after program is written Correct answer is known before executing the program

number of non-blank lines avg words per non-blank line This is a test case How many non-blank lines are there? Average words/non-blank line? Average alphas / word? number of non-blank lines avg words per non-blank line avg alphas per word

Some other test cases?? number of non-blank lines: 4 This is a test case How many non-blank lines are there? Average words/non-blank line? Average alphas / word? number of non-blank lines: 4 avg words per non-blank line: 4.25 avg alphas per word: 4.88 Some other test cases??

Design involves decomposing big problem into smaller problems Divide and conquer What are some smaller problems?

Some smaller problems Open the input file Display the results Read from the input file into a vector Is a line blank? Number of words in a line? Number of alphas in a word?

Hierarchy chart as a design tool Use hierarchy chart to develop functional decomposition Each box represents a function that solves one piece of the problem Communication with its caller shown External I/O shown

main

OpenFile

Incremental development Bottom up Implement and test individual functions Top down Write main using stubs for lower level functions

Zybook assignment Before 11:30 on Tuesday Jan 30 complete non optional exercises from Ch. 10 (sec 1 to 7) classes and objects

assignments How to submit Grading criteria See course information page See assignment Assignment 1

Cs240c style standards Decompose program into functions Main consists of calls to other functions Each function has a single job to do Organize programs as shown in class Document each function prototype Use meaningful names for variables, parameters, functions, classes, etc. One statement per line Indentation must follow program logic Use blank lines and spaces for readability Appropriately label all output Avoid wrap around when printed Use break only in a switch statement Do not use global variables See Chapter 6.3 of zyBook

Who are the readers of your code?

some readers of your code compiler Grader You Team members (for team projects) maintainers

Quiz 1 Given: string str1; string str2 = “Cs240c”; Length(str1) returns: ____ Str2.at(3) returns: ____ Strings can be compared using relational operators (true or false) str2.insert(3,”-”) makes the value of str2: _________ Str2.find(‘c’) returns: _____ Myvector[i] and myvector.at(i) have the same behavior (true or false) given: vector<int> nums; nums.size() returns: ____ given: vector<char> chars (26); chars.push_back(‘.’); chars.size() returns: ______

Given: string str1; string str2 = “Cs240c”; Length(str1) returns: _0___ Str2.at(3) returns: _4___ Strings can be compared using relational operators (true) str2.insert(3,”-”) makes the value of str2: _CS2-40c__ Str2.find(‘c’) returns: __0___ Myvector[i] and myvector.at(i) have the same behavior (false) given: vector<int> nums; nums.size() returns: _0___ given: vector<char> chars (26); chars.push_back(‘.’); chars.size() returns: __27____

Where can you find a list of all of the methods of the string or the vector class?

Type char What is the difference between ‘c’ and “c” ? See <cctype> for useful functions dealing with the char Also covered in zybook 8.3

vector<T> One of several container classes provided by the c++ standard Template Library (STL) Is a template class – pattern used to instantiate a real class (instance) Vector<string> words; Vector<int> numbers; Vector<myclass> list; Vectors contain a sequence of elements that occupy positions 0 to size-1 Elements can be added to, removed from or accessed by position

How is a vector different from a c or C++ array?

Some differences Vector is a template class A vector knows its own size Size can grow and shrink as needed Has a size and a capacity Has many useful methods Provides element access with range checking Can be used with many functions from the <algorithm> library Ex: Sort, find, for_each, copy, max_element