Custome Size 2D Array with Fitting For Loops - c#

I am creating a 2D array that can have any custom size 1x1, 2x2, 3x3 .... nxn and depending on what the size of the array is, it gets filled with 0 to (n^2)-1, so if it is 2x2 it outputs:
3 2
1 0
It should be something like this int[,] arr = new int [n, n];, to create the 2d array, but how would the for loops be constructed?
what I have tried
using System;
namespace moveElement {
public class move {
static void Main(string[] args) {
int n = 3;
int[, ] arr = new int[n, n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
}
}
The result of the code:
0 0 0
0 0 0
0 0 0

int k = 0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
arr[i,j] = k++;
}
}

full code to the question:
using System;
namespace moveElement
{
public class move
{
static void Main(string[] args)
{
int n = 3;
int[,] arr = new int [n, n];
//Ivan Smyrnov solution
int k = (n * n) - 1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
arr[i,j] = k--;
}
}
//Ivan Smyrnov solution
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
}
}
Output when n is 3:
8 7 6
5 4 3
2 1 0

Related

C# 2DArray IndexOutOfBound, Why?

In the following C# code I want to create a 2D Array with the size of [5,2].
If I understand it correctly, that should look like this:
0 1 2
0[0,0] [0,1] [0,2]
1[1,0] [1,1] [1,2]
2[2,0] [2,1] [2,2]
3[3,0] [3,1] [3,2]
4[4,0] [4,1] [4,2]
But why does my program throw out an
IndexOutOfRangeException
...?
using System;
namespace program {
class program{
Random random = new Random();
int[,] board = new int[5,2];
public program(){
Print2DArray(randomBoard(0.5,board));
}
public int[,] randomBoard(double chance, int[,] board){
int[,] temp = board;
for(int y = 0; y < board.GetLength(1); y++){
for(int x = 0; x < board.GetLength(0); x++){
if(random.NextDouble() <= chance){
board[y,x] = 1;
} else {
board[y,x] = 0;
}
}
}
return temp;
}
public static void Print2DArray<T>(T[,] matrix)
{
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i,j] + "\t");
}
Console.WriteLine();
}
}
static void Main(string[] args){
program program1 = new program();
}
}
}
In your code, y goes from 0 to number of elements in dimension 1
while x goes 0 to number of elements in dimension 0
However, inside your for loop you seem to have the dimensions swapped,
board[y,x] = 1; // change this to board [x,y] = 1;
similarly for the else condition it should be board[x,y] = 0

calculate matrix east west diagonal in C#

any body can help me for a program
I want to make a code that can fill a matrix that way in C#
0 0 0 0 5
0 0 0 4 0
0 0 3 0 0
0 2 0 0 0
1 0 0 0 0
i am working on it for a week but i dident find a solution
thank you
Here is your solution. At least try to understand how it works.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
var n = 5;
var result = Enumerable.Range(0,n*n).GroupBy(k=> k/n).Select(g=> g.Select(k=> (k % n == (n-1-k/n)) ? k%n+1 : 0).ToArray()).ToArray();
foreach(var r in result)
{
foreach(var a in r)
{
Console.Write(" {0}", a);
}
Console.WriteLine();
}
}
}
}
thank you i found a solution
using System;
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
int[,] arr = new int[5, 5];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (i + j == 4)
{
arr[i, j] = j + 1;
}
else
{
arr[i, j] = 0;
}
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
}
}
}
Here's my attempt:
int[,] arr = new int[n,n];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(i+j-1 == n)
{
arr[i,j] = j+1;
}
else
{
arr[i,j] = 0;
}
Console.Write(arr[i,j] + " ");
}
Console.WriteLine();
}
I haven't checked if this specific code works, but something like this should do the trick.

Multiplication table using 2D arrays and methods

I'm a new student having a bit of trouble with this assignment, but the powerpoint notes and other online guides don't seem to help. If anyone can give me a pointer it would be much appreciated!
private static int[,] GenerateTT(int size)
{
int[,] table = new int[size,size];
for (int i = 1; i < size+1; i++)
{
for (int j = 1; j < i+1; j++)
{
table[i-1, j-1] = i * j;
}
}
return table;
}
private static void DisplayTT(int[,] table)
{
Console.WriteLine();
Console.WriteLine("Here is the times table for that size:");
Console.WriteLine();
for (int i = 1; i <= table.Length; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("{0}\t", table[i-1, j-1]);
}
Console.WriteLine("\n");
}
Console.WriteLine();
}
The output is supposed to be like this (if you enter 4 for example):
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
But what I'm getting is this, plus it crashes at DisplayTT(table):
1
2 4
3 6 9
4 8 12 16
here's the relevant part of the Main method if it helps.
int size = GetValue("Please enter the size (4-20) of the times table: ", 4, 20);
Console.WriteLine();
int[,] table = GenerateTT(size);
DisplayTT(table);
You can use GetLength(X) Property for multi dimensional arrays where X is index of dimension.
for (int i = 0; i < table.GetLength(0); i++)
{
for (int j = 0; j < table.GetLength(1); j++)
{
Console.Write("{0}\t", table[i, j]);
}
Console.WriteLine("\n");
}
You should change the inner loop hi-bound:
private static int[,] GenerateTT(int size)
{
int[,] table = new int[size,size];
for (int i = 1; i < size+1; i++)
{
for (int j = 1; j < i+1; j++) // <-- change i+1 to size+1
{
table[i-1, j-1] = i * j;
}
}
return table;
}
I'd rather keep the loops start from zero:
private static int[,] GenerateTT(int size)
{
int[,] table = new int[size,size];
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
table[i, j] = (i+1) * (j+1);
}
}
return table;
}

Count No Of 1's Colony From [1,0] 2D Array Using C#

I have a 2D array of integers which stores only values [1,0] as shown below.
int[,] INPUT = new int[10, 10] {
{1,1,0,0,0,1,1,1,0,0},
{1,0,0,0,1,0,0,0,1,0},
{0,0,0,1,1,0,0,1,0,1},
{1,1,0,0,0,0,0,1,1,0},
{0,1,0,0,0,1,0,1,1,0},
{0,0,0,0,1,0,1,0,0,0},
{1,0,0,1,0,0,1,1,1,0},
{0,1,0,0,1,1,0,0,1,1},
{0,1,1,1,0,1,0,1,1,0},
{0,0,0,0,0,1,1,0,0,0},
};
I need to identify a Colony count for the input image, where a Colony is defined as a contiguous sequence of 1s (Like in this array, there are 3 colony of 1's). Connection between 1s can be either adjacent or on the diagonal.
I am required to use recursion for this.
class Program
{
public static int count;
public static int colony;
public static int[,] INPUT;
public static int rows = 10;
public static int cols = 10;
static void Main(string[] args)
{
int[,] INPUT = new int[10, 10] {
{1,1,0,0,0,1,1,1,0,0},
{1,0,0,0,1,0,0,0,1,0},
{0,0,0,1,1,0,0,1,0,1},
{1,1,0,0,0,0,0,1,1,0},
{0,1,0,0,0,1,0,1,1,0},
{0,0,0,0,1,0,1,0,0,0},
{1,0,0,1,0,0,1,1,1,0},
{0,1,0,0,1,1,0,0,1,1},
{0,1,1,1,0,1,0,1,1,0},
{0,0,0,0,0,1,1,0,0,0},
};
// Showing All Values From Upper 2D array
Console.WriteLine("Input File Contain: \n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write(INPUT[i, j]);
}
Console.WriteLine();
}
// Getting Total 1's Count Only
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (INPUT[i, j] == 1)
{
count = count + 1;
}
}
Console.WriteLine();
}
// Getting Total 1's Colony Only
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (INPUT[i, j] == 1)
{
INPUT[i, j] = 0;
colony = GetColony(i, j);
}
}
}
Console.WriteLine("Counts Of 1 Are: " + count);
Console.WriteLine("Counts Of 1's Colony Are: " + colony );
}
static public int GetColony(int i, int j)
{
if ((i < rows) && (j < cols))
{
if ((INPUT[i - 1, j] == 1) || (INPUT[i + 1, j] == 1) || (INPUT[i, j - 1] == 1) || (INPUT[i, j + 1] == 1))
{
INPUT[i, j] = 0;
return GetColony(i, j);
}
else
{
return 1;
}
}
else
{
return 1;
}
}
}

Shuffling a 2D array without using Collections

I don't know how to shuffle 2D array without duplicate elements. Can anyone help me to shuffle a 2D array?
Here is what I have so far:
public class Shuffle2DArray
{
public Shuffle2DArray ()
{
}
public static void Main(string[] args)
{
int[,] a = new int[3, 3] { { 1, 2, 3, }, { 6, 7, 8 }, { 11, 12, 13 } };
Shuffle2DArray shuffle = new Shuffle2DArray ();
shuffle.getshuffle2D (a);
}
void getshuffle2D(int[,] arr)
{
Random ran = new Random ();
for (int i = 0; i < arr.GetLength (0); i++) {
for (int j = 0; j < arr.GetLength (1); j++) {
int m = ran.Next(arr.GetLength (0)-1);
int n = ran.Next(arr.GetLength (1)-1);
int temp = arr[0,j];
arr[i,0] = arr[m,n+1];
arr[m,n] = temp;
Console.Write(arr[i,j]+ "\t");
}
Console.WriteLine();
}
}
}
Well, I would say shuffle the 2d array the same way as you shuffle the 1d array.
For instance, Fisher–Yates shuffle for 1d array is something like this
public static class Utils
{
public static void Swap<T>(ref T a, ref T b) { var temp = a; a = b; b = temp; }
public static void RandomShuffle<T>(this T[] target, Random random = null)
{
if (target.Length < 2) return;
if (random == null) random = new Random();
for (int i = target.Length - 1; i > 0; i--)
{
int j = random.Next(i + 1);
if (i != j) Swap(ref target[i], ref target[j]);
}
}
}
All you need is to realize that having a 2d array
T[,] array
and accessing the element of the array
array[row, column]
that
row = index / columnCount
column = index % columnCount
where
index = [0, array.Lenght - 1] corresponds to the index in 1d array
columnCount = array.GetLength(1)
adding the 2d version function to the class above is trivial
public static class Utils
{
// ...
public static void RandomShuffle<T>(this T[,] target, Random random = null)
{
if (target.Length < 2) return;
if (random == null) random = new Random();
int columnCount = target.GetLength(1);
for (int i = target.Length - 1; i > 0; i--)
{
int j = random.Next(i + 1);
if (i != j) Swap(ref target[i / columnCount, i % columnCount], ref target[j / columnCount, j % columnCount]);
}
}
}
Sample usage:
int[,] a = new int[3, 3] { { 1, 2, 3, }, { 6, 7, 8 }, { 11, 12, 13 } };
a.RandomShuffle();
You need to first order your array by random sequence of numbers. What you are doing now is just changing two random items of 2d array at each iterate thus it may result in duplicate items.
Look at this Sort algorithm for 1d array.
for (int i = 0; i < arr.Length - 1; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] > arr[j]) // ran.Next(-1,1) == 0 // or any random condition
{
int temp = arr[i];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
As you can see we need 2 loops to sort 1d array. So in order to sort 2d array we need 4 loops.
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(0); j++)
{
for (int k = 0; k < arr.GetLength(1); k++)
{
for (int l = 0; l < arr.GetLength(1); l++)
{
if (arr[i, k] > arr[j, l]) // ran.Next(-1,1) == 0
{
int temp = arr[i, k];
arr[i, k] = arr[j, l];
arr[j, l] = temp;
}
}
}
}
}
Then write another algorithm to print items.
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i, j] + "\t");
}
Console.WriteLine();
}
This was Sort algorithm. Now if you just change this condition with random one you sort your array by random.
Change if (arr[i, k] > arr[j, l])
To if (ran.Next(-1,1) == 0). this is just randomly true or false.

Categories