S
S
Sergey Khlopov2018-01-31 23:28:50
Pascal
Sergey Khlopov, 2018-01-31 23:28:50

How to read matrices from a file?

Hello, tell me please, I have a text file, it contains, let's say, two matrices, above the matrix there is its dimension, 4 and 3. My task is such that the text file contains square matrices, integers, of order n, and I need to convert file, removing the elements of the secondary diagonal from each matrix. And I don't quite understand. We read here the first line "4" now 4 times 4 elements are read. And how to calculate the next matrix?

const N = 4;
var
  f1: text;
  j,i,k,l,endline : integer;
  
begin
assign(f1,'D:\in.txt');
reset(f1);
  while not Eoln(f1) do begin
    Readln(f1,endline);
    //Считываем 4 раза по 4 элемента
    inc(j);
  end;
  close(f1);
end.

After all, the next iteration will be 1, tell me please, thank you in advance for the answer.
4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
3
1 2 3
1 2 3
1 2 3

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Fedoryan, 2018-02-01
@Shlop

program ChangeMatrix;

const
  FileIn = 'C:\Users\admin\Documents\in.txt';
  FileOut = 'C:\Users\admin\Documents\out.txt';

var
  fin, fout: Text;
  n: integer;
  matrix: array of array of string;
  row, col: integer;
  src: string;
  target: array of string;
  counter: integer;

function Split(sep: string; source: string): array of string;
var
  i: integer;
  position: integer;
begin
  result := Nil;
  SetLength(result, n);
  
  for i := 0 to n - 1 do
  begin
    position := Pos(sep, source);
    if position > 0 then
    begin
      result[i] := Copy(source, 1, position - 1);
      Delete(source, 1, position + Length(sep) - 1);
    end
    else
      result[i] := source;
  end;
end;

function Join(sep: string; source: array of string): string;
var
  i: integer;
begin
  result := source[0];
  
  for i := 1 to n - 1 do
    result := Concat(result, sep, source[i]);
end;

begin
  Assign(fin, FileIn);
  Reset(fin);
  
  Assign(fout, FileOut);
  Rewrite(fout);
  
  While not EoF(fin) do
  begin
    // Читаем размер матрицы
    Readln(fin, n);
    // Обнуляем матрицу
    matrix := Nil;
    // Задаем размеры матрицы
    SetLength(matrix, n);
    for row := 0 to n - 1 do
      SetLength(matrix[row], n);
    // Читаем значения и заполняем матрицу
    for row := 0 to n - 1 do
    begin
      Readln(fin, src);
      target := Split(' ', src);
      for col := 0 to n - 1 do
        matrix[row][col] := target[col];
      begin
      end;
    end;
    // Удаляем элементы побочной диагонали
    counter := 0;
    row := n - 1;
    col := 0;
    while counter < n do
    begin
      matrix[row][col] := '*';
      Dec(row);
      Inc(col);
      Inc(counter);
    end;
    // Записываем результат в файл
    Writeln(fout, n);
    for row := 0 to n - 1 do
    begin
      Writeln(fout, Join(' ', matrix[row]));
    end;
    
  end;
  
  Close(fout);
  Close(fin);
end.

Overwriting the original file:
program ChangeMatrix;

const
  DataFile = 'C:\Users\Jonathan\Documents\in.txt';

var
  fdata: Text;
  n: integer;
  matrix: array of array of string;
  change: array of string;
  cursor: integer;
  row, col: integer;
  src: string;
  target: array of string;
  counter: integer;

function Split(sep: string; source: string): array of string;
var
  i: integer;
  position: integer;
begin
  result := Nil;
  SetLength(result, n);
  
  for i := 0 to n - 1 do
  begin
    position := Pos(sep, source);
    if position > 0 then
    begin
      result[i] := Copy(source, 1, position - 1);
      Delete(source, 1, position + Length(sep) - 1);
    end
    else
      result[i] := source;
  end;
end;

function Join(sep: string; source: array of string): string;
var
  i: integer;
begin
  result := source[0];
  
  for i := 1 to n - 1 do
    result := Concat(result, sep, source[i]);
end;

begin
  cursor := 0;
  Assign(fdata, DataFile);
  Reset(fdata);
  
  While not EoF(fdata) do
  begin
    // Читаем размер матрицы
    Readln(fdata, n);
    // Обнуляем матрицу
    matrix := Nil;
    // Задаем размеры матрицы
    SetLength(matrix, n);
    SetLength(change, Length(change) + n + 1);
    change[cursor] := IntToStr(n);
    Inc(cursor);
    for row := 0 to n - 1 do
      SetLength(matrix[row], n);
    // Читаем значения и заполняем матрицу
    for row := 0 to n - 1 do
    begin
      Readln(fdata, src);
      target := Split(' ', src);
      for col := 0 to n - 1 do
        matrix[row][col] := target[col];
      begin
      end;
    end;
    // Удаляем элементы побочной диагонали
    counter := 0;
    row := n - 1;
    col := 0;
    while counter < n do
    begin
      matrix[row][col] := '*';
      change[cursor + row] := Join(' ', matrix[row]);
      Dec(row);
      Inc(col);
      Inc(counter);
    end;
    Inc(cursor, n);
  end;
  
  Close(fdata);
  
  Assign(fdata, DataFile);
  Rewrite(fdata);
    // Записываем результат в файл
    for row := 0 to cursor - 1 do
    begin
      Writeln(fdata, change[row]);
    end;
  Close(fdata);
end.

L
Leydenfrost, 2018-02-12
@Leydenfrost

begin
 assign(input,' {имя файла}');
  reset(input);
assign(output,'{имя файла}');
  rewrite(output);

 readln(n);
 for i:=1 to n do
   for j:=1 to n do
    read(a[i, j]);

readln(n2);
for i:=1 to n2 do
  for j:=1 to n2 do
   read(a2[i,j]);

{и решение}
end.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question