Results 1 to 15 of 15

Thread: array handling and vb .net to c# translation help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    17

    array handling and vb .net to c# translation help

    I am new to C# and I would like to know how to handle array in C#. Here's the code I wrote in vb .net. I got stuck when I tried to translated it to C#. Could someone help?

    Code:
        Sub Main()
            Randomize()
            Dim randNum(45) As Integer  ' counters
            Dim randGen As New Random(DateTime.Now.Millisecond)  ' random generator
            Dim randInd As Integer = 0  ' loop counter
    
            Dim intTemp As Int16 = 0
    
            'Start...
            For randInd = 1 To 2000
                randNum(randGen.Next(1, 46)) += 1  ' increment counter
            Next
    
            Console.WriteLine("Numbers   Times")
            Console.WriteLine("=======   =====")
    
            For randInd = 1 To 45
                Console.WriteLine(randInd.ToString().PadLeft(3) + "       " + randNum(randInd).ToString())
            Next
    
            'Most frequent randomized number:
            Dim temp As String
            'Dim memory As String
            Dim check As Int16
            Dim checkTime As Int16
            checkTime = 7
    
            Console.WriteLine("")
            Console.WriteLine("The top 7 most frequent randomized numbers are:")
            Console.WriteLine("")
    
            For check = 1 To checkTime
                For randInd = 1 To 45
                    If randNum(randInd) = intTemp Then
                        If temp.Length <> 0 Then
                            temp = temp + " & " + randInd.ToString()
                            'memory = randInd.ToString()
                        Else
                            temp = randInd.ToString()
                            'memory = randInd.ToString()
                        End If
                    ElseIf randNum(randInd) > intTemp And randNum(randInd) <> intTemp Then
                        intTemp = randNum(randInd)
                        temp = randInd.ToString()
                        'memory = randInd.ToString()
                    End If
                Next
    
                For randInd = 1 To 45
                    If randNum(randInd) = intTemp Then randNum(randInd) = 0
                Next
    
                Console.WriteLine(temp.PadLeft(25) + ": " + intTemp.ToString() + " times") 'Write the top numbers
                intTemp = 0
            Next check
    
            Console.WriteLine("")
            Console.WriteLine("Press any key to continue")
            Console.ReadLine()
    
        End Sub

  2. #2
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    whats where u have problems with? in c# the array indexer simbol is [] instead of vb's () so in a string array you do myArray[0] to get the first slot of the array
    \m/\m/

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    17
    here: Console.WriteLine(randInd.ToString().PadLeft(3) + " " + randNum(randInd).ToString())
    when i used [] it told me error or something

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Console.WriteLine(randInd.ToString().PadLeft(3) + " " + randNum[randInd].ToString());
    \m/\m/

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    17
    not doesn't work. it says cannot apply indexing with [] to an expression as type 'int'

    it also happens when I try to do this...
    Code:
    for(randInd=1; randInd<=2000; randInd++)
    {	randNum[randGen.Next(45)]++;	}

  6. #6
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    randNum is not declared as an array. Try this
    Code:
    int[] randNum = new int[45];

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    17
    thanks but this doesn't work still:
    Code:
    Console.WriteLine(randInd.ToString().PadLeft(3) + " " + randNum[randInd].ToString());

  8. #8
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Post all the C# code that you have.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    17
    i haven't finished it yet but so far this is what i got...

    Code:
    int[] randNum = new int[45];
    			Random randGen = new Random();
    			int randInd = 0;
    			int intTemp = 0;
    
    			//Start
    			for(randInd=1; randInd<=2000; randInd++)
    			{	randNum[randGen.Next(45)]++;	}
    
    			Console.WriteLine("Numbers   Times");
    			Console.WriteLine("=======   =====");			
    
    			for(randInd=1; randInd<=45; randInd++)
    			{
    				Console.WriteLine(randInd.ToString().PadLeft(3) + " " + randNum[randInd].ToString());
                }

  10. #10
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    I just ran the code and it worked fine. I do get an exception at the end saying index is out of range. The reason for that is because arrays in C# are zero based. They start at zero. So if I sad
    Code:
    int[] num = new int[3]; 
    //indexing would start at zero
    num[0] = 100;
    num[1] = 200;
    num[2] = 300;
    
    num[3] = 400 // this would throw an out of range exception
    So I made a few changes to your code;
    Code:
    int[] randNum = new int[45];
    Random randGen = new Random();
    int randInd = 0;
    int intTemp = 0;
    
    //Start
    for(randInd=1; randInd<=2000; randInd++)
    {	
    	randNum[randGen.Next(45)]++;	
    }
    
    Console.WriteLine("Numbers   Times");
    Console.WriteLine("=======   =====");			
    
    for(randInd=0; randInd < 45; randInd++)
    {
    	Console.WriteLine(randInd.ToString().PadLeft(3) + " " + randNum[randInd].ToString());
    }
    Console.ReadLine();

  11. #11
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    in vb.net arent they all 0 based too?
    \m/\m/

  12. #12
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    Originally posted by PT Exorcist
    in vb.net arent they all 0 based too?
    Both are zero based but the size differs.

    Dim aData(4) As Object creates an array of five elements, zero through 4.

    object[] aData = new object[4]; creates an array of 4 elements, zero through 3.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    17
    I guess in VB, you can make them start from 1 to the desired number?!

  14. #14
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    yeah you can set the lower and upper bound..from 69 to 666 for example
    \m/\m/

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jul 2003
    Posts
    17

    Final codes

    Here are my final codes. Anything to change/improve or are there any faster ways to accomplish the task?

    Code:
    using System;
    
    namespace ConsoleApplication1
    {
    	/// <summary>
    	/// Randomize from 0~44 for 200 times then pick up 7 of the most frequent randomized numbers.
    	/// </summary>
    	class Class1
    	{
    		/// <summary>
    		/// See summary of Class1
    		/// </summary>
    		[STAThread]
    		static void Main(string[] args)
    		{
    			int[] randNum = new int[45];
    			Random randGen = new Random();
    			int randInd = 0;
    			int intTemp = 0;
    
    			// Start
    			for(randInd=1; randInd<=2000; randInd++)
    			{	
    				randNum[randGen.Next(45)]++;	
    			}
    
    			Console.WriteLine("Numbers   Times");
    			Console.WriteLine("=======   =====");			
    
    			for(randInd=0; randInd < 45; randInd++)
    			{
    				Console.WriteLine(randInd.ToString().PadLeft(3) + "       " + randNum[randInd].ToString());
    			}
    
    			// Most frequent randomized numbers:
    			string temp="";
    			int check;
    			int checkTime = 7;
    			
    			Console.WriteLine("");
    			Console.WriteLine("The top "+checkTime.ToString()+" most frequent randomized #'s are:");
    			Console.WriteLine("");
    
    			for(check=1; check<=checkTime; check++)
    			{
    				for(randInd=0; randInd<45; randInd++)
    				{
    					if(randNum[randInd]==intTemp)
    					{
    						if(temp.Length!=0)
    						{	temp=temp+" & "+randInd.ToString();	}
    						else
    						{	temp=randInd.ToString();	}
    					}
    					if(randNum[randInd]>intTemp && randNum[randInd]!=intTemp)
    					{
    						intTemp=randNum[randInd];
    						temp=randInd.ToString();
    					}
    				}
    
    				for(randInd=0; randInd<45; randInd++)
    				{
    					if(randNum[randInd]==intTemp)
    					{	randNum[randInd]=0;	}
    				}
    
    				Console.WriteLine(temp.PadLeft(21)+": "+intTemp.ToString()+" times");
    				intTemp=0;
    			}
    
    			Console.WriteLine("");
    			Console.WriteLine("Press any key to continue");
    			Console.ReadLine();
    		}
    	}
    }

Posting Permissions

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



Click Here to Expand Forum to Full Width