Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "I/O Streams File I/O 2-D array review"— Presentation transcript:

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

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

3 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!

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

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

6 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

7

8

9 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

10

11

12 Questions about file streams?

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

14 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

15 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!

16 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

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

18 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}};

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 Feel free to ask any questions after class!


Download ppt "I/O Streams File I/O 2-D array review"

Similar presentations


Ads by Google