Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Project1;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- type
- MatrixType = array of array of Integer;
- TArr = array of Real;
- function SizeOfMatrix(var MyFile: TextFile): Integer;
- var
- Numb, n, NumbEx, Temp: Integer;
- IsCorrect, FirstIt: Boolean;
- begin
- n := 0;
- FirstIt := True;
- IsCorrect := True;
- while (not Eof(MyFile)) and IsCorrect do
- begin
- NumbEx := Numb;
- Numb := 0;
- while not Eoln(MyFile) do
- begin
- Inc(Numb);
- Read(MyFile, Temp);
- end;
- if NumbEx <> Numb then
- IsCorrect := False;
- if FirstIt then
- begin
- IsCorrect := True;
- FirstIt := False;
- end;
- Inc(n);
- Readln(MyFile);
- end;
- if (Numb = n) and IsCorrect then
- SizeOfMatrix := n
- else
- SizeOfMatrix := 0;
- end;
- procedure ReadFileName(var MyFile: TextFile);
- var
- FName: string;
- CorrectName: Boolean;
- begin
- Writeln('Enter a file name for data entry in the format Name.txt');
- repeat
- Readln(FName);
- if FileExists(FName) then
- CorrectName := True
- else
- begin
- Writeln('The file name was entered incorrectly. Try again. Example: Name.txt');
- CorrectName := False;
- end;
- until CorrectName;
- AssignFile(MyFile, FName);
- Reset(MyFile);
- end;
- function IsTheMatrixCorrect(var MyFile: TextFile): Boolean;
- var
- Number: Integer;
- TrueMatrix, IsValidInput: Boolean;
- begin
- TrueMatrix := True;
- while (not Eof(MyFile)) do
- begin
- try
- read(MyFile, Number)
- except
- TrueMatrix := False
- end;
- end;
- if Eof(MyFile) then
- IsValidInput := True
- else
- Writeln('File is empty.');
- CloseFile(MyFile);
- IsTheMatrixCorrect := TrueMatrix;
- end;
- procedure ReadMatrix(var Matrixx: MatrixType; var MyFile: TextFile; n: Integer);
- var
- i, j: Integer;
- Arr: array of array of Integer;
- begin
- for i := 0 to n - 1 do
- for j := 0 to n - 1 do
- Read(MyFile, Matrixx[i, j]);
- CloseFile(MyFile);
- end;
- function Average(Matrixx: MatrixType; n: Integer): TArr;
- var
- Sum, Amount, i, j: Integer;
- Arr: TArr;
- begin
- SetLength(Arr, n);
- Arr[i] := 0;
- for i := 0 to n - 1 do
- begin
- Sum := 0;
- Amount := 0;
- for j := 0 to n - 1 do
- if Matrixx[j, i] > 0 then
- begin
- Sum := Sum + Matrixx[j, i];
- Inc(Amount);
- end;
- Arr[i] := Sum / Amount;
- end;
- Average := Arr;
- end;
- procedure MatrixOutput(const Matrixx: MatrixType; n: Integer);
- var
- i, j: Integer;
- begin
- for i := 0 to n do
- begin
- for j := 0 to n do
- write(Matrixx[i, j], ' ');
- Writeln;
- end;
- Writeln;
- end;
- procedure AverageOutput(Arr: TArr; n: Integer);
- var
- i: Integer;
- begin
- for i := 0 to n - 1 do
- begin
- write(Arr[i], ' ');
- end;
- Writeln;
- end;
- procedure WriteToFile(const Arr: TArr; n: Integer);
- var
- FName: string;
- NewFile: TextFile;
- i: Integer;
- begin
- AssignFile(NewFile, FName);
- rewrite(NewFile);
- for i := 0 to n - 1 do
- begin
- write(NewFile, Arr[i]);
- write(NewFile, ' ');
- end;
- CloseFile(NewFile);
- Writeln('Saved to file: ', FName);
- end;
- var
- Matrixx: MatrixType;
- Size, i: Integer;
- Avrg: Real;
- MyTFile: TextFile;
- CorrectMatr: Boolean;
- Mass: TArr;
- begin
- Writeln('This program finds the arithmetic mean of the positive elements of each column of the square matrix.');
- repeat
- ReadFileName(MyTFile);
- CorrectMatr := IsTheMatrixCorrect(MyTFile);
- if CorrectMatr = True then
- begin
- Writeln('Given matrix:');
- ReadMatrix(Matrixx, MyTFile, Size);
- Size := SizeOfMatrix(MyTFile);
- SetLength(Matrixx, Size, Size);
- MatrixOutput(Matrixx, Size);
- SetLength(Mass, Size);
- Mass := Average(Matrixx, Size);
- Writeln('The result after addition:');
- AverageOutput(Mass, Size);
- Writeln('Enter a file name to save:');
- WriteToFile(Mass, Size);
- end
- else
- CorrectMatr := False;
- Writeln('The name of the matrix inputs incorrect. Try again. Example: Name.txt');
- until CorrectMatr;
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement