c# - trying to randomize a 2d array - c#

I am trying to make a 2d array that has shuffled elements in each row. For example, in a 3x5 array, each row will have 1, 2, and 3, but the order of the elements will be different:
what I want:
1 3 2
2 1 3
2 3 1
3 1 2
1 2 3
code I've tried:
//initialize a matrix
int[,] matrix = new int[3, 5];
Random rnd = new Random();
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
matrix[i,j] = rnd.Next(1,3);
Console.WriteLine(matrix[i,j]);
}
Console.ReadLine();
}
It seems fairly easy, but as a newbie, I've been struggling for days over this problem.
Any help or lead will be appreciated!

You can make an List containing 1, 2, and 3, and then shuffle it for each row. Then just copy the shuffled values over:
public static void Main(string[] args)
{
int[,] matrix = new int[5, 3];
Random rnd = new Random();
var values = Enumerable.Range(1, 3).ToList();
for (int row = 0; row < matrix.GetLength(0); row++)
{
values = values.OrderBy(x => rnd.NextDouble()).ToList();
for (int col = 0; col < matrix.GetLength(1); col++)
{
matrix[row, col] = values[col];
Console.Write(matrix[row, col] + " ");
}
Console.WriteLine();
}
Console.WriteLine("Press Enter to Quit.");
Console.ReadLine();
}
Sample Output:
2 3 1
3 1 2
3 2 1
1 3 2
2 1 3
Press Enter to Quit.

Related

input 2d array sideways

how can I input an integer array sideways not downward? suppose it has array [2,2] then in c # :
input :
1
2
3
4
I want to store like this:
1 2
3 4
what should i do? I am confused, as for the code I use:
int length = Convert.ToInt16(Console.ReadLine());
int[,] box = new int[length, 2];
for(int i = 0; i<length; i++){
for(int j = 0; j<2; j++){
photo[i, j] = Convert.ToInt16(Console.ReadLine());
}
}
static void Main(String[] args){
int length = Convert.ToInt32(Console.ReadLine());
int[,] box = new int[length, 2];
for (int i = 0; i < length; i++){
box[i, 0] = Int32.Parse(Console.ReadLine());
box[i, 1] = Int32.Parse(Console.ReadLine());
}
}
As you have mentioned that you have two columns it might give you a better understanding.For each row since we have two columns due to that I we are fetching values two times.

Delete border of matrix

I have a 2d array[,] and I need to delete the border of it (or create a new array without it). I tried everything I thought of and nothing worked since I don't usually work with 2d arrays. Help please?
1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7
becomes:
6 7
1 2
Queue<int> numbers = new Queue<int>();
int[,] b = new int[4, 4];
for (int i = 0; i < b.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
int x = int.Parse(Console.ReadLine());
b[i, j] = x;
}
}
int rows = b.GetLength(0);
Console.WriteLine("Number of rows: "+rows);
int columns = b.GetLength(1);
Console.WriteLine("Number of columns: " + columns);
int[,] arrayUpdated = new int[columns - 2,rows - 2];
for (int n = 0; n < b.GetUpperBound(1); n++)
{
for (int i = 0; i < b.GetUpperBound(0); i++)
{
arrayUpdated[i, n] = b[i, n];
Console.WriteLine(arrayUpdated[i, n]);
}
}
public static T[,] RemoveBorders<T>(T[,] source)
{
//index of last item, not number of items
var ROW = source.GetUpperBound(0);
if (ROW < 2) throw new ArgumentException("source array is not tall enough");
var COL = source.GetUpperBound(1);
if (COL < 2) throw new ArgumentException("source array is not wide enough");
var result = new T[ROW - 1, COL - 1];
for (int r = 1; r < ROW; r++)
{
for (int c = 1; c < COL; c++)
{
result[r-1, c-1] = source[r, c];
}
}
return result;
}
https://dotnetfiddle.net/dtUpRA
You would use it in the code in the question like this:
int[,] arrayUpdated = RemoveBorders(b);

How to create a 3D List in C#

I have a list which is made up of 2 lists with the following numbers:
list(1) = (2,3,5,3)
list(2) = (1,3,9,2).
Now I have to create two matrix:
The first matrix 4x4 should have all the elements of list(1) on the diagonal, the rest of the numbers should be zero.
The second matrix 4x4 should have all the elements of list(2) on the diagonal. The rest of the numbers should be zero.
I want to do this with a for loop.
Could you please help me? I don't know how to start, I'm new in C# and I can't find references in which it's clear how to work with 3D matrix as I did with Matlab.
Thanks a lot!
Create a regular List<int> for the first list.
List<int> list = new List<int>() { 2, 3, 5, 3 };
Then your 'matrix'(which really is a 2D array):
int[,] matrix = new int[4, 4];
Now, the diagonal means column == row, so using two loops you can enter the value only when that condition is met.
for (int row = 0; row < list.Count; row++)
{
for (int col = 0; col < list.Count; col++)
{
if (col == row)
matrix[row, col] = list[row];
else
matrix[row, col] = 0;
}
}
Confirmation:
And do the same thing for the 2nd list. You could easily write a function that would do this.
EDIT
Here it is put into a function, and how to use it.
static int[,] CreateMatrix(List<int> list)
{
int[,] matrix = new int[list.Count, list.Count];
for (int row = 0; row < list.Count; row++)
{
for (int col = 0; col < list.Count; col++)
{
if (col == row)
matrix[row, col] = list[row];
else
matrix[row, col] = 0;
}
}
return matrix;
}
Calling:
var list1 = new List<int>() { 2, 3, 5, 3 };
var list2 = new List<int>() { 1, 3, 9, 2 };
var matrix1 = CreateMatrix(list1);
var matrix2 = CreateMatrix(list2);

how multiply two dimensional array aka matrix [duplicate]

This question already has answers here:
How to multiply a matrix in C#?
(2 answers)
Closed 6 years ago.
I want to know how to multiply 2D arrays (matrices) and show them as a matrix.
The current output is
1 1 1
1 1 1
1 1 1
which is not the correct result.
The code is below:
static void Main(string[] args)
{
int[,] metrix1 = new int[3, 3] { { 2, 0, 2 }, { 0, 1, 2 }, { 1, 2, 1 } };
int[,] metrix2 = new int[3, 3] { { 1, 1, 1 }, { 0, 1, 0 }, { 0, 0, 1 } };
int[,] result = new int[3, 3];
int m1 =0;
int m2 =0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
m1 = metrix1[i, j];
}
}
// metrix 2
for (int y = 0; y < 3; y++)
{
for (int z = 0; z < 3; z++)
{
m2 = metrix2[y, z];
}
}
//m
for (int a = 0; a < 3; a++)
{
for (int b = 0; b < 3; b++)
{
result[a, b] = m1 *m2;
Console.Write(result[a, b] );
}Console.WriteLine();
}Console.ReadLine();
}
}
}
You're approaching this incorrectly.
for (int y = 0; y < 3; y++)
{
for (int z = 0; z < 3; z++)
{
m2 = metrix2[y, z];
}
}
After this, m2 will contain the lower right-hand value of the array (matrix[2, 2]). You're literally multiplying the same value over and over again for your multiplication. You need to put all of this in one for loop.
Also, that's not the correct way to do matrix multiplication. To begin with, your code will only work for a 3x3 matrix; not only can you have other sizes of matrices (4x4, 5x5, etc.), matrices don't even have to be square to be able to multiply them (as long as the number of columns of one equals the number of rows of the other). Even if they are both 3x3 matrices, the calculation would still be wrong. Wikipedia has some good examples of the proper way to do this.
2 0 2 1 1 1 (2*1)+(0*0)+(2*0) (2*1)+(0*1)+(2*0) ...
0 1 2 x 0 1 0 = (0*1)+(1*0)+(2*0) (0*1)(1*1)(2*0) ...
1 2 1 0 0 1 ..

How to get all the numbers from 2d array that pass the if condition into 1d array

sorry for noob question :). I've got 2d array 3x3 filled with random numbers (-5,5)
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
dPole[i, j] = nc.Next(-10, 10);
I want all the numbers that are positive and then save them into 1d array:
foreach (int j in dPole)
{
if (j > 0)
{
Console.WriteLine(j);
for (int i = 0; i < sizeOf1dArray; i++)
jPole[i] = j;
}
}
Output of Console.WriteLine(j)- to check if the condition is right:
6
2
5
6
9
8
Output of 1d Array:
8
8
8
8
8
8
Only the last number is saved into array. Why? Thanks.
An alternative route would be to use Cast<int> to flatten the array and use Where to filter.
int[,] dPole = new int[,] { { 3, -5, 0 }, { -3, 3, 2 }, { -2, 1, 1 } };
int[] jPole = dPole.Cast<int>().Where(i => i > 0).ToArray();
// jPole is now { 3, 3, 2, 1, 1 };
for (int i = 0; i < sizeOf1dArray; i++)
jPole[i] = j;
because at this loop action last j value is 8 and this loop fills all jPole array with j value every time, that means it fills all with 6 first, than puts 2 at entire array, then 5...... and for last it fills it with 8.
try something like that
int i = 0;
foreach (int j in dPole)
{
if (j > 0)
{
Console.WriteLine(j);
jPole[i] = j;
i++;
}
}

Categories

Resources