4 Replies - 1093 Views - Last Post: 27 October 2009 - 07:36 AM Rate Topic: -----

#1 dlastral  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 20-October 09

How to translate this code from c in basic?

Posted 26 October 2009 - 01:00 AM

I have this code in c:

void Back()
for(i=1;i<=n;i++)
cin>>a[i]; 'display vector a[i] a[i] is a vector with 10elements
while(k>0) 'while k is greater than 0

{ do 'do loop
	{succ(x,k,as); 'call succ function
	   if(as) 'if as call function valid

		   valid(x,k,ev);
	}
while(as && !ev); 'while condition

if(as) 'if as
   if(k==p) afis(x,k); 'if k==p then call afis()
		 else 'else
			  {k=k+1;
			   x[k]=0;
			   }
else k=k-1;



How can I translate this code from c in vb.net?

I tried that:

  Sub Back()

		For i As Integer = 1 To n
			Console.WriteLine("a(i)")
		Next

		k = 1
		x(k) = 0

		While k > 0

{ do
	{succ(x,k,as);
			If (as) Then

				valid(x, k, ev)
			End If
	}Loop
			Do Until (as And Not ev)
End While
		If (ad Then
   if(k=p) afis(x,k)
						ElseIf
			  {k=k+1
				x(k) = 0
			   }
			End If
		Else : k = k - 1
		End If
	End Sub



I wait your response and hope I gave all the details!

I'm beginner in learning vb.net.

This post has been edited by dlastral: 26 October 2009 - 01:08 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to translate this code from c in basic?

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: How to translate this code from c in basic?

Posted 26 October 2009 - 01:54 AM

I'd say it's something like this:

  Private Sub Back()

	For i As Integer = 1 To n
	  Console.Write(a(i))
	Next

	While k > 0

	  Do While [as] And Not ev

		succ(x, k, [as])
		If [as] Then
		  valid(x, k, ev)
		End If

	  Loop

	  If [as] Then
		If k = p Then
		  asif(x, k)
		Else
		  k = k + 1
		  x(k) = 0
		End If
	  Else
		k = k - 1
	  End If

	End While

  End Sub



The C code seems to be missing the ending curly brace of while loop so I assume it ends after the last else statement.

Also here are the variables I used and the types of them but as that's just one method, it is impossible to say if they are the same as in above code. But not too far away anyway.
  Private n As Integer = 0
  Private a As List(Of String)
  Private k As Integer = 0
  Private [as] As Boolean = True
  Private ev As Boolean = True
  Private p As Integer = 0
  Private x As List(Of Integer)



Because 'as' is a reserved word for vb.net, it has to be in square brackets if you want to use it as a variable.

Btw. Why are you learning VB.NET? If you know C and want to move to .NET environment, you should consider learning C# because the syntax would be much more familiar. Of course if the place you work at uses VB.NET, I doubt they would like you to code with C#. But if it's just a hobby, I suggest you try C# instead of VB.NET.

This post has been edited by janne_panne: 26 October 2009 - 02:00 AM

Was This Post Helpful? 0
  • +
  • -

#3 dlastral  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 20-October 09

Re: How to translate this code from c in basic?

Posted 26 October 2009 - 02:25 AM

I modify the code in Combinations.vb file but still having an error.

In Combinations.vb file I want to count the number of permutation I read n=16 and p=2 and I want to display all combinations of 16 taken by 2.


I don't know what I'm missing I'm very near to solve the problem.

Please tell me what I'm wrong :^:

Here's the full code contained in Combinations.vb file:



Module Module1
	Dim nr_echipe As Integer = 16 'variabila pt.definirea nr.de echipe angrenate in competitie
	Dim nrc As Integer 'variabila pt.stocarea nr.de combinari

	Dim a(120) As Integer 'multimea tuturor solutiilor posibile
	'a e o multime
	'gen_mec multimea tuturor solutiilor admisibile -doar combinarile
	'gen_mec este o submultime -combinari submultime perechi de cate 2
	Dim gen_mec(120) As Integer 'gen_mec vector in care stocam meciurile -generarea automata a meciurilor comb16 luate cate2
	'combinari de n luate cate p
	Dim n As Integer
	Dim p As Integer
	Dim i As Integer
	Dim k As Integer
	Dim amsucc As Integer 'variabila amsuccesor
	Dim ev As Integer 'variabila e valid

	'procedura de numarare a combinarilor

	Sub nr_comb()
		nrc = (nr_echipe * (nr_echipe - 1)) / 2
	End Sub

	'functia succesor

	Function succ(ByVal gen_mec() As Integer, ByVal k As Integer, ByRef amsucc As Integer)

		If (gen_mec(k) < n) Then
			amsucc = 1
			gen_mec(k) = gen_mec(k) + 1
		Else : amsucc = 0
		End If
	End Function

	'functia valid

	Function valid(ByVal gen_mec() As Integer, ByVal k As Integer, ByVal ev As Integer)
		ev = 1
		If ((k > 1) And Not (a(gen_mec(k) > a(gen_mec(k - 1))))) Then
			ev = 0
		End If
	End Function

	Function afis(ByVal gen_mec() As Integer, ByVal k As Integer)
		For i As Integer = 1 To k
			Console.WriteLine("a(x(i))" + " ")
			Console.WriteLine("")
		Next i
	End Function
	
	'functia backtracking de generare a combinarilor -intr-un vector stocam combinarile in sine cu backtracking
	'metoda backtracking evita generarea tuturor solutiilor posibile ci doar pe cele admisibile

	Sub Main()
		Console.WriteLine("Numarul de combinari")
		Console.WriteLine("")
		nr_comb()
		Console.WriteLine(nrc)
		Console.ReadLine()
		Console.WriteLine("Combinari de=")
		n = Console.ReadLine()
		Console.WriteLine("Luate cate=")
		p = Console.ReadLine()

		'functia back de generare a combinarilor in programul principal


		For i As Integer = 1 To n
			Console.Write(a(i))
		Next

		While k > 0 'do din c

			Do While [amsucc] And Not ev ' 'while conditie din c

				succ(gen_mec, k, [amsucc])
				If [amsucc] Then
					valid(gen_mec, k, ev)
				End If

			Loop 'do loop se termina cu Loop cuv.rezervat

			If [amsucc] Then
				If k = p Then afis(gen_mec, k)
			Else
				k = k + 1
				gen_mec(k) = 0
			End If

			Else
			k = k - 1
			'End If
		End While


	End Sub

End Module



When I run my file and read n=16 and p=2 he displays combinations weird 000000 :blink:

I don't know what I'm wrong in display function (in my case is called afis() )

This post has been edited by dlastral: 26 October 2009 - 02:28 AM

Was This Post Helpful? 0
  • +
  • -

#4 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: How to translate this code from c in basic?

Posted 26 October 2009 - 03:50 AM

The reason you get the 00000000 is because of the first for loop in the main method:

		For i As Integer = 1 To n
			Console.Write(a(i))
		Next



You never initialize the values of array 'a' and the default value for them is 0 so it prints the 0 as many times as you describe in variable 'n'.

Also it never goes to the while loop because variable 'k' is never initialized so its default value is 0 too.
		While k > 0 'do din c
			' some logic here '
		End While


so k is never greater than 0 because k is 0.

So you have to start initializing those variables to other than 0 in order to get some real output.
Was This Post Helpful? 0
  • +
  • -

#5 dlastral  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 20-October 09

Re: How to translate this code from c in basic?

Posted 27 October 2009 - 07:36 AM

Sorry to disturb you again panne but I still don't know how to correct my error :crazy: . I'm dummy or I'm tired and I still can't figure out what I'm doing wrong in my code.

Here is the full source to run in vb.net (I want to list combinations of 16 take by 2 so we type n=16 and p=2 on command prompt when we run the program).

Here I attached to you the full source: (in visual basic)

http://www.4shared.c...a50a19/Mec.html

This post has been edited by dlastral: 27 October 2009 - 07:37 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1