static void Main(string[] args)
{
Random r = new Random();
int[,] x = new int[10,8];
int[] temp = new int[x.Length];
// two dimensional array and i want for three dimensional array
for(int i = 0; i < temp.Length; i++)
{
temp[i] = r.Next(10, 100);
for(int j = 0; j < i; j++)
{
if(temp[i] == temp[j])
{
i--;
break;
}
}
}
for(int i = 0, index = 0; i < x.GetLength(0); i++)
{
for(int j = 0; j < x.GetLength(1); j++)
{
x[i, j] = temp[index++]; //two dimensional array unique numbers
Console.Write(x[i, j] + " ");
}
}
// i want do it as for 3D and 4 D array unique numbers like that method what can i change or add?
It's pretty straight forward to do what you're doing for higher dimensions.
Here's my code for 3D:
var r = new Random();
int [,,] x = new int[10, 8, 8];
var count =
Enumerable
.Range(0, x.Rank)
.Select(y => x.GetLength(y))
.Aggregate((y, z) => y * z);
var values =
Enumerable
.Range(10, count)
.OrderBy(y => r.Next())
.ToArray();
var v = 0;
for (var i = x.GetLowerBound(0); i <= x.GetUpperBound(0); i++)
for (var j = x.GetLowerBound(1); j <= x.GetUpperBound(1); j++)
for (var k = x.GetLowerBound(2); k <= x.GetUpperBound(2); k++)
x[i, j, k] = values[v++];
To change it to 4D these lines change:
int [,,,] x = new int[10, 8, 8, 12];
// ...
var v = 0;
for (var i = x.GetLowerBound(0); i <= x.GetUpperBound(0); i++)
for (var j = x.GetLowerBound(1); j <= x.GetUpperBound(1); j++)
for (var k = x.GetLowerBound(2); k <= x.GetUpperBound(2); k++)
for (var l = x.GetLowerBound(3); l <= x.GetUpperBound(3); l++)
x[i, j, k, l] = values[v++];
Now, in this code I have explicitly called GetLowerBound as well as GetUpperBound as it is possible in .NET code to have a non-zero based array.
Also, rather than repeatedly re-try getting random numbers until you have unique numbers I simply generated a sequence of unique numbers and then randomly sorted them. That's a little different from your original code. You needed 80 (10 x 8) random values and you were choosing from values ranging from 10 to 99 inclusive. So you had some holes in your numbers.
Random r = new Random();
int[,,] x = new int[10, 8, 8];
int[] temp = new int[x.Length];
#region one dimensional array unique numbers.
for (int i = 0; i < temp.Length; i++)
{
temp[i] = r.Next(10, 650);
for (int j = 0; j < i; j++)
{
if (temp[i] == temp[j])
{
i--;
break;
}
}
}
#endregion
for (int i = 0, index = 0; i < x.GetLength(0); i++)
{
for (int j = 0; j < x.GetLength(1); j++)
{
for (int k = 0; k < x.GetLength(2); k++)
{
x[i, j, k] = temp[index++];
Console.Write(x[i, j, k] + " ");
}
Console.WriteLine();
}
}// i think it's correct code i've changed it
Related
I am new to c# and I am trying to print parts of an integer array sorted by multiple threads. For that I use bubble sort inside my function mySort which takes part of the array as a parameter and stores it to a list that I want to display afterwards. The division of the original array is in a loop where I also start the threads. The problem is that the resulting parts are all the same although I pass the different parts of the array to a function to be executed on a thread. The resulting parts includes just numbers of the last part of the original sorted array. I would be glad for any help.
int[] myArray = new int[100];
for (int i = 0; i < myArray.Length; i++)
{
myArray[i] = r.Next(0, 100);
}
parts = new List<int[]>();
Thread[] t = new Thread[5];
int[] tmp = new int[myArray.Length/5];
int k = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < tmp.Length; j++)
{
tmp[j] = myArray[k];
k++;
}
Thread myT = new Thread(() => mySort(tmp));
t[i] = myT;
t[i].Start();
}
Function for sorting
static void mySort(int [] arr)
{
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = 0; j < arr.Length - i - 1; j++)
{
if (arr[j + 1] < arr[j])
{
int tmp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = tmp;
}
}
}
parts.Add(arr);
}
EDIT:
By declaring tmp array inside for loop the problem is solved.
for (int i = 0; i < 5; i++)
{
int[] tmp = new int[myArray.Length/5];
for (int j = 0; j < tmp.Length; j++)
{
tmp[j] = myArray[k];
k++;
}
Thread myT = new Thread(() => mySort(tmp));
t[i] = myT;
t[i].Start();
}
How to create and fill multiple arrays [5,5], and print it to console.
for now i have that, but i cant get how to dynamically create arrays.
incoming value - for example 3, its mean to create 3 arrays
and fill them with random values
int n = 5;
int count_array = Convert.ToInt32(Console.ReadLine());
int[,] arr = new int[n, n];
Random rnd = new Random();
for (var i = 0; i < n; i++)
{
for (var j = i; j < n; j++)
{
---
}
}
Like that?
int n = 5;
int count_array = Convert.ToInt32(Console.ReadLine());
int[,,] arr = new int[count_array, n, n];
Random rnd = new Random();
for (int current_array = 0; current_array < count_array; current_array++)
{
Console.WriteLine($"Populating array #{current_array}:");
for (var i = 0; i < n; i++)
{
for (var j = 0; j < n; j++)
{
int value = rnd.Next();
arr[current_array, i, j] = value;
Console.Write(value+(j==n-1?"\r\n":","));
}
}
}
Console.ReadLine();
So far I have,
int[,] array2d = new int[20, 20];
for (int i = 1; i < array2d.GetLength(0); i++)
{
for (int j = 1; j < array2d.GetLength(1); j++)
{
array2d[i, j] = i * j ;
Console.WriteLine(array2d[i, j]);
}
}
but this is skipping some quite a few numbers, I tried to fix it by checking if I is <= but that throws a IndexOutOfRangeException
Is there some point where I made a major error? or is it a simple one.
Arrays are zero-based by default; so your array is [0..19, 0..19]; however, you want a different range: [1..20, 1..20]. We should not mix them: either (better choice)
int[,] array2d = new int[20, 20];
// i, j - array indexes
for (int i = 0; i < array2d.GetLength(0); i++)
{
for (int j = 0; j < array2d.GetLength(1); j++)
{
// since i, j are array indexes we multiply (i + 1) * (j + 1)
array2d[i, j] = (i + 1) * (j + 1);
Console.Write($"{array2d[i, j],3} ");
}
Console.WriteLine();
}
Or
int[,] array2d = new int[20, 20];
// i, j are values to be multiplied
for (int i = 1; i <= array2d.GetLength(0); i++)
{
for (int j = 1; j <= array2d.GetLength(1); j++)
{
// since i, j are values we have to compute array's indexes: i - 1, j - 1
array2d[i - 1, j - 1] = i * j;
Console.Write($"{array2d[i - 1, j - 1],3} ");
}
Console.WriteLine();
}
int[,] array2d = new int[20, 20];
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++)
{
array2d[i, j] = (i+1) * (j+1) ;
Console.WriteLine(array2d[i, j]);
}
}
Your problem was that when you define array like new int[20] then it can be iterated from 0 to 19, when you try to acces its value at index 20 you get this exception because index 20 doesnt exist in this array.
When you call arrays .GetLength method it returns its count of fields which is 20, not its maximum index which is 19.
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 think this might be a pretty simple question, but I haven't been able to figure it out yet. If I've got a 2-dimensional array like so:
int[,] matris = new int[5, 8] {
{ 1, 2, 3, 4, 5,6,7,8 },
{9,10,11,12,13,14,15,16},
{ 17,18,19,20,21,22,23,24 },
{ 25,26,27,28,29,30,31,32 },
{ 33,34,35,36,37,38,39,40 },
};
and a for loop, like this:
for (int r = 0; r < 5; r++)
{
for (int j = 0; j < 8; j++)
Console.Write("{0} ", matris[r, j]);
Console.WriteLine();
}
So with this code I am printing out the multi dimensional array. But how do I print out a transpose of the array?
Just change your loops with each other:
for (int j = 0; j < 8; j++)
{
for (int r = 0; r < 5; r++)
Console.Write("{0} ", matris[r, j]);
Console.WriteLine();
}
Creating new array:
var newArray = new int[8, 5];
for (int j = 0; j < 8; j++)
for (int r = 0; r < 5; r++)
newArray[j, r] = matris[r, j];
You just need to do this:
for (int r = 0; r < 8; r++)
{
for (int j = 0; j < 5; j++)
Console.Write("{0} ", matris[j, r]);
Console.WriteLine();
}