Sort a 2d Array of integers in C# - c#

I want to manually sort a 2D Array in C#.
I am confused on what to do when the pointer is on index 0 and I need to go to the previous row.
My problem when I reach to index 0 I cant go any further and it is then out of bounds.
int temp3 = 25;
bool swap = false;
bool swap2 = false;
long pointer = productarray[4,4];
for(int i = 4; i >= 0; i--){
for(int j = 4; j >= 0; j--){
int temp5 = 1;
while(swap != true){
if(temp3 != temp5){
pointer = productarray[i,j];
if (pointer < productarray [i, j - temp5]) {
long temporary = productarray [i, j];
productarray [i, j] = productarray [i, j - temp5];
productarray [i, j - temp5] = temporary;
temp5 = 1;
} else {
temp5++;
}
}
else{
swap = true; //Current pointer is the greatest int
temp3--;
}
}
swap = false;
}
}

Something like this
var myOrderedRows = myArray.OrderBy(row => row[columnIndex]);
from How can I sort a 2d array using Linq?

Related

Bubble sort 2D string array

How can I bubble sort a 2D string array by their lenght? In the array's zeroth column there are random generated messages and in the first column there are random generated priorities.
string[,] array = new string[50, 2];
Random r = new Random();
int number = 0;
int space = 0;
double fontossag = 0;
for (int i = 0; i < 50; i++)
{
string message = "";
int hossz = r.Next(10,51);
for (int h = 0; h < hossz; h++)
{
number = r.Next(0,101);
space = r.Next(0, 101);
if (number<=50)
{
message += (char)r.Next(97,122);
}
else if(number >= 50)
{
message += (char)r.Next(65, 90);
}
if (space<=10)
{
message += " ";
}
}
for (int f = 0; f < 50; f++)
{
fontossag = r.NextDouble() * (10.0);
}
array[i, 0] += message;
array[i, 1] += fontossag;
}
I want to sort the array by the random generated messages length.
This is my method to Bubble sort on the first column length:
public static string[,] BubbleSortStringByLength(string[,] array)
{
int num = array.GetLength(0);
for (int i = 0; i < num - 1; i++)
{
for (int j = 0; j < num - i - 1; j++)
{
if (array[j, 0].Length > array[j + 1, 0].Length)
{
// swap first column
string tmp = array[j, 0];
array[j, 0] = array[j + 1, 0];
array[j + 1, 0] = tmp;
// swap second column
tmp = array[j, 1];
array[j, 1] = array[j + 1, 1];
array[j + 1, 1] = tmp;
}
}
}
return array;
}
You can download the Visual Studio solution on GitHub
So you want to compare lengths of 1st columns and swap rows to ensure descending priority:
for (bool hasWork = true; hasWork;) {
hasWork = false;
for (int row = 0; row < array.GetLength(0) - 1; ++row) {
int priority1 = array[row, 0]?.Length ?? -1;
int priority2 = array[row + 1, 0]?.Length ?? -1;
// if we have wrong order...
if (priority1 < priority2) {
// we should keep on working to sort the array
hasWork = true;
// and swap incorrect rows
for (int column = 0; column < array.GetLength(1); ++column)
(array[row, column], array[row + 1, column]) =
(array[row + 1, column], array[row, column]);
}
}
}
Please, fiddle yourself

Control in a 2D array

[8, 8] I have a 2-dimensional array creating rows and columns [i, j].
I want to check if there is an object in a single row. For example, line 4 or line 1.
You can run a for lopp and check.
int i = 0; //or 3 //here you can set the row number
bool exists = false;
int value = 4;
for(int j = 0; j < array.GetLength(1); j++)
{
if(array[i, j] == value)
{
exists = true;
break;
}
}

How to store the results of an array matrix into a smaller array c#

I need to add a value which would be either p1(payoff one) or p2 (payoff two) to the surrounding four neighbours of a value in a matrix that'll then be printed into a new array matrix. If it's 1 then p1 will need to be added to it's neighbours or if its 0 then p2 will be added to its neighbours. I've tried to do this approach with a nested for loop but my 'if' statement in my for loop is giving me errors and Im not sure where to go next with it.
class MainClass
{
static void Main(string[] args)
{
int m, n, i, j, p1, p2;
// rows and columns of the matrix+
m = 3;
n = 3;
//Payoff matrix
p1 = 10; //cheat payoff matrix
p2 = 5; //co-op payoff matrix
int[,] arr = new int[3, 3];
Console.Write("To enter 1 it means to co-operate" );
Console.Write("To enter 0 it means to cheat");
Console.Write("Enter elements of the Matrix: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
arr[i, j] = Convert.ToInt16(Console.ReadLine());
}
}
Console.WriteLine("Printing Matrix: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(arr[i, j] + "\t");
}
Console.WriteLine();
}
// how to change the values of the matrix
int[] payoffMatrix = new int[4];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if(arr[i,j] == 1)
{
arr[i, j] = arr[i - 1, j] , arr[i + 1, j] , arr[i, j - 1] , arr[i, j + 1];
}
}
Console.WriteLine();
}
The result of the neighbouring values need to be printed into the payoff matrix aswell
If I understood correctly you need to make a copy of your array first. Because otherwise you would read e.g. "1" from a array position (i + 1) from the current iteration. That's probably not what you want.
Then you just set the desired values in your for-loop. You need some bound checking, because e.g. arrNew[i - 1] will only be accessible if i > 0
this gives you something like:
int[,] arrNew = arr.Clone() as int[,]; //creates a copy of arr
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (arr[i, j] == 1)
{
if (i > 0) //bounds checking
{
arrNew[i - 1, j] = 1;
}
if (i < m - 1) //bounds checking
{
arrNew[i + 1, j] = 1;
}
if (j > 0) //bounds checking
{
arrNew[i, j - 1] = 1;
}
if (j < n - 1) //bounds checking
{
arrNew[i, j + 1] = 1;
}
}
}
}
For a matrix:
0 0 0
0 1 0
0 0 0
the result would be
0 1 0
1 1 1
0 1 0

Keep receiving "System.ArgumentException" when array count is not even

I keep receiving the error:
Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection
When the count of array elements is not even at directions.RemoveRange(i, 2);
public static string[] way(String[] arr)
{
int[] secondaryArr = new int[arr.Length];
for (int j = 0; j < arr.Length; j++)
{
switch (arr[j])
{
case "NORTH":
secondaryArr[j] = 1;
break;
case "SOUTH":
secondaryArr[j] = -1;
break;
case "WEST":
secondaryArr[j] = 2;
break;
case "EAST":
secondaryArr[j] = -2;
break;
}
}
var directions = arr.ToList();
for (int i = 0; i < directions.Count / 2; i++)
{
for (int j = 0; j < secondaryArr.Length - 1; j++)
{
if (secondaryArr[j] + secondaryArr[j + 1] == 0)
{
directions.RemoveRange(i, 2);
i = 0;
j = 0;
}
}
}
arr = directions.ToArray();
return arr;
}
I need to reduce the directions when the route is not reasonable. For example when I receive input like: [NORTH],[SOUTH],[WEST], it should be reduced to: [WEST], because going NORTH and SOUTH is not reasonable. The problem is that I get stuck at sorting the array.

Variable parameter in the definition of array

I have the following code, which creates a 2D array in a nested loop, What I want to do is to create this 2D array each iteration but with name concatenated to "j" value . For example,
In first iteration the result will be : double[,] visualmatrix1.
I tried to put +j but it fails.
ExtractDescriptorsForm ex = new ExtractDescriptorsForm(65,10);
int a = ex.m_maxExtract;
for (int i = 0; i < m_descriptor.visualword.Length; i++)
{
for (int j = 0; j < a; j++)
{
double[,] visualmatrix = new double[m_descriptor.visualword.Length, 2];
visualmatrix[i, 0] = m_descriptor.visualword[i].identifier;
visualmatrix[i, 1] = (m_descriptor.visualword[i].tf) * (m_descriptor.visualword[i].idf);
}
}
Since your code lost the new-created array inside the loops, you should use a list to store the 2D arrays.
List<double[,]> arrays = new List<double[,]>();
ExtractDescriptorsForm ex = new ExtractDescriptorsForm(65,10);
int a = ex.m_maxExtract;
for (int i = 0; i < m_descriptor.visualword.Length; i++)
{
for (int j = 0; j < a; j++)
{
double[,] visualmatrix = new double[m_descriptor.visualword.Length, 2];
visualmatrix[i, 0] = m_descriptor.visualword[i].identifier;
visualmatrix[i, 1] = (m_descriptor.visualword[i].tf) * (m_descriptor.visualword[i].idf);
arrays.Add(visualmatrix);
}
}
Then access it by
arrays[n]

Categories