EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019

Slides:



Advertisements
Similar presentations
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
Advertisements

CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
ECE Application Programming
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
ECE Application Programming
Bill Tucker Austin Community College COSC 1315
Hank Childs, University of Oregon
Chapter 12 Classes and Abstraction
ECE Application Programming
EECE.2160 ECE Application Programming
ECE Application Programming
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
EECE.2160 ECE Application Programming
ECE Application Programming
COMP 2710 Software Construction File I/O
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
EECE.2160 ECE Application Programming
File I/O with Records Lesson xx
Today’s Lecture I/O Streams Tools for File I/O
EECE.2160 ECE Application Programming
Chapter 3 Input output.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
CHAPTER 4 File Processing.
Arrays Arrays A few types Structures of related data items
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Reading from and Writing to Files
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
COMS 261 Computer Science I
Algorithmic complexity
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Instructor: Dr. Michael Geiger Spring 2019 Lecture 6: Strings
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Announcements Exam 2 Lecture Grades Posted on blackboard
EECE.2160 ECE Application Programming
Reading from and Writing to Files
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019 Lecture 3: Structures in C++

Announcements/reminders Program 1 due date TBD (within 2 weeks or so) All programs to be submitted via Blackboard Good C++ reference site: http://www.cplusplus.com/ Basic tutorials + in-depth reference pages Relevant tutorials under “Reading” on schedule page Shubham Tikare assigned as grader for course Will announce his office hours ASAP Poll to be posted to schedule Exam 1 & 2 Final exam time set: Wednesday, 5/8, 3-6 PM 6/6/2019 Data Structures: Lecture 3

Data Structures: Lecture 3 Lecture outline Review Basic C++ program structure I/O in C++ Structs in C++ Working with multiple files 6/6/2019 Data Structures: Lecture 3

Review: basic program structure Covered basic C++ program structure—very similar to C! #include directive: add libraries to program <iostream>: basic input/output Namespaces Introduced std namespace—includes cin, cout, etc. Could include entire namespace: using namespace std; Or, just include members being used: using std::cout; Using statements directly after relevant #include 6/6/2019 Data Structures: Lecture 3

Review: Hello World! in C++ #include <iostream> // C++ input/output library using namespace std; int main() { cout << "Hello World!\n"; return 0; } 6/6/2019 Data Structures: Lecture 3

Data Structures: Lecture 3 Review: Basic I/O Output (cout) streams Can output multiple values in same statement cout << "x= " << x << ", y=" << y << endl; Recall endl ≈ ’\n’ + flush output stream Input (cin) streams Use cin to read values into variables E.g., cin >> x; Skips whitespace characters Can cause problems if input doesn’t match variable type 6/6/2019 Data Structures: Lecture 3

Data Structures: Lecture 3 Review: File I/O File stream objects Must include <fstream> ifstream (input) or ofstream (output) Have methods for file open/close File I/O syntax similar to standard I/O Replace cin/cout with appropriate stream object Same operators used: << >> 6/6/2019 Data Structures: Lecture 3

Data Structures: Lecture 3 File I/O example #include <fstream> #include <iostream> using std::ifstream; using std::ofstream; int main() { int x, y; ifstream infile; ofstream outfile; // Open input, output files infile.open("f1.txt"); outfile.open("f2.txt"); // Read integer values, then reprint to output infile >> x >> y; outfile << "x = " << x << ", y = " << y << endl; return 0; } 6/6/2019 Data Structures: Lecture 3

Review: Structures in C User-defined types; example: typedef struct { char first[50]; char middle; char last[50]; unsigned int ID; double GPA; } StudentInfo; Can define variables of that type Scalar: StudentInfo student1; Array: StudentInfo classList[10]; Pointer: StudentInfo *sPtr; Access members using Dot operator: student1.middle = ‘J’; Arrow (if pointers): sPtr->GPA = 3.5; Typically passed to functions by address 6/6/2019 Data Structures: Lecture 3

Data Structures: Lecture 3 Structures in C++ 99% the same as in C Only (visible) difference: typedef isn’t necessary Could rewrite previous structure definition as: struct StudentInfo { char first[50]; char middle; char last[50]; unsigned int ID; double GPA; }; Declarations shown in previous slide still work You can use StudentInfo as type name In C, type would be struct StudentInfo 6/6/2019 Data Structures: Lecture 3

Data Structures: Lecture 3 Structure example #include <iostream> using namespace std; struct Point { double x; double y; }; int main() { Point p1 = {3, 5}; Point p2 = {7.8, 9.1}; cout << "p1: (" << p1.x << ", " << p1.y << ")\n"; cout << "p2: (" << p2.x << ", " << p2.y << ")\n"; // Assume user enters -1 1 cout << "Enter new x, y for p1: "; cin >> p1.x >> p1.y; p2 = p1; p2.x -= 1.2; p1.y *= 2; cout << "p1: (" << p1.x << ", " << p1.y << ")\n"; cout << "p2: (" << p2.x << ", " << p2.y << ")\n"; return 0; } 6/6/2019 Data Structures: Lecture 3

Example solution (user input underlined) Enter new x, y for p1: -1 1 p1: (-1, 2) p2: (-2.2, 1) 6/6/2019 Data Structures: Lecture 3

Data Structures: Lecture 3 Nested structures Structures can contain other structures: struct Name { char first[50]; // First name char middle; // Middle initial char last[50]; // Last name }; struct SINew { Name sname; // Student name unsigned int ID; // ID # double GPA; // Grade point Will need multiple dot operators to access field within nested structure Given SINew s1; s1.sname  Name structure within s1 s1.sname.middle  middle initial of name within s1 6/6/2019 Data Structures: Lecture 3

Typical file usage with structures Each structure has its own .h/.cpp files .h file (i.e., Name.h, SINew.h) contains structure definition and prototypes of related functions .cpp file (i.e., Name.cpp, SINew.cpp) contains function definitions To use struct type and functions, include .h file SINew.h would include Name.h Allows Name variable inside SINew structures Main program could include both … … although that’s redundant—including SINew.h implicitly includes Name.h Will organize classes similarly 6/6/2019 Data Structures: Lecture 3

Avoiding multiple inclusion of .h files Say we have two files that start as shown: file1.h: #include "file2.h" ... myprogram.cpp: #include "file1.h" What’s the problem? What does an #include directive really do? 6/6/2019 Data Structures: Lecture 3

Avoiding multiple inclusion of .h files Anything in file2.h is included twice #include directive copies/pastes contents of file Compiler will think program redefines structures, function prototypes, etc. Solution: conditional compilation Header files include header guard code to ensure they’re only compiled once Example code for fictional file2.h: #ifndef file2_h // Start of file #define file2_h ... #endif // End of file Code between #ifndef & #endif only compiled once 6/6/2019 Data Structures: Lecture 3

Data Structures: Lecture 3 Final notes Next time: more on going from C to C++ Functions Argument passing in C++ (Maybe) I/O manipulators for output formatting Discussion of Program 1 requirements Reminders: Program 1 due date TBD (within 2 weeks or so) All programs to be submitted via Blackboard 6/6/2019 Data Structures: Lecture 3