must save the position of the array element - c#

I used the translator. I'm sorry.
Hello!
C# I'm a beginner.
Please help me because I'm having a hard time writing....
Whenever the coordinates of the array change, I want to save the changed coordinates in a different array.
What should I do?
Please advise.
You just have to show the direction.
Thank you!
string size = Console.ReadLine();
int Consize = Convert.ToInt32(size);
int[,] array = new int[Consize, Consize];
for (int i = 0; i < Consize; i++)
{
string value = Console.ReadLine();
string[] result = value.Split();
for (int j = 0; j < result.Length; j++)
{
int value2 = Convert.ToInt32(result[j]);
array[i, j] = value2;
this is an array A that stores the entered array.
int x = 0, y = 0;
string movePath = "";
int _max = 0;
while (x != Consize && y != Consize)
{
int[,] _array = new int[x, y];
int max = -101;
int[] temp = new int[4] { -101, -101, -101, -101 };
if (x - 1 > 0)
temp[0] = array[x - 1, y];
if (y - 1 > 0)
temp[1] = array[x, y - 1];
if (x + 1 < Consize)
temp[2] = array[x + 1, y];
if (y + 1 < Consize)
temp[3] = array[x, y + 1];
int index = 0;
for (int i = 0; i < temp.Length; i++)
{
if (max < temp[i])
{
max = temp[i];
index = i;
}
}
switch (index)
{
case 0:
movePath += "U";
_max += temp[0];
x--;
break;
case 1:
movePath += "L";
_max += temp[1];
y--;
break;
case 2:
movePath += "D";
_max += temp[2];
x++;
break;
case 3:
movePath += "R";
_max += temp[3];
y++;
break;
default:
break;
}
Compare each element of the array
They move on to the big factor.
I want to save the coordinates of the moving element in a different arrangement.

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

filling the array with a random number, from 0 to 2, but so that the opposite cell is filled depending on the condition

Working on a grading model, but stuck on filling an array.
I need to fill the array with numbers from 0 to 2, but:
- so that the main diagonal is filled only 1;
- if, for example, the element a (31) = 2, then a (13) must be 0, and a set of;
- if for example the element a (31) = 1, then a (13) must be 1, and a set of;
The principle itself, I understand.
if (array[i+1,j] = 2)
{
(array[i, j+1] = 1)
}
it's wrong i know
for now i have
int[,] array = new int[5, 5];
Random rnd = new Random();
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
array[i, j] = rnd.Next(0, 3);
if (i == j)
{
array[i, j] = 1;
}
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
Console.Write("{0}\t", array[i, j]);
}
Console.WriteLine();
}
here more detailes
Please check does this work for you(hope understood task correctly):
var arr = new int[4, 4];
var rnd = new Random();
var length = arr.GetLength(0);
for (var i = 0; i < length; i++)
{
for (var j = i; j < length; j++)
{
if (i == j)
{
arr[i, j] = 1;
}
else
{
var curr = rnd.Next(0, 2);
arr[i, j] = curr;
var reverse = curr switch
{
0 => 2,
1 => 1,
2 => 0,
_ => throw new Exception("Should not happen")
};
// or if C# 8.0 is not way to go:
//int reverse;
//switch (curr)
//{
// case 0: reverse = 2; break;
// case 1: reverse = 1; break;
// case 2: reverse = 0; break;
// default: throw new Exception("Should not happen");
//}
arr[j, i] = reverse;
}
}
}

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.

Creating an isosceles triangle in C3

The code below only makes a right angle triangle, how would I make it into an isosceles triangle?
int height = 4;
string star = "";
for (int i = 0; int i < height; i++)
{
star += "*";
Console.WriteLine(star);
}
Console.ReadLine();
This only displays a right angle triangle. What I attempted to make was a pyramid.
Here you have a cleaner code:
int numberoflayer = 4;
int empty;
int number;
for (int i = 1; i <= numberoflayer; i++)
{
for (empty = 1; empty <= (numberoflayer - i); empty++)
Console.Write(" ");
for (number = 1; number <= i; number++)
Console.Write('*');
for (number = (i - 1); number >= 1; number--)
Console.Write('*');
Console.WriteLine();
}
This draws your christmas tree:
int height = 4;
for (int i = 0; i < height; i++)
{
int countSpaces = (int)Math.Ceiling((height * 2 / 2d) - i);
int countStars = 1 + (i * 2);
string line = new string(' ', countSpaces) + new string('*', countStars);
Console.WriteLine(line);
}
Dirty code but there you go
int height = 4;
string empty = " ";
String star = "";
for(int i = 0; i<height; i++)
{
star += " *";
empty = empty.Length > 0 ? empty.Remove(0,1) : " ";
Console.WriteLine(empty + star);
}
Console.ReadLine();

In C# arrays, how not to create duplicated random numbers?

I'm a beginner in C#, trying to make a lottery form applicaton.
There are types, first when you have 5 tips ( otos bool ) and 5 tips ( hatos bool ).
And there are many types of how many numbers will be raffled (tiz, harminc, kilencven, negyvenot).
I tried to scan the numbers after the raffle with Array.Equals with this code:
for (int i = 0; i <= 4; i++)
{
for (int y = 0; y <= 4; y++)
{
if (Array.Equals(lottoszamok[i], lottoszamok[y]))
lottoszamok[i] = r.Next (1, ?);
}
}
but at this the number will be scanned with itself too, so it will be always equal.
here is my code by the way:
if (otos == true)
{
for (int i = 0; i <= 5; i++)
{
if (tiz == true)
{
lottoszamok[i] = r.Next(1, 10);
}
else if (harminc == true)
{
lottoszamok[i] = r.Next(1, 30);
}
else if (kilencven == true)
{
lottoszamok[i] = r.Next(1, 90);
}
else if (negyvenot == true)
{
lottoszamok[i] = r.Next(1, 45);
}
else if (egyeni == true)
{
lottoszamok[i] = r.Next(1, (egyeniertek + 1));
}
}
}
if (hatos == true)
{
for (int i = 0; i <= 6; i++)
{
if (tiz == true)
{
lottoszamok[i] = r.Next(1, 10);
}
else if (harminc == true)
{
lottoszamok[i] = r.Next(1, 30);
}
else if (kilencven == true)
{
lottoszamok[i] = r.Next(1, 90);
}
else if (negyvenot == true)
{
lottoszamok[i] = r.Next(1, 45);
}
else if (egyeni == true)
{
lottoszamok[i] = r.Next(1, (egyeniertek + 1));
}
}
}
If you're trying to pick numbers from a range 1..n without repetitions, you need to "shuffle" the numbers out:
int[] allPossibleNumbers = Enumerable.Range(1, maxNumber).ToArray();
int[] picked = new int[numberToPick];
for (int i = 0; i < numberToPick; i++)
{
int index = r.Next(i, maxNumber);
picked[i] = allPossibleNumbers[index];
allPossibleNumbers[index] = allPossibleNumbers[i];
}
where numberToPick is 5 if otos or 6 if hatos, and maxNumber depends on tiz, harminc, kilencven, negyvenot, egyeni and egyeniertek.
If your maxNumber is huge and you only want to pick a few numbers, the following doesn't require the whole range to be in memory at once:
Dictionary<int, int> outOfPlace = new Dictionary<int,int>();
int[] picked = new int[numberToPick];
for (int i = 0; i < numberToPick; i++)
{
int shuffleOut = outOfPlace.ContainsKey(i) ? outOfPlace[i] : i;
int index = r.Next(i, maxNumber);
picked[i] = 1 + (outOfPlace.ContainsKey(index) ? outOfPlace[index] : index);
outOfPlace[index] = shuffleOut;
outOfPlace.Remove(i);
}
Try this one!
if (i!=y && Array.Equals(lottoszamok[i], lottoszamok[y]))
I made it this way, if you want you could put swapping like method.
static void SwapInts(int[] array, int position1, int position2)
{
// Swaps elements in an array.
int temp = array[position1]; // Copy the first position's element
array[position1] = array[position2]; // Assign to the second element
array[position2] = temp; // Assign to the first element
}
static void Main()
{
Random rng = new Random();
int n = int.Parse(Console.ReadLine());
int[] intarray = new int[n];
for (int i = 0; i < n; i++)
{
// Initialize array
intarray[i] = i + 1;
}
// Exchange resultArray[i] with random element in resultArray[i..n-1]
for (int i = 0; i < n; i++)
{
int positionSwapElement1 = i + rng.Next(0, n - i);
SwapInts(intarray, i, positionSwapElement1);
}
for (int i = 0; i < n; i++)
{
Console.Write(intarray[i] + " ");
}
}
}
I spend many time to get this, but i believe i can do it, now it's done, By the Easier way in the word, this kill every think about Random not duplicate,very simply code without any philosophy or difficulty of Developers made ... (welcome to my work) that (BEST OF THE BEST):
Numbers between (1-10) without any duplicate, 1- MY WORK in C#
private void TenNumbersRandomly()
{
int[] a = new int[10];
Random r = new Random();
int x;
for (int i = 0; i < 10; i++)
{
x= r.Next(1, 11);
for (int j = 0; j <= i ; j++)
{
while (a[j] == x)
{
x = r.Next(1, 11);
j = 0;
}
}
a[i] = x;
tb1.Text += a[i]+"\n";
}
}
2- in VB some Different i also have it :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim x As Integer, i As Integer, j As Integer
x = Int(Rnd() * 10) + 1
Label1.Text = ""
Dim a(9) As Integer
For i = 0 To 9
x = Int(Rnd() * 10) + 1
For j = 0 To i
While (a(j) = x)
x = Int(Rnd() * 10) + 1
j = 0
End While
Next j
a(i) = x
Label1.Text += a(i).ToString() + " "
Next i

Categories