I wrote a program. It should receive the number of known residues (when dividing), these residues themselves (in turn, one by one), the numbers when dividing by which these residues are obtained (sequentially, one by one). The task of the program is to find the smallest positive number that, when divided by the entered numbers, gives the entered residuals respectively (ie, the first remainder when divided by the first number, the second by the second, etc.), using the Chinese remainder theorem its solutions, which will be difficult to describe. Here is the code:
Program A0xCHIN; type TMas1=array [Byte] of Integer; TMas2=array [Byte] of Integer; function GetMult (B: TMas2; N: Integer): Integer; var Y, G, I: Integer; begin Y:=B[1]; for I:=2 to N do begin G:=B[I]; Y:=Y*G; end; GetMult:=Y; end; procedure GetRemainders (N: Integer; A: TMas1); {procedure writes remainders to TMas1} var I: Integer; begin for I:=1 to N do begin WriteLn ('Input a remainder ', I); Read (A[I]) end; end; procedure GetModules (N: Integer; B: TMas2); {procedure writes modules to TMas2} var I: Integer; begin for I:=1 to N do begin WriteLn ('Input a module ', I); Read (B[I]) end; end; function FindNum (A: TMas1; B: TMas2; N: Integer): LongInt; {function finds number} var I, X: Integer; Result, Res, PRes: LongInt; Label startloop; begin X:=0; PRes:=A[1]; for I:=1 to N do begin startloop: if (((PRes+(X*GetMult (B, I)) - A[I]) mod B[I])=0)then begin Res:=PRes+(X*GetMult (B, I)); PRes:=PRes+(X*GetMult (B, I)); X:=0 end else begin X:=X+1; {GoTo startloop;} end; Result:=Res; FindNum:=Result end; end; var N: Integer; A: TMas1; B: TMas2; begin WriteLn ('Input number of remainders: '); Read (N); GetRemainders (N, A); GetModules (N, B); WriteLn ('Result is ', FindNum (A, B, N)); Write ('Press ENTER to exit'); ReadLn; end.
I have a regular compiler, like this:
Questions about this:
- The program assigns PRes is not the first residue that I entered, but something else. Why?
- Are the types in the array declaration suitable for the following functions: should the array store up to several hundred non-negative integers?
Code:
type TMas=array [1..10] of Integer; var A: TMas;
a[1]=12
and not something else?What is stored in the array in place of those elements that are not given a value?