I am creating a function to generate random values for a multidimensional array and I get the error "method must have a return type" and "; expected".
Here is my code:
public static Double[,] X Generate_random()
{
Random rnd = new Random();
X = new Double[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
X[i, j] = rnd.Next(0, 10);
}
}
}
I think you meant to write your code like this
public static Double[,] Gerar_Aleatorio() {
Random rnd = new Random();
Double [,] x;
x = new Double[3, 3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
x[i, j] = rnd.Next(0, 10);
}
}
return x;
}
When you declare your return type for your method (in this case, Double[,]), you need to ensure that all paths through the method will end up returning an instance of that type. This is done using the return statement, which you would just place at the end of the method:
return X;
However, you never explicitly declare what type X is. When defining a variable, you must do: [Type] [VariableName] and additionally you can do the assignment on the same line (just as you did for Random). So your X definition should look like:
Double[,] X = new Double[3, 3];
Additionally, you have a typo (I assume) in the signature, where you have an X in the signature next to the type declaration. That should be removed, so your signature looks like:
public static Double[,] Generate_random()
Putting that all together, you have:
public static Double[,] Generate_random()
{
Random rnd = new Random();
Double[,] X = new Double[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
X[i, j] = rnd.Next(0, 10);
}
}
return X;
}
Related
I have a method which returns the following type and object:
object {object[,]}
test {object[19, 2]}
Is it possible to convert this into 2d string array, or a datatable?
Looks akward, but I haven't found another way.
int firstDimensionSize = 19;
int secondDimensionSize = 2;
object[,] objectArray = new object[firstDimensionSize, secondDimensionSize];
string[,] stringArray = new string[objectArray.Length / secondDimensionSize, secondDimensionSize];
for (int i = 0; i < objectArray.Length / secondDimensionSize; i++)
{
for (int j = 0; j < secondDimensionSize; j++)
{
stringArray[i, j] = objectArray[i, j].ToString();
}
}
In case your second dimension is always the same you could leave out the inner loop, replacing it with static code.
See also https://www.dotnetperls.com/2d
I made this extension method, but you can aswell make it a normal method if you don't need it that often.
public static class Extensions
{
public static string[,] ConvertToString2D(this object[,] arr)
{
int dim1 = arr.GetLength(0);
int dim2 = arr.GetLength(1);
string[,] strArray = new string[dim1, dim2];
for (int i = 0; i < dim1; i++)
{
for (int j = 0; j < dim2; j++)
{
strArray[i, j] = Convert.ToString(arr[i, j]);
}
}
return strArray;
}
}
and then you can call it like this:
var result = objArr.ConvertToString2D();
Here's my code
public int[,] GetBigEyeRoad(int x)
{
int[,] arrayBigEyeResult = new int[6, x];
Array.Copy(arrayBigEyeRoad, arrayBigEyeResult, arrayBigEyeRoad.GetLength(0) * arrayBigEyeRoad.GetLength(1));
return arrayBigEyeResult;
}
And calling it on my main class like this
int[,] arrayBigEyeRoad = bsb.GetBigEyeRoad(104);
string s = "";
for (int y = 0; y < arrayBigEyeRoad.GetLength(0); y++)
{
for (int x = 0; x < arrayBigEyeRoad.GetLength(1); x++)
{
s += string.Format("{0:D2}", arrayBigEyeRoad[y, x]);
s += ".";
}
s += "\n";
}
Debug.Log(s);
Here on this part
int[,] arrayBigEyeRoad = bsb.GetBigEyeRoad(104);
I only want to display 12 2D array values just like this
int[,] arrayBigEyeRoad = bsb.GetBigEyeRoad(12);
The problem is that it won't let me . Because it will give me an error saying
Destination array was not long enough. Check destIndex and length, and the array's lower bounds
Now how can I possible do it something like this
Limit the 2d display on the console
Pretty simple:
int[,] a1 = new int[100,200];
int[,] a2 = new int[10,5];
for (int i = 0; i < 10; i++)
for (int j = 0; j < 5; j++)
a2[i,j] = a1[i,j];
or
public class MyArray : int[,]
{
public override string ToString()
{
string result = "";
for (int i = 0; i < 10; i++)
for (int j = 0; j < 5; j++)
result += (a1[i,j].ToString() + ",");
return result;
}
}
So I need to make 2 Dimension Array with random numbers, and expel the numbers like in multiplication table. It should look like this:
My code for now looks like this:
Random random = new Random();
int[,] array = new int[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
array[i, j] = random.Next();
Console.Write("\t{0}\t{1}" ,i,j);
}
EDIT:::This worked for me!
Random random = new Random();
int[,] array = new int[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
array[i, j] = random.Next(10);
Console.Write(array[i,j]+"\t");
}
Console.WriteLine("\n");
A 10x10 array of random numbers could be generated like this:
var rnd = new Random();
var randomArray = Enumerable.Range(0,10).Select(x => {
return Enumerable.Range(0,10).Select(y => rnd.Next()).ToArray();
}).ToArray();
Live example: http://rextester.com/GWTAK54347
As suggested by ThatBrianDude
static void Main(string[] args)
{
Random random = new Random();
int[,] array = new int[10, 10];
for (int i = 0;
i < 10;
i++)
{
for (int j = 0;
j < 10;
j++)
{
array[i,
j] = random.Next();
Console.Write("\t{0}",
array[i,
j]);
}
Console.Write("\n");
}
Console.ReadKey();
}
I think that this should work for you:
private static void RunArrayCode()
{
Random random = new Random();
int[,] array = new int[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
array[i, j] = random.Next();
// No tabs if it's the first item in a row
Console.Write("{0}{1}\t{2}", (j == 0 ? "" : "\t"), i, j);
}
// Each new row goes on its own line
Console.WriteLine();
}
}
I am making a array filled with random numbers in c# but I can't get it to work.
int[,] array = new int[10, 5];
int x, y;
x = 0;
y = 0;
while (y <= 5)
{
Random r = new Random();
int rand = r.Next(-50, 50);
array[x, y] = rand;
if (x == 10)
{
x = 0;
y++;
}
x++;
}
Use nested for loops, it is much easier:
Random rnd = new Random();
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5; j++)
{
array[i, j] = rnd.Next(-50, 50);
}
}
Your while loop is not readable but it's correct,except you should change while (y <= 5) to while (y < 5) otherwise you will get an IndexOutOfRangeException. And you should define your Random instance outside of the loop.
int[,] array = new int[10, 5];
int x = 0, y = 0;
Random r = new Random();
while (y < 5)
{
int rand = r.Next(-50, 50);
array [x, y] = rand;
x++;
if (x == 10)
{
x = 0;
y++;
}
}
Try this:
int[,] array = new int[10, 5];
Random rnd = new Random();
for (int row = 0; row < 10; row++)
{
for (int col = 0; col < 5; col++)
{
array[row, col] = rnd.Next(-50, 50);
}
}
you need to change yours if or array declaration to
int[,] array = new int[11, 6];
also there is other problem, you need to create random before while
Random r = new Random();
while (y <= 5)
{
to print out the values you can use for
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
Console.WriteLine(array[i, j]);
}
int[,] array = new int[10, 5];
int x, y;
x = 0;
y = 0;
while (y < 5)
{
Random r = new Random();
int rand = r.Next(-50, 50);
array[x, y] = rand;
if (x == 9)
{
x = 0;
y++;
}
x++;
}
Let's see if this works.
I would like to fill out a 2-dimensional array randomly .But I dont know how to assign .This is what i have yet
Random rnd = new Random();
int x = rnd.Next(0, 3);
int y = rnd.Next(0, 3);
int[,] array=new int[2,2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
?? I have no idea
}
}
try this.
Random rnd = new Random();
int[,] array=new int[2,2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
array[i,j] = rnd.Next(0,100);
}
}
You can do it like this. But i wonder why you are generating 2 random numbers
Random rnd = new Random();
int x = rnd.Next(0, 3);
int y = rnd.Next(0, 3);
int[,] array=new int[2,2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
array[i,j] = yourvalue;
}
}