Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program Project9;
- {$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;
- Reset(MyFile);
- 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.');
- Reset(MyFile);
- IsTheMatrixCorrect := TrueMatrix;
- end;
- procedure ReadMatrix(var Matrixx: MatrixType; var MyFile: TextFile; n: Integer);
- var
- i, j: Integer;
- FName: string;
- begin
- for i := 0 to n - 1 do
- for j := 0 to n - 1 do
- Read(MyFile, Matrixx[i, j]);
- Reset(MyFile);
- end;
- procedure FindMax(const Matrixx: MatrixType; n, iFMax, jFMax, iSMax, jSMax: Integer);
- var
- FirstMax, SecondMax, i, j: Integer;
- begin
- FirstMax := Matrixx[0][0];
- iFMax := 0;
- jFMax := 0;
- for i := 0 to n do
- begin
- for j := 0 to n do
- if (Matrixx[i][j] > FirstMax) then
- begin
- FirstMax := Matrixx[i][j];
- iFMax := i;
- jFMax := j;
- end;
- end;
- SecondMax := Matrixx[0][0];
- iSMax := 0;
- jSMax := 0;
- for i := 0 to n do
- begin
- for j := 0 to n do
- if ((Matrixx[i][j] > SecondMax) and((i <> iFMax) or (j <> jFMax))) then
- begin
- FirstMax := Matrixx[i][j];
- iSMax := i;
- jSMax := j;
- end;
- end;
- end;
- procedure MatrixOutput(const Matrixx: MatrixType; n: Integer);
- var
- i, j: Integer;
- begin
- for i := 0 to n - 1 do
- begin
- for j := 0 to n - 1 do
- Write(Matrixx[i, j], ' ');
- Writeln;
- end;
- Writeln;
- end;
- procedure MaxFindOutput(const Matrixx: MatrixType; n, iFMax, jFMax, iSMax, jSMax: Integer);
- var
- i, j: Integer;
- begin
- for i := 0 to n - 1 do
- begin
- for j := 0 to n - 1 do
- Writeln(Matrixx[iFMax][jFMax]);
- Writeln(Matrixx[iSMax][jSMax]);
- Writeln;
- end;
- Writeln;
- end;
- procedure ReadFileOutputName(var MyFile: TextFile);
- var
- NewFName: string;
- CorrectName: Boolean;
- begin
- Writeln('Enter a file name for data entry in the format Name.txt');
- repeat
- Readln(NewFName);
- if Copy(NewFName, length(NewFName) - 3, 4) = '.txt' then
- CorrectName := True
- else
- begin
- Writeln('The file name was entered incorrectly. Try again. Example: Name.txt');
- CorrectName := False;
- end;
- until CorrectName;
- AssignFile(MyFile, NewFName);
- Rewrite(MyFile);
- end;
- procedure WriteToFile(const Matrixx: MatrixType; n, iFMax, jFMax, iSMax, jSMax: Integer; var NewFile: TextFile);
- var
- i: Integer;
- begin
- for i := 0 to n - 1 do
- begin
- Write(NewFile, Matrixx[iFMax][jFMax]);
- Write(NewFile, Matrixx[iSMax][jSMax]);
- Write(NewFile, ' ');
- end;
- CloseFile(NewFile);
- Writeln('Saved to file. ');
- end;
- var
- Matrixx: MatrixType;
- Size, iFirstMax,jFirstMax ,iSecondMax, jSecondMax: Integer;
- MyTFile: TextFile;
- MyTFileName: string;
- 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:');
- Size := SizeOfMatrix(MyTFile);
- SetLength(Matrixx, Size, Size);
- ReadMatrix(Matrixx, MyTFile, Size);
- CloseFile(MyTFile);
- MatrixOutput(Matrixx, Size);
- SetLength(Mass, Size);
- FindMax(Matrixx, Size, iFirstMax, iSecondMax, jFirstMax, jSecondMax );
- Writeln(' Max elemets are: ');
- MaxFindOutPut(Matrixx, Size, iFirstMax, iSecondMax, jFirstMax, jSecondMax);
- ReadFileOutputName(MyTFile);
- WriteToFile(Matrixx, Size, iFirstMax, iSecondMax, jFirstMax, jSecondMax);
- end
- else
- begin
- CorrectMatr := False;
- Writeln('The name of the matrix inputs incorrect. Try again. Example: Name.txt');
- end;
- until CorrectMatr;
- Readln;
- end.
Advertisement
Add Comment
Please, Sign In to add comment