Programming Taskbook


E-mail:

Password:

User registration   Restore password

Russian

SFedU

1100 training tasks on programming

©  M. E. Abramyan (Southern Federal University), 1998–2018

 

Tasks | Task groups | File

PrevNext


Binary files

The binary file is a file that contains elements of the same type in the binary format.

In Pascal, such files are called typed files and should be declared as file of <element type>.

In Visual Basic, such files are called random access files and should be declared with the Random attribute.

In C++, binary files should be opened in the ios_base::binary mode; the read and write operations for binary files are implemented by the read and write methods with the ((char *)&x, sizeof(x)) parameters, where the x variable has a type that matches the type of file elements.

In .NET languages, binary files should be processed with using the FileStream, BinaryReader, and BinaryWriter classes.

In Python and Ruby, binary files should be opened with the b attribute of the file access mode, for example, "rb" provides for opening existing binary file in read mode, "wb" provides for creation binary file and opening this file in write mode, "r+b" — provides for opening existing binary file in read and write mode. Use the unpack and pack methods while processing files with data of numerical types (in Python, these methods are defined in the struct module).

In Java, binary files should be processed with using the RandomAccessFile class.

The condition "A file of integers (real numbers, characters, strings) is given" means that the file name is given (as a string input data) and the corresponding file exists and is located in the working directory. In some tasks it is necessary to check the file existence; this task condition is pointed out explicitly. File components must be read using standard input procedures (or functions) of the programming language being used.

If the task requires to create a new file then the new file name is included in the input data set; this name is usually the last element of input data. Standard output procedures (or functions) of the programming language must be used for writing data to files.

The size of a typed file always means the amount of file components of the corresponding type. File components are assumed to be numbered beginning with 1.

The minimal size of any input file is assumed to be 2 (that is, any file contains at least two components), if the task does not specify it explicitly. The maximal file size is not fixed, so auxiliary arrays should not be used to store all file components but one may use temporary files.

Binary files: basic operations

File1. A string S is given. If the string S is an admissible name of file then create an empty file called S and output True. If a file with the name S cannot be created then output False.

File2. A file name and an integer N (> 1) are given. Create a file of integers with the given name and write N first positive even numbers (2, 4, …) to this file.

File3. A file name and two real numbers A, D are given. Create a file of real numbers with the given name and write 10 first terms of an arithmetic sequence with the first term A and the common difference D (A, A + D, A + 2·D, …, A + 9·D) to this file.

File4. Four file names are given. Find the amount of files with the given names that are located in the working directory.

File5. A name of file of integers is given. Find the amount of the file components. If a file with the given name does not exist then output −1.

File6. An integer K and a file of nonnegative integers are given. Output the file component with the order number K (file components are numbered beginning with 1). If the file does not contain a component with the required number then output −1.

File7. Given a file of integers that contains at least four components, output its components with the order numbers 1, 2, N−1, N, where N is the amount of the file components.

File8. Two names of files of real numbers are given. It is known that the first file exists and is nonempty whereas the second file is absent in the working directory. Create the absent file and write the first and the last components of the existing file (in that order) to the file that has been created.

File9. Two names of files of real numbers are given. It is known that one of these files exists and is nonempty whereas the other file is absent in the working directory. Create the absent file and write the last and the first components of the existing file (in that order) to the file that has been created.

File10. A file of integers is given. Create a new file that contains all components of the given file in inverse order.

File11. A file of real numbers is given. Create two new files; the first resulting file must contain the given file components with odd order numbers (1, 3, …), the second resulting file must contain the given file components with even order numbers (2, 4, …).

File12. A file of integers is given. Create two new files; the first resulting file must contain the given file components whose values are even numbers, the second resulting file must contain the given file components whose values are odd numbers. If the given file does not contain odd or even numbers then the corresponding resulting file must be empty.

File13. A file of integers is given. Create two new files; the first resulting file must contain the given file components (in inverse order) whose values are positive numbers, the second resulting file must contain the given file components (in inverse order too) whose values are negative numbers. If the given file does not contain positive or negative numbers then the corresponding resulting file must be empty.

File14. A file of real numbers is given. Find the average of its components.

File15. A file of real numbers is given. Find the sum of its components with even order numbers.

File16. A file of integers is given. Find the amount of series of equal numbers that are contained in the file (a series of equal numbers is defined as a set of successive file components with equal values). For instance, if the file contains components with values 1, 5, 5, 5, 4, 4, 5 then the number 4 must be output.

File17. A file of integers is given. Create a new file of integers that contains lengths of all series of equal numbers from the given file (a series of equal numbers is defined as a set of successive file components with equal values; the amount of these components is called the length of series). For instance, if the given file contains components with values 1, 5, 5, 5, 4, 4, 5 then the resulting file must contain the following integers: 1, 3, 2, 1.

File18. A file of real numbers is given. Find its first local minimum (a file component is called the local minimum if it is smaller than its neighbors).

File19. A file of real numbers is given. Find its last local maximum (a file component is called the local maximum if it is greater than its neighbors).

File20. A file of real numbers is given. Find the total amount of its local extremums, that is, its local maximums and local minimums (see the local minimum and local maximum definitions in File18 and File19 respectively).

File21. A file of real numbers is given. Create a file of integers that contains order numbers of all local maximums of the given file in ascending order (see the local maximum definition in File19).

File22. A file of real numbers is given. Create a file of integers that contains order numbers of all local extremums of the given file in descending order (see the local extremum definition in File20).

File23. A file of real numbers is given. Create a file of integers that contains lengths of all sets of successive file components whose values are in descending order. For instance, if the given file contains components with values 1.7, 4.5, 3.4, 2.2, 8.5, 1.2 then the resulting file must contain the following integers: 3, 2.

File24. A file of real numbers is given. Create a file of integers that contains lengths of all sets of successive file components whose values are in ascending or descending order. For instance, if the given file contains components with values 1.7, 4.5, 3.4, 2.2, 8.5, 1.2 then the resulting file must contain the following integers: 2, 3, 2, 2.

File25. Given a file of real numbers, replace the values of all file components with their squares.

File26. Given a file of real numbers, exchange values of its minimal and maximal component.

File27. Given a file of integers with components A1, A2, …, AN (N is the amount of the file components), change the order of the file components as follows:

A1,    AN,    A2,    AN−1,    A3,    … .

File28. Given a file of real numbers, replace each file component (except for the first and last one) with the average of this component and its neighbors.

File29. Given a file of integers that contains more than 50 components, reduce the size of file to 50 components by means of removing last components.

File30. Given a file of integers that contains an even number of components, remove the second half of the file components.

File31. Given a file of integers that contains more than 50 components, reduce the size of file to 50 components by means of removing first components.

File32. Given a file of integers that contains an even number of components, remove the first half of the file components.

File33. Given a file of integers, remove file components with even order numbers (2, 4, …).

File34. Given a file of integers, remove file components with negative values.

File35. Given a file of integers that contains less than 50 components, increase the size of file up to 50 components by means of inserting zero components to the beginning of the file.

File36. Given a file of integers, write all its components in the same order to the end of this file (as a result, the size of file will be doubled).

File37. Given a file of integers, write all its components in inverse order to the end of this file (as a result, the size of file will be doubled).

File38. Given a file of integers, double occurrences of file components with odd order numbers.

File39. Given a file of integers, double occurrences of file components whose values are in the range 5 to 10.

File40. Given a file of integers, replace each file component, whose order number is an even number, with two zero components.

File41. Given a file of integers, replace each file component, whose value is a positive number, with three zero components.

Untyped files processing

File42. Given two untyped files, exchange their contents.

File43. Given an untyped file and a string S, create the file copy called S.

File44. Given three untyped files of different size, replace the contents of the longest file with the contents of the shortest one.

File45. Given three untyped files of different size, replace the contents of the shortest file with the contents of the longest one.

File46. A string S0, an integer N (≤ 4), and N files called S1, …, SN are given. Components of all given files have the identical type. Combine the contents of the given files (in the same order) in a new file called S0.

File47. Given two files whose components have the identical type, add the initial contents of the second file to the end of the first file and the initial contents of the first file to the end of the second file.

Work with several numeric files. Archival files

File48. Three files of integers called SA, SB, SC and a string SD are given. All given files are of the same size. Create a new file called SD; this file must contain triples of components of the given files as follows:

A1,    B1,    C1,    A2,    B2,    C2,    … .

File49. Four files of integers called SA, SB, SC, SD and a string SE are given. The given files are of different size. Create a new file called SE; this file must contain quadruples of components of the given files as follows:

A1,    B1,    C1,    D1,    A2,    B2,    C2,    D2,    … .

Do not write "superfluous" components of longer files to the resulting file.

File50. Two files of real numbers called S1, S2 and a string S3 are given. Values of components of each given file are in ascending order. Create a new file called S3; this file must contain all components of the given files in ascending order of their values.

File51. Three files of real numbers called S1, S2, S3 and a string S4 are given. Values of components of each given file are in descending order. Create a new file called S4; this file must contain all components of the given files in descending order of their values.

File52. A string S0, an integer N (≤ 4), and N files of integers called S1, …, SN are given. Create a new file called S0; this file (an archival file) must contain data of all given files in the following format: the number N (the first component of the archival file), the amounts of components of each given file (the next N components of the archival file), values of all components of the file S1 in the same order, values of all components of the file S2 in the same order, and so on.

File53. A string S, an integer N (> 0) and an archival file are given. The archival file contains data of several files; the format of archival file is described in File52. Restore the data of the N-th file from the archival file and save it in the file called S. If the amount of files, which are contained in the archival file, is less than N then the resulting file S must be empty.

File54. A string S and an archival file are given. The archival file contains data of several (no more than six) files; the format of archival file is described in File52. Find the average of all component values for each file, which is contained in the archival file. Save all obtained averages (as a real numbers) in a new file called S.

File55. A string S0, an integer N (≤ 4), and N files of integers called S1, …, SN are given. Create a new file called S0; this file (an archival file) must contain data of all given files in the following format: the amount of components of the file S1 and values of all its components in the same order; the amount of components of the file S2 and values of all its components in the same order; …; the amount of components of the file SN and values of all its components in the same order.

File56. A string S, an integer N (> 0) and an archival file are given. The archival file contains data of several files; the format of archival file is described in File55. Restore the data of the N-th file from the archival file and save it in the file called S. If the amount of files, which are contained in the archival file, is less than N then the resulting file S must be empty.

File57. Two strings S1, S2 and an archival file are given. The archival file contains data of several files; the format of archival file is described in File55. Create two new files called S1, S2; the first resulting file must contain first components of all files, which are contained in the archival file, the second resulting file must contain last components of all files, which are contained in the archival file.

Files of characters and files of strings

A file of characters is a binary file whose components are characters. A file of strings (or a string binary file) is a binary file whose components are strings of characters. Unlike text files, files of strings can be opened in read/write access mode (that is, allows both reading and writing), and their components can be accessed both sequentially and randomly (by moving the current file position to a specified component).

In Borland Delphi, Lazarus, and PascalABC.NET, variables for string binary files should be declared as file of ShortString.

In Visual Basic, the components of a file of characters are assumed to have the String * 1 type and the components of a string binary file are assumed to have the String * 80 type in any task of Programming Taskbook.

In C++, the components of a string binary file are assumed to have the char[80] type in any task of Programming Taskbook.

In .NET languages, Python, Java, and Ruby, string binary files are assumed to contain left-aligned strings of length 80 in any task of Programming Taskbook (therefore, you may need to use a method for removing right blank characters from the string after reading it from a string binary file and a method for padding string by blank characters before writing it to a string binary file).

In Java, you should use variables of type byte and byte[] to read/write characters and strings from/to binary files. Use appropriate constructor of the String class to convert bytes to string, use the getBytes method of the String class to convert string to bytes.

File58. Given a file of characters containing at least one blank character, remove its components that are located after the first blank character (including this blank character).

File59. Given a file of characters containing at least one blank character, remove its components that are located after the last blank character (including this blank character).

File60. Given a file of characters containing at least one blank character, remove its components that are located before the first blank character (including this blank character).

File61. Given a file of characters containing at least one blank character, remove its components that are located before the last blank character (including this blank character).

File62. Given a file of characters, rearrange file components in ascending order of their numeric values in the character set.

File63. An integer K (> 0) and a file of strings are given. Create two new files; components of the first resulting file are strings that contain first K characters of each string of the given file; components of the second resulting file are K-th characters of each string of the given file. If the length of some string of the given file is less than K then the entire string and a blank character must be written to the first and second resulting file respectively.

File64. Given a file of strings, write its components of the minimal length to a new file (in the same order).

File65. Given a file of strings, write its components of the maximal length to a new file (in inverse order).

File66. Given a file of strings, write all its components to a new file in lexicographic order (that is, in ascending order of the numeric values of their characters).

File67. A file of strings is given. The file contains dates in the "day/month/year" format, the "day" and "month" fields contain two digits, the "year" field contains four digits (for example, "16/04/2001"). Create two new files and write integer values of days and months for each date from the given file to the first and second resulting file respectively (in the same order).

File68. A file of strings is given; the file contains dates in the "day/month/year" format (see File67). Create two new files and write integer values of months and years for each date from the given file to the first and second resulting file respectively (in inverse order).

File69. A file of strings is given; the file contains dates in the "day/month/year" format (see File67). Write its components that corresponds to summer dates to a new file (in the same order). If the given file does not contain required dates then the resulting file must be empty.

File70. A file of strings is given; the file contains dates in the "day/month/year" format (see File67). Write its components that corresponds to winter dates to a new file (in the same order). If the given file does not contain required dates then the resulting file must be empty.

File71. A file of strings is given; the file contains dates in the "day/month/year" format (see File67). Find its component that represents the earliest spring date. If the given file does not contain spring dates then output an empty string.

File72. A file of strings is given; the file contains dates in the "day/month/year" format (see File67). Find its component that represents the latest autumn date. If the given file does not contain autumn dates then output an empty string.

File73. A file of strings is given; the file contains dates in the "day/month/year" format (see File67). Write all its components to a new file in descending order of their date values.

Files containing matrices

An M × N matrix is a rectangular table of numbers consisting of M horizontal rows and N vertical columns. As a rule, two-dimensional array are used to store matrix elements (see tasks of the Matrix group). But matrix elements can be stored in binary files of real numbers too. The files of such a "matrix" structure are considered in this subsection. As well as in other tasks of the File group, additional arrays should not be used to store all file components.

The tasks of this subsection use some notions of matrix theory. Let us recall the definitions of these notions.

If A is an M × N matrix then the matrix B of order N × M formed from A by interchanging its rows with its columns is called the transpose of A:

BI,J = AJ,I,        I = 1, …, N,    J = 1, …, M.

Let A be an M × N matrix and B be an N × P matrix. An M × P matrix C is called the product of A and B (and is denoted by A·B) if its elements satisfy the following relations:

CI,J = AI,1·B1,J + AI,2·B2,J + … + A1,N·BN,J,        I = 1, …, M,    J = 1, …, P.

A square matrix A is called an upper triangular matrix if all its elements below the main diagonal are equal to zero (see the main diagonal definition in the task Matrix80):

AI,J = 0,        I > J.

A square matrix A is called a lower triangular matrix if all its elements above the main diagonal are equal to zero:

AI,J = 0,        I < J.

A square matrix A is called a tridiagonal matrix if all its elements that are off the main diagonal and two adjacent diagonals are equal to zero:

AI,J = 0,        |I − J| > 1.

File74. Two integers I, J and a file of real numbers are given. This file contains elements of square matrix (by rows). Output the value of the matrix element in the I-th row and J-th column (rows and columns are numbered beginning with 1). If the given matrix does not contain the required row or column then output 0 (as a real number).

File75. A file of real numbers is given. This file contains elements of square matrix (by rows). Create a new file that contains the transpose of the given matrix.

File76. Two files of real numbers called SA and SB are given. These files contain elements of square matrix A and B (by rows). Create a new file called SC that contains elements of the matrix product A·B. If matrices A and B cannot be multiplied then the resulting file SC must be empty.

File77. Two integers I, J and a file of real numbers are given. The first component of the file contains the amount of matrix columns, other file components contain matrix elements (by rows). Output the value of the matrix element in the I-th row and J-th column (rows and columns are numbered beginning with 1). If the given matrix does not contain the required row or column then output 0 (as a real number).

File78. A file of real numbers is given. The first component of the file contains the amount of matrix columns, other file components contain matrix elements (by rows). Create a new file that contains the transpose of the given matrix. The resulting file must have the same structure as the given file.

File79. Two files of real numbers called SA and SB are given. The first components of the files SA and SB contain the amount of columns of matrices A and B respectively, other file components contain elements of matrices A and B (by rows). Create a new file called SC that contains the matrix product A·B (the resulting file must have the same structure as the given files). If matrices A and B cannot be multiplied then the resulting file must be empty.

File80. A file of real numbers is given. This file contains elements of an upper triangular matrix (by rows). Create a new file that contains elements of nonzero part of the given matrix (by rows).

File81. A file of real numbers is given. This file contains elements of a lower triangular matrix (by rows). Create a new file that contains elements of nonzero part of the given matrix (by rows).

File82. A file of real numbers is given. This file contains elements of a tridiagonal matrix (by rows). Create a new file that contains elements of nonzero part of the given matrix (by rows).

File83. Two integers I, J and a file of real numbers are given. This file contains elements of nonzero part of an upper triangular matrix (by rows). Output the matrix order and the value of the matrix element in the I-th row and J-th column (rows and columns are numbered beginning with 1). If the required element lies in zero part of the matrix then output 0 (as a real number). If the given matrix does not contain the required row or column then output −1 (as a real number).

File84. Two integers I, J and a file of real numbers are given. This file contains elements of nonzero part of a lower triangular matrix (by rows). Output the matrix order and the value of the matrix element in the I-th row and J-th column (rows and columns are numbered beginning with 1). If the required element lies in zero part of the matrix then output 0 (as a real number). If the given matrix does not contain the required row or column then output −1 (as a real number).

File85. Two integers I, J and a file of real numbers are given. This file contains elements of nonzero part of a tridiagonal matrix (by rows). Output the matrix order and the value of the matrix element in the I-th row and J-th column (rows and columns are numbered beginning with 1). If the required element lies in zero part of the matrix then output 0 (as a real number). If the given matrix does not contain the required row or column then output −1 (as a real number).

File86. A file of real numbers is given. This file contains elements of nonzero part of an upper triangular matrix (by rows). Create a new file that contains all elements of the given matrix (by rows).

File87. A file of real numbers is given. This file contains elements of nonzero part of a lower triangular matrix (by rows). Create a new file that contains all elements of the given matrix (by rows).

File88. A file of real numbers is given. This file contains elements of nonzero part of a tridiagonal matrix (by rows). Create a new file that contains all elements of the given matrix (by rows).

File89. Two files of real numbers called SA and SB are given. These files contain nonzero parts of upper triangular matrices A and B (by rows). Create a new file called SC that contains nonzero part of the matrix product A·B (by rows). If matrices A and B cannot be multiplied then the resulting file must be empty.

File90. Two files of real numbers called SA and SB are given. These files contain nonzero parts of lower triangular matrices A and B (by rows). Create a new file called SC that contains nonzero part of the matrix product A·B (by rows). If matrices A and B cannot be multiplied then the resulting file must be empty.


PrevNext

 

  @Mail.ru

Designed by
M. E. Abramyan and V. N. Braguilevsky

Last revised:
06.05.2018