CS 1337- Files and Arrays

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/51

encourage image

There's no tags or description

Looks like no tags are added yet.

52 Terms

1
New cards

Three main steps of file processing

  1. Open the file\n2. Read/write data\n3. Close the file


2
New cards

Header file required for file processing in C++

#include <fstream>

3
New cards

Comparison of ifstream, ofstream, and fstream in C++

  • ifstream: Input from file\n- ofstream: Output to file\n- fstream: Input and output


4
New cards

Syntax for declaring input and output file stream objects in C++

ifstream infile;\nofstream outfile;

5
New cards

Function of the open() stream method in C++

Establishes a connection between the program and a file on disk.

6
New cards

Syntax to open a file named inventory.dat for reading in C++

infile.open("inventory.dat");

7
New cards

Syntax to open a file named report.txt for writing in C++

outfile.open("report.txt");

8
New cards

Result of opening a non-existent file for reading using ifstream

The file stream fails to open and enters an error state.

9
New cards

Effect of opening an existing file with default ofstream

The existing file contents are erased (truncated) before writing new data.

10
New cards

Stream operator used to write data to a file in C++

<< (insertion operator)

11
New cards

Resulting file content after executing: ofstream outputFile; outputFile.open("demo.txt"); outputFile << "I love C++" << endl;

A file named demo.txt containing the text: I love C++

12
New cards

Requirements for the Random Number File Assignment

Generate 100100 random numbers ranging from 3030 to 8080, writing 1010 numbers per line to a file.

13
New cards

Requirements for the Count-The-Numbers Assignment

Count total integers in a file, print numbers from 11 to count−10\text{count} - 10, and display 2020 numbers per line.

14
New cards

Requirements for the Read/Write Assignment

Read 1010 integers, calculate their average, minimum, and maximum, and write the output to a file.

15
New cards

Requirements for the Paragraph Analysis Assignment

Read a paragraph from a file and count the total characters, words, and sentences.

16
New cards

Definition of an array

A data structure that stores multiple values of the same data type in contiguous memory.

17
New cards

Fundamental rule that every array must follow

All elements stored in the array must be of the exact same data type.

18
New cards

Physical arrangement of array elements in memory

Elements are stored in adjacent (contiguous) memory locations.

19
New cards

C++ syntax to declare an integer array named tests with 5 elements

int tests[5];

20
New cards

Maximum capacity of elements in int tests[5];

55 integer elements

21
New cards

Difference between declaring int tests[5]; vs int x;

tests stores 55 integers in contiguous memory, whereas x stores a single integer value.

22
New cards

Memory allocation produced by int tests[5];

Allocates memory for 55 consecutive integer locations.

23
New cards

Total element count in int numbers[12];

1212 elements

24
New cards

Total element count in double values[25];

2525 elements

25
New cards

Error present in: int data[3]; data[0] = 10; data[1] = "Hello";

Type mismatch error: string "Hello" cannot be stored in an array declared for integers.

26
New cards

Primary practical benefit of using arrays

Enables storing and managing many related values under a single variable name.

27
New cards

Can a standard C++ array store a mix of different data types?

No, every element in an array must share the exact same data type.

28
New cards

Data structure that stores elements of a single type in sequential memory addresses

An array

29
New cards

Two defining properties of an array

  1. Stores multiple values\n2. All values must share the same data type
30
New cards

Definition of parallel arrays

Two or more arrays of equal size that store related properties of items using matching index values.

31
New cards

Data fields stored in the parallel array example

Student IDs, Names, Courses, and Grades

32
New cards

Method used to connect related records across parallel arrays

Elements sharing the same index across different arrays refer to the same entity.

33
New cards

Array location of John's grade if his name is stored in name[2]

grade[2]

34
New cards

Array location of a student's course if their student ID is in id[4]

course[4]

35
New cards

Main reason to use parallel arrays

To link different pieces of information about an entity without defining custom structures or classes.

36
New cards

Entity represented by index 0 in a set of parallel arrays

The first item or record across all parallel arrays.

37
New cards

Most critical rule for maintaining parallel arrays

Matching information for an entity must always remain at the same index across all arrays.

38
New cards

Definition of a two-dimensional array

An array structure organized into a grid of rows and columns.

39
New cards

Real-world structure analogous to a 2D array

A table or spreadsheet.

40
New cards

C++ syntax to declare a 2D integer array with 4 rows and 3 columns

const int ROWS = 4; const int COLS = 3; int exams[ROWS][COLS];

41
New cards

Dimension represented by the first size parameter in a 2D array declaration

The number of rows.

42
New cards

Dimension represented by the second size parameter in a 2D array declaration

The number of columns.

43
New cards

Total element count in int exams[4][3];

4×3=124 \times 3 = 12 elements

44
New cards

General syntax for indexing an element in a 2D array

arrayName[rowIndex][columnIndex]

45
New cards

Action performed by the statement exams[2][2] = 86;

Assigns the value 8686 to the cell at row index 2 and column index 2.

46
New cards

Location accessed by scores[1][3]

Row index 1, Column index 3

47
New cards

Location accessed by scores[2][0]

Row index 2, Column index 0

48
New cards

Action performed by the statement exams[1][0] = 50;

Updates the element at row index 1, column index 0 to 5050.

49
New cards

Can a 2D array combine string and integer elements?

No, all elements in a 2D array must share the exact same data type.

50
New cards

Why player names and numeric stats cannot coexist in one numeric 2D array

Names are strings and stats are numeric; arrays mandate uniform data types.

51
New cards

Can player statistics alone be stored in a 2D array?

Yes, because all numeric statistics share the same data type.

52
New cards

Condition for choosing a 2D array over parallel arrays

When data naturally forms a uniform grid where every row and column holds the exact same data type.