Presentation is loading. Please wait.

Presentation is loading. Please wait.

From lab 1 Using cs lab machines (including remote)

Similar presentations


Presentation on theme: "From lab 1 Using cs lab machines (including remote)"— Presentation transcript:

1 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

2 Any problems using zylabs?

3 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

4

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

6 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

7 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

8 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

9 Use extraction (>>) or getline?
Sample file: joe idil can

10 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

11 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

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

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

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

15 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

16 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.

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

18 understanding the problem
Any questions?

19 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

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

21 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

22 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??

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

24 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?

25 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

26 main

27 OpenFile

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

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

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

31 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

32 Who are the readers of your code?

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

34 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: ______

35 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____

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

37 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

38 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

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

40 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


Download ppt "From lab 1 Using cs lab machines (including remote)"

Similar presentations


Ads by Google