Array out of bounds? Why? [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Currently designing an application that will compute for the sale of salesment for 4 month given an initial sale. The sale is expected to raise by 5% every month in 4 months.
ProduceSalesProjectionTable function seems to be giving me an out of bound error and i don't know why can't seem to figure it out.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace itse
{
class TempAgency
{
static string[] salesman1 = new string[4];
static double[,] sales1 = new double[4, 5];
static void DisplayInstructions()
{
Console.WriteLine("You will be asked to enter data for four salesmen.");
Console.WriteLine("For their name, enter their first name followed");
Console.WriteLine("by a space and then their las name.");
Console.WriteLine("");
Console.WriteLine("For testing purposes enter data for four <4> salesmen.");
Console.WriteLine("");
Console.WriteLine("");
}
static void GetSalesData(string[] salesman, double[,] sales)
{
for (int x = 0; x < 4; x++)
{
Console.WriteLine("Enter name: ");
salesman[x] = Console.ReadLine();
string [] split = salesman[x].Split(new Char [] {' '});
Console.WriteLine(split[0]+"'s Sales goal: ");
sales[x,0]=Convert.ToDouble(Console.ReadLine());
}
}
static void ProduceSalesProjectionTable(double[,] sales)
{
for (int i = 0; i < 4; i++)
{
double salesPercentage = 0.05;
for (int j = 0; j < 4; i++)
{
sales[i, j + 1] = sales[i, 0] + (sales[i, 0] * salesPercentage); //here lies the problem
salesPercentage += 0.05;
}
}
}
static void DisplaySalesProjections()
{
}
public static void Main(String[] args)
{
DisplayInstructions();
GetSalesData(salesman1, sales1);
//Console.WriteLine("salesman: " + salesman[0]);
//Console.WriteLine("sales: " + sales[0, 0]);
ProduceSalesProjectionTable(sales1);
//for (int i = 0; i < 4; i++)
//{
// //double salesPercentage = 0.05;
// for (int j = 0; j < 4; i++)
// {
// Console.WriteLine("salesman: " + salesman[i] + "\tsales: " + sales[i, j]);
// }
//}
//Console.WriteLine("sales:" + sales[0, 1]);
//Console.WriteLine("sales:" + sales[1, 1]);
//Console.WriteLine("sales:" + sales[2, 1]);
//Console.WriteLine("sales:" + sales[3, 1]);
for (int i = 0; i < 4; i++)
{
//double salesPercentage = 0.05;
for (int j = 0; j < 4; i++)
{
Console.WriteLine("salesman: " + salesman1[i] + "\tsales: " + sales1[i, 0]);
}
}
Console.ReadLine();
}
}
}

Look carefully at the line
for (int j = 0; j < 4; i++)
Your indexer is j but you are increasing i instead of j.
Eventually sales[i, j + 1] evaluates to sales[4, 1] which is out of bounds.
Solution: change your code to
for (int j = 0; j < 4; j++)
(You have done this in 2 places)

If you build your for-loops like this, there should be no "out of bound error":
static void ProduceSalesProjectionTable(double[,] sales)
{
for (int i = 0; i < sales1.GetLength(0); i++)
{
for (int j = 0; j < sales1.GetLength(1); i++)
{
...Do Stuff...
}
}
}
When you use 2-dimensional arrays GetLength(0) gives you the length of the first dimension of your 2-dimensional array and GetLength(1) gives you the length of he second dimension.

Related

Check common elements of two-dimensional arrays of integers, considering the positions they are on

at the beginning I want to mention that I am a beginner in programming. So, I want to write a program that checks the similarity of two-dimensional arrays of integers. The similarity is to be determined by the amount of numbers that are in the same positions in both tables. The user gives the number of columns in the table and the elements themselves, number of rows is the same all the time.The similarity result is displayed as a percentage and the similarity itself should be calculated taking into account the number of elements of the larger array. My problem is: When the two arrays are the same size, the program throws the exception and it doesn't check all the numbers in the column.(I wrote before program for one dimensional array and it works perfectly) So far I have managed to write something like this:
This is what I want to do In the picture, the similarity between the arrays is 20%
{
Console.WriteLine("How extensive is the first table supposed to be?");
int n = int.Parse(Console.ReadLine());
int z = 2;
int[,] tab1 = new int[2, n];
Console.WriteLine("Enter the numbers into the first array:");
for (int i = 0; i < z; i++)
{
for (int j = 0; j < n; j++)
{
tab1[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\n");
int rowLength = tab1.GetLength(0);
int colLength = tab1.GetLength(1);
for (int i = 0; i < rowLength; i++)
{
for (int j = 0; j < colLength; j++)
{
Console.Write(string.Format("{0} ", tab1[i, j]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
Console.WriteLine("How extensive is the second table supposed to be?");
int m = int.Parse(Console.ReadLine());
int b = 2;
int[,] tab2 = new int[2, m];
Console.WriteLine("Enter the numbers into the second array: ");
for (int i = 0; i < b; i++)
{
for (int j = 0; j < m; j++)
{
tab2[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\n");
int Len4gth = tab2.GetLength(0);
int Len2gth = tab2.GetLength(1);
for (int i = 0; i < Len4gth; i++)
{
for (int j = 0; j < Len2gth; j++)
{
Console.Write(string.Format("{0} ", tab2[i, j]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
double similarity= 0;
if (tab1.GetLength(1) > tab2.GetLength(1))
{
for (int i = 0; i < tab2.GetLength(1); i++)
{
for (int j = 0; j < z; j++)
{
if (tab1[i, j] == tab2[i, j])
{
similarity+= 1;
}
}
}
}
if (tab1.GetLength(1) < tab2.GetLength(1))
{
for (int i = 0; i < tab1.GetLength(1); i++)
{
for (int j = 0; j < z; j++)
{
if (tab2[i, j] == tab1[i, j])
{
similarity+= 1;
}
}
}
}
if (tab1.GetLength(1) == tab2.GetLength(1))
{
for (int i = 0; i < tab1.GetLength(1); i++)
{
for (int j = 0; j < z; j++)
{
if (tab1[i, j] == tab2[i, j])
{
similarity+= 1;
}
}
}
}
if (tab1.Length < tab2.Length)
{
Console.WriteLine("The similarity of the arrays is: " + (similarity/ tab2.Length) * 100 + "%");
}
if (tab1.Length > tab2.Length)
{
Console.WriteLine("The similarity of the arrays is: " + (similarity/ tab1.Length) * 100 + "%");
}
if (tab1.Length == tab2.Length)
{
Console.WriteLine("The similarity of the arrays is: " + (similarity/ tab2.Length) * 100 + "%");
}
Console.ReadKey();
You must compare each element of the first array with the elements of the second array.
use this code :
//get first array items
Console.WriteLine("How extensive is the first table supposed to be?");
int n = int.Parse(Console.ReadLine());
int[,] tab1 = new int[2, n];
Console.WriteLine("Enter the numbers into the first array:");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < n; j++)
{
tab1[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\n");
//write first array items
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(string.Format("{0} ", tab1[i, j]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
//get second array items
Console.WriteLine("How extensive is the second table supposed to be?");
int m = int.Parse(Console.ReadLine());
int[,] tab2 = new int[2, m];
Console.WriteLine("Enter the numbers into the second array: ");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < m; j++)
{
tab2[i, j] = int.Parse(Console.ReadLine());
}
}
//write second array items
Console.WriteLine("\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < m; j++)
{
Console.Write(string.Format("{0} ", tab2[i, j]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
//find similarity items
double similarity = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < n; j++)
{
int firstValue = tab1[i, j];
for (int k = 0; k < 2; k++)
{
for (int d = 0; d < m; d++)
{
if (firstValue == tab2[k, d])
{
similarity += 1;
}
}
}
}
}
double percentage = n > m ? ((similarity / tab1.Length) * 100) : ((similarity / tab2.Length) * 100);
Console.WriteLine("The similarity of the arrays is: " + percentage + "%");
Console.ReadKey();
this code work without error and It does not matter which array is larger.
If you want similar elements like this example enter link description here, use this code snippet to find similar elements
//find similarity items
double similarity = 0;
int z = n > m ? m : n;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < z; j++)
{
if (tab1[i, j] == tab2[i, j])
{
similarity += 1;
}
}
}
double percentage = n > m ? ((similarity / tab1.Length) * 100) : ((similarity / tab2.Length) * 100);
Console.WriteLine("The similarity of the arrays is: " + percentage + "%");
Console.ReadKey();

Convert list to 3D Array in C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
does anyone know how to convert a List to a 3D Array?. My input list will actually always be a "flattened" verison of a 3D Array, so I will always know the arrays dimensions. Any clues would be great
T[,] output = new T[height, width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
output[i, j] = input[i * width + j];
}
}
This is to convert a List/Array to a 2D Array, but I dont seem to wrap my head around to convert to a 3D Array
You will need to know each dimension of the three of the 3D array. Lets say they are d1, d2, and d3, then you can use this code to get the array you want, assuming an int array:
int i, j, k, p;
int[,,] Arr = new int[d1, d2, d3];
p = 0;
for (i = 0; i < d1; i++)
for (j = 0; j < d2; j++)
for (k = 0; k < d3; k++)
a[i, j, k] = lst[p++];
If you want a solution similar to you example you can try this:
int i, j, k;
int[,,] Arr = new int[d1, d2, d3];
for (i = 0; i < d1; i++)
for (j = 0; j < d2; j++)
for (k = 0; k < d3; k++)
a[i, j, k] = lst[i * d2 * d3 + j * d3 + k];
You just need to know which order the items were stored in when going from an array to a list, and from that you can see how to calculate the index into the list:
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// N.B. You will have to pick which order the dimensions go in.
var ni = 4;
var nj = 3;
var nk = 2;
// Make a list that could be interpreted as a 3-d array:
var x = new List<string>();
for (int k = 0; k < nk; k++)
{
for (int j = 0; j < nj; j++)
{
for (int i = 0; i < ni; i++)
{
x.Add($"{k}-{j}-{i}");
}
}
}
Console.WriteLine(string.Join(", ", x));
// Copy the content of the list to a 3-d array:
string[,,] array1 = new string[nk, nj, ni];
for (int k = 0; k < nk; k++)
{
for (int j = 0; j < nj; j++)
{
for (int i = 0; i < ni; i++)
{
var idx = i + j * (nj + 1) + k * (nk + 1) * (nj + 1);
array1[k, j, i] = x[idx];
Console.Write(array1[k, j, i] + ", ");
}
}
}
Console.ReadLine();
}
}
}
Which outputs, for confirmation,
0-0-0, 0-0-1, 0-0-2, 0-0-3, 0-1-0, 0-1-1, 0-1-2, 0-1-3, 0-2-0, 0-2-1, 0-2-2, 0-2-3, 1-0-0, 1-0-1, 1-0-2, 1-0-3, 1-1-0, 1-1-1, 1-1-2, 1-1-3, 1-2-0, 1-2-1, 1-2-2, 1-2-3
0-0-0, 0-0-1, 0-0-2, 0-0-3, 0-1-0, 0-1-1, 0-1-2, 0-1-3, 0-2-0, 0-2-1, 0-2-2, 0-2-3, 1-0-0, 1-0-1, 1-0-2, 1-0-3, 1-1-0, 1-1-1, 1-1-2, 1-1-3, 1-2-0, 1-2-1, 1-2-2, 1-2-3,
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 3; k++)
a[i, j ,k] = x;

Infinite loop when iterating over a character array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
The following program is going in infinite loop when I am trying to access a multiple line input,do you have any idea why is it not working?
namespace AlternatingCharacters
{
class Program
{
static void Main(string[] args)
{
int N = Int32.Parse(Console.ReadLine());
string[] str = new string[N];
for (int i = 0; i < N ; i++)
{
str[i] = Console.ReadLine();
}
for (int i = 0; i < str.Length; i++)
{
int count = 0;
Char[] strArray = str[i].ToCharArray();
for (int j = 0; j < strArray.Length; j++)
{
if (strArray[i] == strArray[i + 1])
{
count++;
}
}
Console.WriteLine(count);
Console.ReadLine();
}
}
}
}
The problem is in this line:
for (int j = 0; i < strArray.Length; j++)
Your condition is checking on i, not on j, so i will always be 0 (start value) and will never change during the loop.
The right code is:
for (int j = 0; j < strArray.Length; j++)
After that it will fail on this line:
if (strArray[i] == strArray[i + 1])
At the end, it can't find 'the last index + 1' which you can prevent by subtracting one on the end, so this (also I think you need j here):
for (int j = 0; j < strArray.Length - 1; j++)
{
if (strArray[j] == strArray[j + 1])
{
count++;
}
}
The error comes from this piece of code:
for (int i = 0; i < str.Length; i++)
{
int count = 0;
Char[] strArray = str[i].ToCharArray();
for (int j = 0; i < strArray.Length; j++)
{
if (strArray[i] == strArray[i + 1])
{
count++;
}
}
Console.WriteLine(count);
Console.ReadLine();
}
First your loop-condition of the inner loop is whrong. While you increment j you check if i equals the number of elements in your array. More critical than this however is that you also use the whring array-elements. I guess you should use if (strArray[j] == strArray[j + 1]) instead of using i as index.
So all in all this should work:
for (int i = 0; i < str.Length; i++)
{
int count = 0;
Char[] strArray = str[i].ToCharArray();
for (int j = 0; j < strArray.Length; j++)
{
if (strArray[j] == strArray[j + 1])
{
count++;
}
}
Console.WriteLine(count);
Console.ReadLine();
}

How to print star like below in console application? I/p 5 O/p like = 1 212 32123 4321234 543212345 [duplicate]

My question is how to make a pyramid using * and 'space' in C#? The output will be like this.
*
* *
* * *
* * * *
* * * * *
We only need to use "for loop" for this program. I only know how to make this one.
*
**
***
****
*****
I made a program like this:
static void Main(string[]args)
{
int i=o;
int j=o;
for(i=5;1>=1;i--)
for(j=1;j<=5;j++)
{
Console.Write("*");
}
Console.WriteLine(" ");
}
I'm confused when it comes to pyramid because it includes spaces. Thanks for your help!
think about how you'd print the pyramid manually.
suppose 5 levels deep.
1st line: 4 spaces, 1 star,
2nd line: 3 spaces, star, space, star
3rd line: 2 spaces, star space star space star
etc.
doesn't matter whether you print spaces after the last star or not - won't make a difference to how it looks.
what do we see?
if we have a total of X levels
line 1: (x-1) spaces, (star space)
line 2: (x-2) spaces, (star space) twice
line 3: (x-3) spaces, (star space) three times
line 4: (x-4) spaces, (star space) four times
that's the pattern. I'll leave the coding to you.
using System;
class Program
{
static void Main(string[] args)
{
int num, i, j, k;
Console.Write("enter the level:");
num=Convert.ToInt32(Console.ReadLine());
for (i = 1; i <= num; i++)
{
for (j = 1; j < num-i+1; j++)
{
Console.Write(" ");
}
for (k = 1; k <= i; k++)
{
Console.Write(i);
Console.Write(" ");
}
Console.WriteLine();
}
}
}
Your problem is spaces, therefore I suggest you think about the spaces. Tell me this: how many spaces are on each row to the left of the first star? You'll likely be able to solve your own problem if you think about this.
Try to think of it as a grid or a matrix and see where you want the '*' in each row and how it relates to your loop index.
sorry I missed this was homework... will give a strategy ... instead
it helps if you do it in notepad and think about what you are doing... you will start to understand the relationship between the line you are on and the spaces and what not...
Post my answer after 3 hours. I think now you have almost finished it under #iluxa's advice?
int height = 20;
for (int level = 1; level <= height; level++)
{
string text = string.Join(" ", Enumerable.Repeat("*", level));
Console.WriteLine(text.PadLeft(height - level + text.Length));
}
I used some build-in methods e.g. Enumerable.Repeat and String.PadLeft, not the pure C-language way. The purpose is that I want to tell you since you have chosen C# as the programming language(not C/Java/etc), you should resolve problems in the C# way.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace pyramid_star
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter a number:");
int n = Convert.ToInt32(Console.ReadLine());
for (int i = 1; i <= n; i++)
{
for (int x = i; x <= n; x++)
{
Console.Write(" ");
}
for (int j = 1; j <= i; j++)
{
Console.Write("*"+" ");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Star_Pyramid
{
class Program
{
static void Main(string[] args)
{
Program o = new Program();
o.show();
Console.ReadKey();
}
void show()
{
for (int i = 1; i <= 12; i++)
{
for (int j = 1; j <= 9 - i / 2; j++)
{
Console.Write(" ");
}
for (int k = 1; k <= i; k++)
{
Console.Write(" * ");
Console.Write(" ");
}
Console.WriteLine();
}
}
}
}
class Program
{
static void Main(string[] args)
{
int num;
Console.WriteLine("enter level");
num = Int32.Parse(Console.ReadLine());
int count = 1;
for (int lines = num; lines >= 1; lines--)
{
for (int spaces = lines - 1; spaces >= 1; spaces--)
{
Console.Write(" ");
}
for (int star = 1; star <= count; star++)
{
Console.Write("*");
Console.Write(" ");
}
count++;
Console.WriteLine();
}
Console.ReadLine();
}
}
Try this and follow this same logic in c, c++, php, java
using System;
class pyramid {
static void Main() {
/** Pyramid stars Looking down
Comment this if u need only a upside pyramid **/
int row, i, j;
// Total number of rows
// You can get this as users input
//row = Int32.Parse(Console.ReadLine());
row = 5;
// To print odd number count stars use a temp variable
int temp;
temp = row;
// Number of rows to print
// The number of row here is 'row'
// You can change this as users input
for ( j = 1 ; j <= row ; j++ ) {
// Printing odd numbers of stars to get
// Number of stars that you want to print with respect to the value of "i"?
for ( i = 1 ; i <= 2*temp - 1 ; i++ )
Console.Write("*");
// New line after printing a row
Console.Write("\n");
for ( i = 1 ; i <= j ; i++ )
Console.Write(" ");
// Reduce temp value to reduce stars count
temp--;
}
/** Pyramid stars Looking up
Comment this if u need only a downside pyramid **/
int rowx, k, l;
// Total number of rows
// You can get this as users input
// rowx = Int32.Parse(Console.ReadLine());
rowx = 5;
// To print odd number count stars use a temp variable
int tempx;
tempx = rowx;
//Remove this if u use
Console.Write("\n");
// Number of rows to print
// The number of row here is 'rowx'
for ( l = 1 ; l <= rowx ; l++ ) {
// Leaving spaces with respect to row
for ( k = 1 ; k < tempx ; k++ )
Console.Write(" ");
// Reduce tempx value to reduce space(" ") count
tempx--;
// Printing stars
for ( k = 1 ; k <= 2*l - 1 ; k++ )
Console.Write("*");
// New line after printing a row
Console.Write("\n");
}
}
}
The following code might help:
public static void print(int no)
{
for (int i = 1; i <= no; i++)
{
for (int j = i; j <= no; j++)
{
Console.Write(" ");
}
for (int k = 1; k < i * 2; k++)
{
if(k % 2 != 0)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
Console.WriteLine();
}
Console.ReadLine();
}
Here I have created a number pyramid:
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("This is a number pyramid....");
var rows = 5;
for(int i = 1; i <= rows; i++)
{
for(int lsc = (-rows); lsc <= -2; lsc ++)
{
if(lsc < (-1)*i)
{
//write left sided blank spaces
Console.Write(" ");
}
else
{
//write left sided numbers
Console.Write(-1*(lsc));
}
}
for(int rsc = 1; rsc <= rows; rsc++)
{
//write right sided blank spaces
Console.Write(" ");
}
else
{
//Write sided numbers
Console.Write(rsc);
}
}
Console.WriteLine();
}
}
}
I have described here https://utpalkumardas.wordpress.com/2018/04/20/draw-number-pyramid
Out put is:
The is a number pyramid....
1
212
32123
4321234
543212345
I know it's javascript but might help
let n = 6;
for (let i = 1; i <= n; i++) {
let spaces = n-i;
let str = '';
for (let j=0; j < spaces; j++) {
str+=' ';
}
for (let j=0; j < i; j++) {
str+='* ';
}
console.log(str)
}
I have found two approaches:
//1st approach
int n = 6;
for (int i = 0; i < n; i++)
{
// need to print spaces
for (int j = 0; j < n - i - 1; j++)
{
Console.Write(" ");
}
// need to print * with one space
for (int k = 0; k <= i; k++)
{
Console.Write("* ");
}
Console.WriteLine();
}
// 2nd Approach
int rows = 6;
int temp = 0;
bool toggle = false;
for (int j = 0; j < rows; j++)
{
int counter = 0;
for (int i = 0; i < 2 * rows - 1; i++)
{
if (i < rows - temp - 1)
{
Console.Write(" ");
}
else
{
if (counter <= j + temp)
{
if (!toggle)
{
Console.Write("*");
toggle = true;
}
else
{
Console.Write(" ");
toggle = false;
}
}
counter++;
}
}
Console.WriteLine();
temp++;
toggle = false;
}

Random two-dimensional array of non-repeating numbers c# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I made a two dimensional array in c # with random numbers but I want the numbers are not repeated
For example, a successful output giving the input 4 (x), 4 (y), 15 (maxElem), would be:
14|8|1|7
3|13|2|4
2|6|12|8
10|9|4|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TwoDimensionArray
{
class Program
{
static void Main(string[] args)
{
int x = 4;
int y = 4;
int[,] z = new int[x, y];
Random r = new Random();
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
z[i, j] = r.Next(1, 15);
}
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (i == 3 && j == 3)
{
Console.Write(" ");
break;
}
else
{
if (z[i, j] > 9)
Console.Write(z[i, j] + " ");
else
Console.Write(z[i, j] + " ");
}
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
Generate an array containing the valid numbers, then use a shuffle algorithm to randomize the order in the array, and then finally populate your two-dimensional array by retrieving in order the values from your shuffled array.
Try doing it like this:
int x = 4;
int y = 4;
int[,] z = new int[x, y];
Random r = new Random();
var values =
Enumerable
.Range(1, x * y)
.OrderBy(n => r.Next())
.ToArray();
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
z[i, j] = values[i * 4 + j];
}
}

Categories