Multidimensional array adding controls - c#

How can i make the below code more efficient, with less lines.
Im adding some PictureBox elements to a two dimensinal array.
int a = 0;
int b = 0;
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Iteration: " + i + " a = " + a);
Console.WriteLine("Iteration: " + i + " b = " + b);
pictureBoxArr[a, b] = new PictureBox();
b++;
}
int aa = 1;
int bb = 0;
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Iteration: " + i + " aa = " + aa);
Console.WriteLine("Iteration: " + i + " bb = " + bb);
pictureBoxArr[aa, bb] = new PictureBox();
bb++;
}
int aaa = 2;
int bbb = 0;
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Iteration: " + i + " aaa = " + aaa);
Console.WriteLine("Iteration: " + i + " bbb = " + bbb);
pictureBoxArr[aaa, bbb] = new PictureBox();
bbb++;
}
I was thinking something like this - but im kinda stuck.
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; i++)
{
}
}

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
pictureBoxArr[i, j] = new PictureBox();
}
}
You almost had it.
If you want something more re-usable, you could set up a couple variables to hold your bounds.
int boundX = 10;
int boundY = 10;
for (int i = 0; i < boundX ; i++)
{
for (int j = 0; j < boundY ; j++)
{
pictureBoxArr[i, j] = new PictureBox();
}
}

Try:
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.WriteLine("Iteration: " + i + " a = " + i);
Console.WriteLine("Iteration: " + j + " b = " + j);
pictureBoxArr[i, j] = new PictureBox();
}
}
Dude, you are very close

Related

How to append Int array to file horizontally?

I am trying to writ to a file horizontally the best i can do is write them to file vertically.
So Instead of printing to the file
1
2
They print to the file
1 2
Code
int[] test3 = new int[2];
Random randNum3 = new Random();
for (int i = 0; i < test3.Length; i++)
{
test3 = Enumerable.Range(1, 11).OrderBy(x => randNum3.NextDouble()).Take(2).ToArray();
}
int[] b = test3;
int u;
for (int i = 0; i < b.Length; i++)
{
// Console.Write(" " + b[i] + " ");
}
for (int j = 0; j <= b.Length - 2; j++)
{
for (int i = 0; i <= b.Length - 2; i++)
{
if (b[i] > b[i + 1])
{
u = b[i + 1];
b[i + 1] = b[i];
b[i] = u;
}
}
}
System.IO.File.AppendAllLines("C:\\Users\\Gandalf\\Desktop\\log.txt", b.Select(i => i.ToString()).ToArray());
Ok, this will append the text "horizontally", instead of using AppendAllLines, we need to use AppendAllText, and the IEnumerable should be transformed to a string
int[] test3 = new int[11];
Random randNum3 = new Random();
for (int i = 0; i < test3.Length; i++)
{
test3 = Enumerable.Range(1, 11).OrderBy(x =>
randNum3.NextDouble()).Take(2).ToArray();
}
int[] b = test3;
int u;
for (int i = 0; i < b.Length; i++)
{
// Console.Write(" " + b[i] + " ");
}
for (int j = 0; j <= b.Length - 2; j++)
{
for (int i = 0; i <= b.Length - 2; i++)
{
if (b[i] > b[i + 1])
{
u = b[i + 1];
b[i + 1] = b[i];
b[i] = u;
}
}
}
var write = $" {String.Join(" ", b.Select(x => x.ToString()))}";
File.AppendAllText("lines.txt", write);
You need to insert a newline between each value. So
So use this to convert an array to a string:
String.Join(Environment.NewLine, a);

How to visualize the steps of an algorithm

How can I delay execution of an algorithm to visually display the results of each iteration in the algorithm? When I try to update the height of the objects in the code below, it only shows the final result. How can I display what happened at each step?
namespace BubbleSortRappresentazione_grafica
{
public partial class MainWindow : Window
{
int[] v;
public MainWindow()
{
InitializeComponent();
Random rnd = new Random();
v = new int[10];
for (int j = 0; j < 10; j++)
{
v[j] = rnd.Next(0, 100);
}
_0.Height = v[0] + 20;
_1.Height = v[1] + 20;
_2.Height = v[2] + 20;
_3.Height = v[3] + 20;
_4.Height = v[4] + 20;
_5.Height = v[5] + 20;
_6.Height = v[6] + 20;
_7.Height = v[7] + 20;
_8.Height = v[8] + 20;
_9.Height = v[9] + 20;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// algoritmo ordinamento
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (v[j] > v[i])
{
int tmp = v[j];
v[j] = v[i];
v[i] = tmp;
_0.Height = v[0] + 20;
_0.Content = v[0];
_1.Height = v[1] + 20;
_1.Content = v[1] ;
_2.Height = v[2] + 20;
_2.Content = v[2];
_3.Height = v[3] + 20;
_3.Content = v[3] ;
_4.Height = v[4] + 20;
_4.Content = v[4];
_5.Height = v[5] + 20;
_5.Content = v[5];
_6.Height = v[6] + 20;
_6.Content = v[6];
_7.Height = v[7] + 20;
_7.Content = v[7];
_8.Height = v[8] + 20;
_8.Content = v[8];
_9.Height = v[9] + 20;
_9.Content = v[9];
Thread.Sleep(100);
}
}
}
}
}
}
I believe that what you're really looking for is Application.DoEvents method:
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (v[j] > v[i])
{
//...
Thread.Sleep(100);
Application.DoEvents();
}
}
}
This will cause that whatever visual changes you're making inside the loop will be applied in each iteration. You can also consider removing the .Sleep(), as I'm not sure how useful it is for you.

pyramid structure alignment is not in place

Pyramid structure in c#. I used Environment.NewLine to break line but this not getting pyramid structure
int i, j, k, n;
n = Convert.ToInt32(TextBox3.Text);
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++)
{
Label4.Text += "";
}
for (k = 0; k < 2 * i - 1; k++)
{
Label4.Text += "*";
}
Label4.Text += Environment.NewLine;
}
your first inner loop ,
replace this code :
for (j = i; j < n; j++)
{
Label4.Text += "";
}
output :
*
**
***
****
with:
for (j = i; j < n; j++)
{
Label4.Text += " "; // just add one space in string
}
output :
*
* *
* * *
* * * *
if you're working with asp.net you need <br /> for line breaks and for space
int n;
string Pyramid = string.Empty;
n = Convert.ToInt32(TextBox3.Text);
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
Pyramid += " ";
}
for (int k = 0; k < 2 * i - 1; k++)
{
Pyramid += "*";
}
Pyramid += "<br />";
}
Label4.Text = Pyramid;
solution to my problem -
int i, j, k, n;
n = Convert.ToInt32(TextBox3.Text);
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++)
{
Label4.Text += " ";
}
for (k = 0; k < 2 * i - 1; k++)
{
Label4.Text += "*";
}
Label4.Text += "</br>";

How to find that one matrix is submatrix of the other in C#?

I want to find that a given matrix is a sub matrix of the other.
I have tried below piece of code but I am not sure that it would work:-
for (int i = 0; i < a.length - b.length + 1; i++) {
for (int j = 0; j < a[0].length - b[0].length + 1; j++) {
boolean submatrix = true; // at start we assume we have a submatrix
for (int k = 0; k < b.length; ++k) {
for (int l = 0; l < b[0].length; ++l) {
if (a[i + k][j + l] == b[k][l]) {
Console.WriteLine("a[" + (i + k) + "][" + (j + l) + "] = b[" + k + "][" + l + "]");
} else {
submatrix = false; // we found inequality, so it's not a submatrix
}
}
}
if (submatrix) {
Console.WriteLine("Found subatrix at " + i + "," + j + ".");
}
}
}
Please suggest??
Your suggested method is correct, there are only a few syntax and control flow issues which I've fixed.
It is important to point out that this method is only useful for detecting a submatrix of a 2D matrix, not any dimension matrix.
I assumed the datatype is a jagged array of int, though it can easily be changed.
private static bool IsSubMatrix(int[][] a, int[][] b)
{
for (int i = 0; i < a.Length - b.Length + 1; i++)
{
for (int j = 0; j < a[0].Length - b[0].Length + 1; j++)
{
bool found = true;
for (int k = 0; k < b.Length; ++k)
{
for (int l = 0; l < b[0].Length; ++l)
{
if (a[i + k][j + l] != b[k][l])
{
found = false;
break;
}
}
if (!found) break;
}
if (found) return true;
}
}
return false;
}
This is probably also not the fastest implementation.

print output for each value of i,j

in the code below that belongs to floyd algorithm how can i print the output for each value of i,j?for example shortest distance of [0,0]=2 and shortest [0,1]=4 etc
now it prints shortest distance from i to j is:3(for example)
private void button10_Click(object sender, EventArgs e)
{
string ab = textBox11.Text;
int matrixDimention = Convert.ToInt32(ab);
int[,] intValues = new int[matrixDimention, matrixDimention];
string[] splitValues = textBox9.Text.Split(',');
for (int i = 0; i < splitValues.Length; i++)
intValues[i / (matrixDimention), i % (matrixDimention)] = Convert.ToInt32(splitValues[i]);
string displayString = "";
for (int inner = 0; inner < intValues.GetLength(0); inner++)
{
for (int outer = 0; outer < intValues.GetLength(0); outer++)
displayString += String.Format("{0}\t", intValues[inner, outer]);
displayString += Environment.NewLine;
}
int n=matrixDimension
MessageBox.Show("matrix"+strn+ "in" + strn + "is\n\n\n" +displayString);
////before this line i wrote the codes to get the numbers that user enter in textbox and put it in an 2d array
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
if (intValues[i, j] > intValues[i, k] + intValues[k, j])
{
intValues[i, j] = intValues[i, k] + intValues[k, j];
string str_intvalues = intValues[i, j].ToString();
MessageBox.Show("Shortest Path from i to j is: " + str_intvalues);
}
else
{
string str_intvalues = intValues[i, j].ToString();
MessageBox.Show("Shortest Path from i to j is: " + str_intvalues);
}
}
}
i edited both of the MessageBox.show() to this form and it corrected:
MessageBox.Show("Shortest distance from" + i + " to " + j + " isnnt: " + str_intvalues);

Categories