I/O Streams File I/O 2-D array review

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

CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 4 Programming with Data Files.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
CSE202: Lecture 16The Ohio State University1 Two Dimensional Arrays.
C++ Coding and Compiling. Why QT/C++ Over Java?  Java is easier than C++  Java has built in GUIs  Java is multi-platform  C/C++ is more used in industry.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays.
Module 1: Array ITEI222 - Advance Programming Language.
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.
Chapter 8 Writing Generic Functions. Objectives Understand the use of generic functions. Learn about the use of templates, their advantages and pitfalls.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
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.
EECS 183 Discussion #10: I’m actually Alex Trebek. November 15th, 2016
Working with Batches of Data
The Ohio State University
Repetitive Structures
Pseudocode Key Revision Points.
Computing and Statistical Data Analysis Lecture 2
EGR 2261 Unit 10 Two-dimensional Arrays
Computing and Statistical Data Analysis Lecture 2
Two Dimensional Array Mr. Jacobs.
Two-Dimensional Arrays Lesson xx
EECs 183 Discussion 10 Hannah Westra.
Dynamic Array Multidimensional Array Matric Operation with Array
CS150 Introduction to Computer Science 1
Multi-dimensional Array
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:
Quiz Next Monday.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
File I/O.
Some Basics for Problem Analysis and Solutions
Some Basics for Problem Analysis
One-Dimensional Array Introduction Lesson xx
Additional Control Structures
Chapter 13 Vector of Vectors (2D Arrays)
Arrays Kingdom of Saudi Arabia
1020: Introduction to Programming Mohamed Shehata November 22, 2017
1020: Introduction to Programming Mohamed Shehata November 7, 2016
Programming with Data Files
C++ Statements and Functions
Chapter 3: Input/Output
Standard Input/Output Stream
Arrays.
CS150 Introduction to Computer Science 1
Multidimensional Arrays
File I/O in C++ I.
From C to C++: Summary of weeks 1 - 4
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
EECE.2160 ECE Application Programming
Arrays Arrays A few types Structures of related data items
CS150 Introduction to Computer Science 1
Programming with Data Files
(Dreaded) Quiz 2 Next Monday.
EECE.2160 ECE Application Programming
CS31 Discussion 1D Winter19: week 4
CS31 Discussion 1H Fall18: week 6
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lecture 3 More on Flow Control, More on Functions,
File I/O in C++ I.
Presentation transcript:

I/O Streams File I/O 2-D array review Discussion 7 I/O Streams File I/O 2-D array review

Discussion Plan File Input/Output (File I/O) 1-D and 2-D array review Practice problems

Projects going forward For projects 3 and 4 you can work with a partner! After that, there is the Final Project, which is in groups of 4 Start thinking now about people you would want to work with A good group can make the project experience better!

File Streams Up until now you’ve needed to #include <iostream>. Why?

File Streams Up until now you’ve needed to #include <iostream>. Why? To be able to use the input stream cin and the output stream cout What is <fstream>, and why do we include it?

File Streams Up until now you’ve needed to #include <iostream>. Why? To be able to use the input stream cin and the output stream cout What is <fstream>, and why do we include it? If we want to use a file stream and read in data/input from a file, or write data (output) to a file

Careful with input streams Do NOT use cin.eof() or my_ifstream.eof() as a condition Can sometimes cause undefined behavior INSTEAD: while (inFile >> x){…} // while reading into the file is still successful while (!inFile.fail()){…} //while reading into the file does not cause a fail state You can use cin.clear() or inFile.clear() or outFile.clear() to reset a fail state Where “outFile” is the variable name of your ostream A useful function to do this all was provided in lecture Maybe add a screenshot of this function

Questions about file streams?

Moving to review of 2D Arrays Any questions about 1-Dimensional arrays?

2 Dimensional Arrays You can visualize these like a matrix, or game board! ALWAYS: arr[row][col] You’ll often see ‘col’ in place of column col stands for column

Initializing all to 0 1-dimensional array: int board[3] = {0}; Both of these initialize every element in the array to 0 THIS IS ONLY THE CASE WITH 0 – if any other number is between the brackets, it sets only the first element to that number, and sets all the rest to zero This isnt true if you put a number into the brackets – only the first element will be set Feel free to try it to see exactly what happens!

Initializing directly const int HEIGHT = 3; const int WIDTH = 4; int board[HEIGHT][WIDTH] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10 , 11, 12} }; You can also initialize all to the same thing, using nested loops We’ll do a lot with nested loops in a few minutes

When initializing… Which of these is invalid? int data[10][2]; int data [4][]; int data [][3] = {{1, 2, 3}, {4, 5, 6}};

When initializing… Which of these is invalid? int data[10][2]; int data [4][]; //compile error int data [][3] = {{1, 2, 3}, {4, 5, 6}};

When initializing… int data[10][2]; int data [4][]; //compile error With a 2-dimensional array, the compiler absolutely needs to know the column (the second parameter) size at compile time The row is optional

How do we declare and initialize our own 2-d array Let’s say we want the values to increase: 1, 2, 3, 4, 5, 6, 7, 8, 9 Do on board first, try only student input

How do we declare and initialize our own 2-d array Let’s say we want the values to increase: 1, 2, 3, 4, 5, 6, 7, 8, 9 int my_arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; You can see how you can write the code to be easy to understand visually You will have done this for Assignment 3 and for project3

How do we iterate through a 2-d array What if we want to print every element, as in the picture? Do on the board with only student input

How do we iterate through a 2-d array What if we want to print every element, as in the picture. We print each element in each row, and start a new line for each row so it looks like a matrix. for (int i = 0; i < 3; ++i){ for (int j = 0; j < 3; ++j){ cout << my_arr[i][j] << " "; } cout << endl; Do on the board with only student input

How do we iterate through a 2-d array Now what if we want to print the values backwards, keeping the matrix form? int my_arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; Do on the board with only student input

How do we iterate through a 2-d array Now what if we want to print the values backwards, keeping the matrix form? int my_arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; for (int i = 2; i >= 0; --i){ for (int j = 2; j >= 0; --j){ cout << my_arr[i][j] << " "; } cout << endl; Do on the board with only student input

How do we iterate through a 2-d array Now what if we want to only reverse each row, not the entire array? int my_arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; Do on the board with only student input

How do we iterate through a 2-d array Now what if we want to only reverse each row, not the entire array? int my_arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; for (int i = 0; i < 3; ++i){ for (int j = 2; j >= 0; --j){ cout << my_arr[i][j] << " "; } cout << endl; Do on the board with only student input

How do we iterate through a 2-d array Now what if we want to only reverse each column, not the entire array? This is the same as doing what? (in terms of rows) int my_arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; Do on the board with only student input

How do we iterate through a 2-d array Now what if we want to only reverse each column, not the entire array? This is the same as doing what? (in terms of rows) Reversing the order of the rows int my_arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; Do on the board with only student input

How do we iterate through a 2-d array Now what if we want to only reverse each column, not the entire array? int my_arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; for (int i = 2; i >= 0; --i){ for (int j = 0; j < 3; ++j){ cout << my_arr[i][j] << " "; } cout << endl; Do on the board with only student input

2-dimensional arrays wrap up There are a lot of examples from last lecture – go through them in detail! Any questions? Understanding arrays is very important for project 3

Feel free to ask any questions after class!