|
-
Jul 13th, 2003, 05:36 AM
#1
Thread Starter
Junior Member
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
-
Jul 13th, 2003, 07:11 AM
#2
yay gay
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/
-
Jul 13th, 2003, 04:48 PM
#3
Thread Starter
Junior Member
here: Console.WriteLine(randInd.ToString().PadLeft(3) + " " + randNum(randInd).ToString())
when i used [] it told me error or something
-
Jul 13th, 2003, 04:49 PM
#4
yay gay
Console.WriteLine(randInd.ToString().PadLeft(3) + " " + randNum[randInd].ToString());
\m/  \m/
-
Jul 13th, 2003, 06:43 PM
#5
Thread Starter
Junior Member
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)]++; }
-
Jul 13th, 2003, 09:08 PM
#6
Frenzied Member
randNum is not declared as an array. Try this
Code:
int[] randNum = new int[45];
-
Jul 13th, 2003, 09:35 PM
#7
Thread Starter
Junior Member
thanks but this doesn't work still:
Code:
Console.WriteLine(randInd.ToString().PadLeft(3) + " " + randNum[randInd].ToString());
-
Jul 13th, 2003, 10:20 PM
#8
Frenzied Member
Post all the C# code that you have.
-
Jul 13th, 2003, 10:58 PM
#9
Thread Starter
Junior Member
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());
}
-
Jul 14th, 2003, 06:13 AM
#10
Frenzied Member
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();
-
Jul 14th, 2003, 12:14 PM
#11
yay gay
in vb.net arent they all 0 based too?
\m/  \m/
-
Jul 14th, 2003, 01:43 PM
#12
Lively Member
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.
-
Jul 14th, 2003, 03:27 PM
#13
Thread Starter
Junior Member
I guess in VB, you can make them start from 1 to the desired number?!
-
Jul 14th, 2003, 05:33 PM
#14
yay gay
yeah you can set the lower and upper bound..from 69 to 666 for example
\m/  \m/
-
Jul 14th, 2003, 06:50 PM
#15
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|