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: alt text

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; 
    Why is a[1]=12 and not something else?

  • What is stored in the array in place of those elements that are not given a value?

  • For some reason, the lines are not translated, although in the preview they were translated ... - Uran_235 pm
  • I found how this prog can be written 2 times shorter and dishonest. When entering, we check all modules or something there for mutual simplicity, then we run through the whole LongInt in search of the right number. Quickly and carelessly! - Uran_235

1 answer 1

You forgot to specify in the parameters of the function var for variables that should be returned through parameters:

 procedure GetRemainders (N: Integer; var A: TMas1); procedure GetModules (N: Integer; var B: TMas2); 

In place of non-specified elements can be any crap. Since this is pascal, when creating arrays, etc. "animals" in the program, it does not guarantee that they will be reset to zero, so if necessary, do it yourself.

And if I were you, I would use lists instead of arrays, it is a bit more complicated, but you will not immediately allocate memory for arrays of hundreds of bytes. Yes, and you have all the cycles without "twists" ie. The list method is very convenient. (IMHO)

Wherein:

 TMas1=array [Byte] of Integer; 

you are limited to 256 numbers. In addition, with such a record, the indices start with "0" And also -32768 <= Integer <= 32767

Using goto tags is not comme il faut.

  • I have no idea =) I have written in <MASSIVE>: = array [<TYPE_index>] of <TYPE_element> in my notebook. I realized that the type of index can be indicated not only as a gap of type 1..10, but also as a name of some type. It turns out that in TMas1 and TMas2 there should be 65,537 elements (not more). It seems to me that I confused something with if / else, because if I do this check: the residuals are 2, 2, 3, 5, the numbers are 9, 4, 5, 7, then in theory the program should find the number 1 118 since 1 118 = 2 + 0 * 9 + 1 * 9 * 4 + 6 * 9 * 4 * 5, and the program must find the numbers 0, 1, 6 on its own. But it finds only 0 correctly. I have already changed a little. - Uran_235
  • Yes you can use. And so and so and in every way. Read carefully what is wrong with you. - Dex