Results 1 to 13 of 13

Thread: [RESOLVED] C++ to C#

  1. #1
    Member
    Join Date
    Apr 12
    Posts
    48

    Resolved [RESOLVED] C++ to C#

    I code a algorithm by C++(Breadth first search), i'm fluent C++, but i'm learning C#, so i don't know function or keyword between it, i need convert to learning and searching, can you help convert it, short:
    Code:
    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    const int maxx = 20;
    
    void Read_input_from_user(bool grid[][maxx], int vertices)
    {
        int u, v;
        for(int x = 0; x < vertices; ++x)
        {
            cout << "Enter u : \t"; 
            cin >> u;
            u--;
            cout << "Enter v : \t";
            cin >> v;
            v--;
            grid[u][v] = true;
                    grid[v][u] = true;
            cout << "---------------------\n";
        }
    }
    
    void Breadth_first_search(queue<int> &Q, vector<int> &trace,
                              bool grid[][maxx], int start, int nodes)
    {
        int u;
        vector<int> visited(maxx,0);
        Q.push(start);
        trace[start] = -1;
        visited[start] = 1;
        do{
            u = Q.front();
            Q.pop();
            for(int v = 0; v < nodes; ++v)
            {
                if((grid[u][v] == true) && visited[v] == 0)
                {
                    Q.push(v);
                    trace[v] = u;
                    visited[v] = 1;
                }
            }
        }while(!Q.empty());
    }
    
    void Trace_result(vector<int> &trace, int start, int end, int nodes)
    {
        cout << "From _nodes" << start + 1 << " you can visit :\n";
        for(int v = 0; v < nodes; ++v)
        {
            if(trace[v] != 0)
            {
                cout << " _nodes : " << v + 1 << " , ";
            }
        }
    
        cout << "\n--------------------------------------------\n";
        cout << "The path from " << start + 1 << " to " << end + 1 << '\n';
        
        if(trace[end] == 0){
            cout << "Unavailable.! to go to from " << end + 1 
                 << " to -> " << start + 1 << '\n';
        }
        else{
            while(end != start)
            {
                cout << end + 1 << "<-";
                end = trace[end];
            }
            cout << start + 1 << endl;
        }
        
    }
    
    
    int main()
    {
        //Initialization
        vector<int> trace(maxx, 0);
        queue<int> Q;
        bool grid[maxx][maxx] = {false};
        
        int nodes, vertices;
        cout << "Please input the number of Node : \n";
        cin >> nodes;
        cout << "Please input the number of Vertices : \n";
        cin >> vertices;
    
        //Set value for all vertices.
        Read_input_from_user(grid, vertices); 
    
        //Read the necessary path
        int starting_position, finishing_position;
        cout << "Please Input the Starting Node : \n";
        cin >> starting_position;
        cout << "Please Input the Finishing Node : \n";
        cin >> finishing_position;
        //Decrease to fit with index of C++ start from 0->size-1
        starting_position--;
        finishing_position--;
        //Algorithm starts 
        Breadth_first_search(Q, trace, grid, starting_position, nodes);
        Trace_result(trace, starting_position, finishing_position, nodes);
        system("pause");
        return 0;
    }
    Suggestions me please, in procedure: void Breadth_first_search(queue<int> &Q, vector<int> &trace, bool grid[][maxx], int start, int nodes). I don't know Q.push, Q.pop, Q.front in C#, >"<.

  2. #2
    Fanatic Member
    Join Date
    Jan 06
    Posts
    515

    Re: C++ to C#

    This should get you started - note that a couple of things, such as 'cin' are not converted:

    Code:
    using System;
    using System.Collections.Generic;
    
    private const int maxx = 20;
    
    private void Read_input_from_user(bool[,] grid, int vertices)
    {
    	int u;
    	int v;
    	for (int x = 0; x < vertices; ++x)
    	{
    		Console.Write("Enter u : \t");
    		cin >> u;
    		u--;
    		Console.Write("Enter v : \t");
    		cin >> v;
    		v--;
    		grid[u, v] = true;
    				grid[v, u] = true;
    		Console.Write("---------------------\n");
    	}
    }
    
    private void Breadth_first_search(Queue<int> Q, List<int> trace, bool[,] grid, int start, int nodes)
    {
    	int u;
    	List<int> visited = new List<int>(maxx);
    	Q.Enqueue(start);
    	trace[start] = -1;
    	visited[start] = 1;
    	do
    	{
    		u = Q.Peek();
    		Q.Dequeue();
    		for (int v = 0; v < nodes; ++v)
    		{
    			if ((grid[u, v] == true) && visited[v] == 0)
    			{
    				Q.Enqueue(v);
    				trace[v] = u;
    				visited[v] = 1;
    			}
    		}
    	}while (Q.Count > 0);
    }
    
    private void Trace_result(List<int> trace, int start, int end, int nodes)
    {
    	Console.Write("From _nodes");
    	Console.Write(start + 1);
    	Console.Write(" you can visit :\n");
    	for (int v = 0; v < nodes; ++v)
    	{
    		if (trace[v] != 0)
    		{
    			Console.Write(" _nodes : ");
    			Console.Write(v + 1);
    			Console.Write(" , ");
    		}
    	}
    
    	Console.Write("\n--------------------------------------------\n");
    	Console.Write("The path from ");
    	Console.Write(start + 1);
    	Console.Write(" to ");
    	Console.Write(end + 1);
    	Console.Write('\n');
    
    	if (trace[end] == 0)
    	{
    		Console.Write("Unavailable.! to go to from ");
    		Console.Write(end + 1);
    		Console.Write(" to -> ");
    		Console.Write(start + 1);
    		Console.Write('\n');
    	}
    	else
    	{
    		while (end != start)
    		{
    			Console.Write(end + 1);
    			Console.Write("<-");
    			end = trace[end];
    		}
    		Console.Write(start + 1);
    		Console.Write("\n");
    	}
    
    }
    
    
    static int Main()
    {
    	//Initialization
    	List<int> trace = new List<int>(maxx);
    	Queue<int> Q = new Queue<int>();
    	bool[,] grid = {false};
    
    	int nodes;
    	int vertices;
    	Console.Write("Please input the number of Node : \n");
    	cin >> nodes;
    	Console.Write("Please input the number of Vertices : \n");
    	cin >> vertices;
    
    	//Set value for all vertices.
    	Read_input_from_user(grid, vertices);
    
    	//Read the necessary path
    	int starting_position;
    	int finishing_position;
    	Console.Write("Please Input the Starting Node : \n");
    	cin >> starting_position;
    	Console.Write("Please Input the Finishing Node : \n");
    	cin >> finishing_position;
    	//Decrease to fit with index of C++ start from 0->size-1
    	starting_position--;
    	finishing_position--;
    	//Algorithm starts 
    	Breadth_first_search(Q, trace, grid, starting_position, nodes);
    	Trace_result(trace, starting_position, finishing_position, nodes);
    	system("pause");
    	return 0;
    }
    Last edited by David Anton; Aug 26th, 2012 at 10:50 AM.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  3. #3
    Member
    Join Date
    Apr 12
    Posts
    48

    Re: C++ to C#

    Thank for help, but something wrong in code, ex: bool[,] grid = {false};
    Can not, can you check?

  4. #4
    Fanatic Member
    Join Date
    Jan 06
    Posts
    515

    Re: C++ to C#

    Try:
    Code:
    bool[,] grid = new bool[maxx, maxx];
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  5. #5
    Member
    Join Date
    Apr 12
    Posts
    48

    Re: C++ to C#

    And this code:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            private const int maxx = 20;
    
             void Read_input_from_user(bool[,] grid, int vertices)
            {
                int u;
                int v;
                for (int x = 0; x < vertices; ++x)
                {
                    Console.Write("Enter u : \t");
                    u = Convert.ToInt32(Console.ReadLine());
                    u--;
                    Console.Write("Enter v : \t");
                    v = Convert.ToInt32(Console.ReadLine());
                    v--;
                    grid[u, v] = true;
                    grid[v, u] = true;
                    Console.Write("---------------------\n");
                }
            }
    
             void Breadth_first_search(Queue<int> Q, List<int> trace, bool[,] grid, int start, int nodes)
            {
                int u;
                List<int> visited = new List<int>(maxx);
                Q.Enqueue(start);
                trace[start] = -1;
                visited[start] = 1;
                do
                {
                    u = Q.Peek();
                    Q.Dequeue();
                    for (int v = 0; v < nodes; ++v)
                    {
                        if ((grid[u, v] == true) && visited[v] == 0)
                        {
                            Q.Enqueue(v);
                            trace[v] = u;
                            visited[v] = 1;
                        }
                    }
                } while (Q.Count > 0);
            }
    
             void Trace_result(List<int> trace, int start, int end, int nodes)
            {
                Console.Write("From _nodes");
                Console.Write(start + 1);
                Console.Write(" you can visit :\n");
                for (int v = 0; v < nodes; ++v)
                {
                    if (trace[v] != 0)
                    {
                        Console.Write(" _nodes : ");
                        Console.Write(v + 1);
                        Console.Write(" , ");
                    }
                }
    
                Console.Write("\n--------------------------------------------\n");
                Console.Write("The path from ");
                Console.Write(start + 1);
                Console.Write(" to ");
                Console.Write(end + 1);
                Console.Write('\n');
    
                if (trace[end] == 0)
                {
                    Console.Write("Unavailable.! to go to from ");
                    Console.Write(end + 1);
                    Console.Write(" to -> ");
                    Console.Write(start + 1);
                    Console.Write('\n');
                }
                else
                {
                    while (end != start)
                    {
                        Console.Write(end + 1);
                        Console.Write("<-");
                        end = trace[end];
                    }
                    Console.Write(start + 1);
                    Console.Write("\n");
                }
    
            }
             static int Main()
            {
                //Initialization
                List<int> trace = new List<int>(maxx);
                Queue<int> Q = new Queue<int>();
                bool[,] grid = new bool[maxx, maxx];
                int nodes;
                int vertices;
                Console.Write("Please input the number of Node : \n");
                //cin >> nodes;
                nodes = Convert.ToInt32(Console.ReadLine());
                Console.Write("Please input the number of Vertices : \n");
                //cin >> vertices;
                vertices = Convert.ToInt32(Console.ReadLine());
                //Set value for all vertices.
                Read_input_from_user(grid, vertices);
                
                //Read the necessary path
                int starting_position;
                int finishing_position;
                Console.Write("Please Input the Starting Node : \n");
                //cin >> starting_position;
                starting_position = Convert.ToInt32(Console.ReadLine());
                Console.Write("Please Input the Finishing Node : \n");
                //cin >> finishing_position;
                finishing_position = Convert.ToInt32(Console.ReadLine());
                //Decrease to fit with index of C++ start from 0->size-1
                starting_position--;
                finishing_position--;
                //Algorithm starts 
                Breadth_first_search(Q, trace, grid, starting_position, nodes);
                Trace_result(trace, starting_position, finishing_position, nodes);
                return 0;
            }
        }
    }
    But error in 3 procedure:
    Read_input_from_user(grid, vertices);
    Breadth_first_search(Q, trace, grid, starting_position, nodes);
    Trace_result(trace, starting_position, finishing_position, nodes);

    Error 3 An object reference is required for the non-static field, method, or property 'ConsoleApplication1.Program.Trace_result(System.Collections.Generic.List<int>, int, int, int)' C:\Users\Longbottom-up\Desktop\thu\ConsoleApplication1\ConsoleApplication1\Program.cs 127 13 ConsoleApplication1

    Error 1 An object reference is required for the non-static field, method, or property 'ConsoleApplication1.Program.Read_input_from_user(bool[*,*], int)' C:\Users\Longbottom-up\Desktop\thu\ConsoleApplication1\ConsoleApplication1\Program.cs 111 13 ConsoleApplication1

    Error 2 An object reference is required for the non-static field, method, or property 'ConsoleApplication1.Program.Breadth_first_search(System.Collections.Generic.Queue<int>, System.Collections.Generic.List<int>, bool[*,*], int, int)' C:\Users\Longbottom-up\Desktop\thu\ConsoleApplication1\ConsoleApplication1\Program.cs 126 13 ConsoleApplication1

    I delete static, is that: int Main() , so not error in 3 procedure, i f5 and:

    Error 1 Program 'C:\Users\Longbottom-up\Desktop\thu\ConsoleApplication1\ConsoleApplication1\obj\x86\Debug\ConsoleApplication1.exe' does not contain a static 'Main' method suitable for an entry point ConsoleApplication1
    What's this?

  6. #6
    Fanatic Member
    Join Date
    Jan 06
    Posts
    515

    Re: C++ to C#

    Make all the methods static. (Note: when our converter see C++ non-static methods outside of a class context, then it assumes that they are instance methods taken out of context, so that's why it didn't add 'static' for the methods).
    Last edited by David Anton; Aug 26th, 2012 at 12:08 PM.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  7. #7
    Member
    Join Date
    Apr 12
    Posts
    48

    Re: C++ to C#

    Quote Originally Posted by David Anton View Post
    Make all the methods static. (Note: when our converter see C++ non-static methods outside of a class context, then it assumes that they are instance methods taken out of context, so that's why it didn't add 'static' for the methods).
    Yes, very thanks. Code finish, but not work, >"<

  8. #8
    Member
    Join Date
    Apr 12
    Posts
    48

    Re: C++ to C#

    Look at C++: in Breadth_first_search(queue<int> &Q, vector<int> &trace, bool grid[][maxx], int start, int nodes) and Trace_result(vector<int> &trace, int start, int end, int nodes)
    I see: &Q and &trace is passing value???? So i code in C#:ref queue<int> Q and ref List<int> trace.
    So, after, whatever i edit, it seem error in: trace[start] = 1:

    System.ArgumentOutOfRangeException was unhandled
    Message=Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    Source=mscorlib
    ParamName=index
    StackTrace:
    at System.ThrowHelper.ThrowArgumentOutOfRangeException()
    at System.Collections.Generic.List`1.set_Item(Int32 index, T value)
    at ConsoleApplication1.Program.Breadth_first_search(Queue`1& Q, List`1& trace, Boolean[,] grid, Int32 start, Int32 nodes) in C:\Users\Longbottom-up\Desktop\thu\ConsoleApplication1\ConsoleApplication1\Program.cs:line 35
    at ConsoleApplication1.Program.Main() in C:\Users\Longbottom-up\Desktop\thu\ConsoleApplication1\ConsoleApplication1\Program.cs:line 126
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()
    InnerException:

  9. #9
    Member
    Join Date
    Apr 12
    Posts
    48

    Re: C++ to C#

    Can you test it on your VS, please.

  10. #10
    Fanatic Member
    Join Date
    Jan 06
    Posts
    515

    Re: C++ to C#

    Quote Originally Posted by Longbottomup View Post
    Look at C++: in Breadth_first_search(queue<int> &Q, vector<int> &trace, bool grid[][maxx], int start, int nodes) and Trace_result(vector<int> &trace, int start, int end, int nodes)
    I see: &Q and &trace is passing value???? So i code in C#:ref queue<int> Q and ref List<int> trace.
    So, after, whatever i edit, it seem error in: trace[start] = 1
    No - you don't need the 'ref' unless you are changing the root object and want that passed back - changing an element of a list does not require 'ref'.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  11. #11
    Member
    Join Date
    Apr 12
    Posts
    48

    Re: C++ to C#

    Quote Originally Posted by David Anton View Post
    No - you don't need the 'ref' unless you are changing the root object and want that passed back - changing an element of a list does not require 'ref'.
    It's still error, not work when i input the node begin and node finish, not work, >"<.

  12. #12
    Member
    Join Date
    Apr 12
    Posts
    48

    Re: C++ to C#

    Same error.

  13. #13
    Member
    Join Date
    Apr 12
    Posts
    48

    Re: C++ to C#

    Thank for all, David, i think, i should be: Start level Zero. Thank for help.
    Last edited by Longbottomup; Aug 28th, 2012 at 05:29 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •