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>";
Related
at the beginning I want to mention that I am a beginner in programming. So, I want to write a program that checks the similarity of two-dimensional arrays of integers. The similarity is to be determined by the amount of numbers that are in the same positions in both tables. The user gives the number of columns in the table and the elements themselves, number of rows is the same all the time.The similarity result is displayed as a percentage and the similarity itself should be calculated taking into account the number of elements of the larger array. My problem is: When the two arrays are the same size, the program throws the exception and it doesn't check all the numbers in the column.(I wrote before program for one dimensional array and it works perfectly) So far I have managed to write something like this:
This is what I want to do In the picture, the similarity between the arrays is 20%
{
Console.WriteLine("How extensive is the first table supposed to be?");
int n = int.Parse(Console.ReadLine());
int z = 2;
int[,] tab1 = new int[2, n];
Console.WriteLine("Enter the numbers into the first array:");
for (int i = 0; i < z; i++)
{
for (int j = 0; j < n; j++)
{
tab1[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\n");
int rowLength = tab1.GetLength(0);
int colLength = tab1.GetLength(1);
for (int i = 0; i < rowLength; i++)
{
for (int j = 0; j < colLength; j++)
{
Console.Write(string.Format("{0} ", tab1[i, j]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
Console.WriteLine("How extensive is the second table supposed to be?");
int m = int.Parse(Console.ReadLine());
int b = 2;
int[,] tab2 = new int[2, m];
Console.WriteLine("Enter the numbers into the second array: ");
for (int i = 0; i < b; i++)
{
for (int j = 0; j < m; j++)
{
tab2[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\n");
int Len4gth = tab2.GetLength(0);
int Len2gth = tab2.GetLength(1);
for (int i = 0; i < Len4gth; i++)
{
for (int j = 0; j < Len2gth; j++)
{
Console.Write(string.Format("{0} ", tab2[i, j]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
double similarity= 0;
if (tab1.GetLength(1) > tab2.GetLength(1))
{
for (int i = 0; i < tab2.GetLength(1); i++)
{
for (int j = 0; j < z; j++)
{
if (tab1[i, j] == tab2[i, j])
{
similarity+= 1;
}
}
}
}
if (tab1.GetLength(1) < tab2.GetLength(1))
{
for (int i = 0; i < tab1.GetLength(1); i++)
{
for (int j = 0; j < z; j++)
{
if (tab2[i, j] == tab1[i, j])
{
similarity+= 1;
}
}
}
}
if (tab1.GetLength(1) == tab2.GetLength(1))
{
for (int i = 0; i < tab1.GetLength(1); i++)
{
for (int j = 0; j < z; j++)
{
if (tab1[i, j] == tab2[i, j])
{
similarity+= 1;
}
}
}
}
if (tab1.Length < tab2.Length)
{
Console.WriteLine("The similarity of the arrays is: " + (similarity/ tab2.Length) * 100 + "%");
}
if (tab1.Length > tab2.Length)
{
Console.WriteLine("The similarity of the arrays is: " + (similarity/ tab1.Length) * 100 + "%");
}
if (tab1.Length == tab2.Length)
{
Console.WriteLine("The similarity of the arrays is: " + (similarity/ tab2.Length) * 100 + "%");
}
Console.ReadKey();
You must compare each element of the first array with the elements of the second array.
use this code :
//get first array items
Console.WriteLine("How extensive is the first table supposed to be?");
int n = int.Parse(Console.ReadLine());
int[,] tab1 = new int[2, n];
Console.WriteLine("Enter the numbers into the first array:");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < n; j++)
{
tab1[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\n");
//write first array items
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(string.Format("{0} ", tab1[i, j]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
//get second array items
Console.WriteLine("How extensive is the second table supposed to be?");
int m = int.Parse(Console.ReadLine());
int[,] tab2 = new int[2, m];
Console.WriteLine("Enter the numbers into the second array: ");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < m; j++)
{
tab2[i, j] = int.Parse(Console.ReadLine());
}
}
//write second array items
Console.WriteLine("\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < m; j++)
{
Console.Write(string.Format("{0} ", tab2[i, j]));
}
Console.Write(Environment.NewLine + Environment.NewLine);
}
//find similarity items
double similarity = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < n; j++)
{
int firstValue = tab1[i, j];
for (int k = 0; k < 2; k++)
{
for (int d = 0; d < m; d++)
{
if (firstValue == tab2[k, d])
{
similarity += 1;
}
}
}
}
}
double percentage = n > m ? ((similarity / tab1.Length) * 100) : ((similarity / tab2.Length) * 100);
Console.WriteLine("The similarity of the arrays is: " + percentage + "%");
Console.ReadKey();
this code work without error and It does not matter which array is larger.
If you want similar elements like this example enter link description here, use this code snippet to find similar elements
//find similarity items
double similarity = 0;
int z = n > m ? m : n;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < z; j++)
{
if (tab1[i, j] == tab2[i, j])
{
similarity += 1;
}
}
}
double percentage = n > m ? ((similarity / tab1.Length) * 100) : ((similarity / tab2.Length) * 100);
Console.WriteLine("The similarity of the arrays is: " + percentage + "%");
Console.ReadKey();
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);
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];
}
i am working oncomplex valued neural networks, rvnn ok but when convert it to cvnn error is not decreases, and i cant figure out where is mistake, my code is here
public bool Train(List<Complex> input, List<Complex> output)
{
if ((input.Count != this.Layers[0].Neurons.Count) || (output.Count != this.Layers[this.Layers.Count - 1].Neurons.Count)) return false;
Run(input);
error = 0;
for(int i = 0; i < this.Layers[this.Layers.Count - 1].Neurons.Count; i++)
{
Neuron neuron = this.Layers[this.Layers.Count - 1].Neurons[i];
neuron.Delta = dSigmoid(neuron.Value) * (output[i] - neuron.Value);
error += (output[i] - neuron.Value).Magnitude;
for(int j = this.Layers.Count - 2; j > 2; j--)
{
for(int k = 0; k < this.Layers[j].Neurons.Count; k++)
{
Neuron n = this.Layers[j].Neurons[k];
n.Delta = dSigmoid(n.Value)*
this.Layers[j + 1].Neurons[i].Dendrites[k].Weight *
this.Layers[j + 1].Neurons[i].Delta;
}
}
}
for(int i = this.Layers.Count - 1; i > 1; i--)
{
for(int j=0; j < this.Layers[i].Neurons.Count; j++)
{
Neuron n = this.Layers[i].Neurons[j];
n.Bias = n.Bias + (this.LearningRate * n.Delta);
for (int k = 0; k < n.Dendrites.Count; k++)
n.Dendrites[k].Weight = n.Dendrites[k].Weight + (this.LearningRate * this.Layers[i - 1].Neurons[k].Value * n.Delta);
}
}
return true;
}
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.