print output for each value of i,j - c#

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);

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);

My code won't do the sum; only the elements of the matrix appear and then nothing happens

namespace Suma_diagonala_secundara
{
class Program
{
static void Main(string[] args)
{
int n, i, j, s = 0;
Console.Write("n= ");
n = Convert.ToInt32(Console.ReadLine());
int[,] tab = new int[n, n];
for(i=0;i<n;i++)
for (j = 0; j < n; j++)
{
Console.Write("tab[{0}][{1}]= ", i + 1, j + 1);
tab[i, j] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\nElementele matricii sunt: ");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
Console.Write("{0} ", tab[i, j]);
Console.WriteLine("");
}
Console.WriteLine("Suma elementelor de pe diagonala secundara este: ");
for (i = 0; i < n; i++)
{
s = s + tab[i, n - i + 1];
}
Console.ReadKey();
}
}
}
In your loop
for (i = 0; i < n; i++)
{
s = s + tab[i, n - i + 1];
}
you are accessing the array out of bounds, since n - 0 + 1 = n + 1 is larger than n - 1 (the largest index in tab).
What you actually want is (note the parentheses)
for (i = 0; i < n; i++)
{
s = s + tab[i, n - (i + 1)];
}
The following line:
s = s + tab[i, n - i + 1];
Is throwing an IndexOutOfRangeException because you are requesting an index higher than the array capacity.
The correct loop code is (with decrement instead increment)
for (i = 0; i < n; i++)
{
s = s + tab[i, n - i - 1];
}

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>";

Multidimensional array adding controls

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

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.

Categories