Need help translating from Pascal to VB (VBA)! Cannot translate array! Pascal:

Procedure obuch; begin repeat clrscr;kol:=kol+1; writeln('****************************************************'); writeln('* Эталон * Значение на выходе * Отклонение *'); writeln('****************************************************'); For i:=1 to N_obuch do begin y:=0; For j:=1 to n do y:=y+w[j]*x[i+(j-1)]; y:=yt; For j:=1 to n do begin w[j]:=w[j]-alfa*(yx[i+n])*x[i+(j-1)]; t:=t+alfa*(yx[i+n]); err:=err+sqr(yx[i+n]); end; writeln('* ',x[i+n]:10:7,' * ',y:10:7,' * ', abs(yx[i+n]):10:7,' *'); end; err:=err/2; writeln('****************************************************'); writeln; writeln('Ошибка : ',err:8:5); read; until (err <= Ej); end; 

my translation (not quite correct) on VB:

 Function obuchenie() Const a = 3, b = 5, d = 0.5, alfa = 0.1, n = 4, Errj = 0.01, N_obuch = 30 Dim i, j, kol As Integer Dim err, y, t, rmp, zn As Long Dim W(n) As Long Dim X(45) As Long Dim x_prgnz(45) As Long kol = kol + 1 Print "*******************************************************" Print "************" Print "********************************************************" Do For i = 1 To N_obuch y = 0 For j = 1 To n y = y + W(j) * X(i + (j - 1)) y = y - t Next j For j = 1 To n W(j) = W(j) - alfa * (y - X(i + n)) * X(i + (j - 1)) t = t + alfa * (y - X(i + n)) err = err + Sqr(y - X(i + n)) Print "********"; X(i + n); "********"; y; "********"; Abs(y - X(i + n)); "********" Next j err = err / 2 Print "****************************************************************" Print "Ошибка:"; err Next i Loop Until (err <= Errj) End Function 
  • This question should be closed, because this site is not a translation agency - aleksandr barakin

2 answers 2

In VB I do not rummage, but immediately immediately:

  1. In the source text on pascal there is not enough section with the declaration of variables.
  2. After For j:=1 to n do y:=y+w[j]*x[i+(j-1)]; y:=yt; is done y:=yt; once. You have y = y - t hit the loop by j .
  3. Print "********"; X(i + n); "********"; y; "********"; Abs(y - X(i + n)); "********" Print "********"; X(i + n); "********"; y; "********"; Abs(y - X(i + n)); "********" should be after Next j .
  4. Next i should be immediately after point 3.

Something like this. Carefully read the code. Better yet, format it so that you can clearly see where the cycles begin and end. And then immediately see for yourself all the errors.

  • On Pascal, this procedure works fine .... carefully look at what this procedure is! - iman
  • one
    Maybe it works fine, but in the code you wrote here there are no variable declarations. Either this procedure is nested in another procedure and its variables are used, or global variables are used. In any case, they must all be declared somewhere. - kot-da-vinci
 Sub obuch() Do Cls: kol = kol + 1 Print "****************************************************" Print "* Эталон * Значение на выходе * Отклонение *" Print "****************************************************" For i = 1 to N_obuch y = 0 For j = 1 to n: y = y + w(j) * x(i + j - 1): Next y = y - t For j = 1 to n w(j) = w(j) - alfa * (y - x(i + n) * x(i + j - 1) t = t + alfa * (y - x(i + n)) err = err + (y - x(i + n)) ^ 2 Next Print "* ", x(i + n), " * ", y, " * ", abs(y - x(i + n)), " *" Next err = err / 2 '' (надо уточнять - возможно имелось в виду err \ 2) Print "****************************************************" Print Print "Ошибка : ", err system "pause > null" Loop Until err <= Ej End Sub 

translated literally (without analyzing the actions of the program), in its current form is suitable for execution on some qbasic. for vba, you need to replace Print with Debug.Print, system - with something else (you can just delete it - it still does nothing).

PS: I also refused to format the output numbers - I don’t remember from memory.