I have a 2d arrays button [5,5] all blue color...how to randomly generate 5 red button in the array...?
int Rows = 5;
int Cols = 5;
Button[] buttons = new Button[Rows * Cols];
int index = 0;
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Cols; j++)
{
Button b = new Button();
b.Size = new Size(40, 55);
b.Location = new Point(55 + j * 45, 55 + i * 55);
b.BackColor = Color.Blue;
buttons[index++] = b;
}
}
panel1.Controls.AddRange(buttons);
As simple as this
int cnt = 0;
Random rnd = new Random();
while (cnt < 5)
{
int idx = rnd.Next(Rows * Cols);
if (buttons[idx].BackColor == Color.Blue)
{
buttons[idx].BackColor = Color.Red;
cnt++;
}
}
You will use the Random class to choose an index value between 0 and 24 and use that index to select one of your blue buttons, if the selected button has a blue backcolor, change it to Red
By the way, this works because you don't really have a 2 dimension array here.
In case your array is declared as a 2 dimension array like here
Button[,] buttons = new Button[Rows, Cols];
then you need two random values at each loop, one for the row and one for the column
int cnt = 0;
Random rnd = new Random();
while (cnt < 5)
{
int row = rnd.Next(Rows);
int col = rnd.Next(Cols);
if (buttons[row, col].BackColor == Color.Blue)
{
buttons[row, col].BackColor = Color.Red;
cnt++;
}
}
Related
I've created a dimensional array using two for loops, and as each inner loop completes, I'd like those values to go into textboxes on a windows form. (I realize that the values will be overwritten each time, with just the values from the final outer loop showing at the end.)
The comments in the code show what I've been trying to get to work.
Thanks!
if (IsValidData())
{
int Columns = 5;
int Rows = Convert.ToInt32(txtNumbDrawings.Text);
int[,] ball = new int[Columns, Rows];
for (int i = 0; i < Rows; i++)
{
List<int> ballsList = new List<int>();
int[] txtBall = new int[Columns];
for (int j = 0; j < Columns; j++)
{
Random number = new Random();
ball[i, j] = number.Next(1, 70);
// prevent duplicate numbers
while (ballsList.Contains(ball[i, j] ))
{
ball[i, j] = number.Next(1, 70);
}
ballsList.Add(ball[i, j]);
/*************************************************
* how do I get the current ball value
* into a textbox on a form?
*
* textboxes are named txtBall1, txtBall2, etc...
*
* I was trying something like:
*
* TextBox[] txtBall = new TextBox[5];
* for (int i = 0; i < 5; i++)
* {
* txtBall[i + 1].Text = ball[i, j].ToString();
* }
*************************************************/
}
ballsList = null;
}
}
Windows Form Image: https://1drv.ms/u/s!Ak6hxvO3Ye6Oj5I9NQy834Yk4TRLyw?e=DniwK8
If I understand what you're trying to do, and that seems to be assigning a shuffled list of integers to one of 5 list boxes, then this should do it:
if (IsValidData())
{
Random rnd = new Random();
// (1, 69) because rnd.Next(1, 70) only produces the numbers 1 to 69
int[] shuffled_balls = Enumerable.Range(1, 69).OrderBy(x => rnd.Next()).ToArray();
ListBox[] list_boxes = new [] { lstBall1, lstBall2, lstBall3, lstBall4, lstBall5 };
for (int i = 0; i < shuffled_balls.Length; i++)
{
list_boxes[i % list_boxes.Length].Items.Add(shuffled_balls[i]);
}
}
I have to write a program that creates a two dimensional array and fills it like a snake. Here is the task itself:
Blockquote write a program that creates a two-dimensional numeric array and this array is filled with a "snake": first the first row (from left to right), then the last column (from top to bottom), the last row (from right to left), the first column (from bottom to top), the second row ( left to right), etc.
I tried to write something, but bugs constantly appear. Please help to shorten this code and make it correct.
class Program
{
static void Main(string[] args)
{
Random rand = new Random();
int len = rand.Next(5, 20);
int wid = rand.Next(5, 20);
int[,] arr = new int[len, wid];
int num = 10;
int line = 0;
int line2 = len;
int col = wid - 1;
int col2 = 0;
for (int i = 0; i <= (len > wid ? len : wid); i++)
{
for (int k = 0; k < wid; k++, num++)
{
arr[line, k] = num;
}
line++;
for (int k = 0; k < len; k++, num++)
{
arr[k, col] = num;
}
col++;
for (int k = (wid - 1); k >= 0; k--, num++)
{
arr[line2, k] = num;
}
line2++;
for (int k = 0; k < (len - 1); k++, num++)
{
arr[k, col2] = num;
}
col2++;
}
for (int i = 0; i <= len; i++)
for (int q = 0; q <= wid; q++)
{
Console.WriteLine(arr[i, q] + " ");
}
}
}
I have a 2d array[,] and I need to delete the border of it (or create a new array without it). I tried everything I thought of and nothing worked since I don't usually work with 2d arrays. Help please?
1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7
becomes:
6 7
1 2
Queue<int> numbers = new Queue<int>();
int[,] b = new int[4, 4];
for (int i = 0; i < b.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
int x = int.Parse(Console.ReadLine());
b[i, j] = x;
}
}
int rows = b.GetLength(0);
Console.WriteLine("Number of rows: "+rows);
int columns = b.GetLength(1);
Console.WriteLine("Number of columns: " + columns);
int[,] arrayUpdated = new int[columns - 2,rows - 2];
for (int n = 0; n < b.GetUpperBound(1); n++)
{
for (int i = 0; i < b.GetUpperBound(0); i++)
{
arrayUpdated[i, n] = b[i, n];
Console.WriteLine(arrayUpdated[i, n]);
}
}
public static T[,] RemoveBorders<T>(T[,] source)
{
//index of last item, not number of items
var ROW = source.GetUpperBound(0);
if (ROW < 2) throw new ArgumentException("source array is not tall enough");
var COL = source.GetUpperBound(1);
if (COL < 2) throw new ArgumentException("source array is not wide enough");
var result = new T[ROW - 1, COL - 1];
for (int r = 1; r < ROW; r++)
{
for (int c = 1; c < COL; c++)
{
result[r-1, c-1] = source[r, c];
}
}
return result;
}
https://dotnetfiddle.net/dtUpRA
You would use it in the code in the question like this:
int[,] arrayUpdated = RemoveBorders(b);
I'm really struggling to find a way to put 10 int numbers ( from 1 to 10) twice in a 20-position array on a random position in C#.Everything I have already tried takes too much time and makes my programme run really slow.
Ok here's the code I have already tried but doesn't really work well.
for (int j = 1; j <= 10; j++)
{
k = 0;
while (k < 2)
{
rnd = new Random();
numb = rnd.Next(1, 20);
//w = numbers[numb].ToString(); not actually working as well
if (numbers[numb] == '\0')
{
numbers[numb] = j;
k++;
}
}
}
So what I'm really trying to do here,is to put all the int numbers from 1 to 10
twice in this 20-position array on random position.Sorry for not posting my code before,but I was from my phone and it was really late.For example:
[1,3,7,9,5,3,4,10,7,8,8,2,1,4,5,6,10,9,2,6]
Try something like this:
int[] array = new int[20];
Random rand = new Random();
for (int i = 0; i < 20; i++)
{
int num = rand.Next(1, 10);
int pos = rand.Next(0, 19);
while (array[pos] != 0)
{
if (pos == 19)
pos = 0;
else
pos++;
}
array[pos] = num;
}
or if you want the positions to be random, but not the numbers and exactly two of each number, try this:
int[] array = new int[20];
Random rand = new Random();
for (int i = 1; i <= 10; i++)
{
int num = i;
int pos1 = rand.Next(0, 19);
while (array[pos1] != 0)
{
if (pos1 == 19)
pos1 = 0;
else
pos1++;
}
array[pos1] = num;
int pos2 = rand.Next(0, 19);
while (array[pos2] != 0)
{
if (pos2 == 19)
pos2 = 0;
else
pos2++;
}
array[pos2] = num;
}
I am making a array filled with random numbers in c# but I can't get it to work.
int[,] array = new int[10, 5];
int x, y;
x = 0;
y = 0;
while (y <= 5)
{
Random r = new Random();
int rand = r.Next(-50, 50);
array[x, y] = rand;
if (x == 10)
{
x = 0;
y++;
}
x++;
}
Use nested for loops, it is much easier:
Random rnd = new Random();
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5; j++)
{
array[i, j] = rnd.Next(-50, 50);
}
}
Your while loop is not readable but it's correct,except you should change while (y <= 5) to while (y < 5) otherwise you will get an IndexOutOfRangeException. And you should define your Random instance outside of the loop.
int[,] array = new int[10, 5];
int x = 0, y = 0;
Random r = new Random();
while (y < 5)
{
int rand = r.Next(-50, 50);
array [x, y] = rand;
x++;
if (x == 10)
{
x = 0;
y++;
}
}
Try this:
int[,] array = new int[10, 5];
Random rnd = new Random();
for (int row = 0; row < 10; row++)
{
for (int col = 0; col < 5; col++)
{
array[row, col] = rnd.Next(-50, 50);
}
}
you need to change yours if or array declaration to
int[,] array = new int[11, 6];
also there is other problem, you need to create random before while
Random r = new Random();
while (y <= 5)
{
to print out the values you can use for
for (int i = 0; i < array.GetLength(0); i++)
{
for (int j = 0; j < array.GetLength(1); j++)
Console.WriteLine(array[i, j]);
}
int[,] array = new int[10, 5];
int x, y;
x = 0;
y = 0;
while (y < 5)
{
Random r = new Random();
int rand = r.Next(-50, 50);
array[x, y] = rand;
if (x == 9)
{
x = 0;
y++;
}
x++;
}
Let's see if this works.