How to create a loop for the following case? - c#

/html/body/table/tr[1]/td[2]
/html/body/table/tr[1]/td[4]
/html/body/table/tr[3]/td[2]
/html/body/table/tr[3]/td[4]
/html/body/table/tr[5]/td[2]
/html/body/table/tr[5]/td[4]
So, the index of tr[ ] would be odd numbers, and td[ ] would always be either 2 or 4.

for(int i = 1; i < bound; i += 2) {
for(int j = 2; j <= 4; j += 2) {
Console.WriteLine(
String.Format("/html/body/table/tr[{0}]/td[{1}]", i, j)
);
}
Console.WriteLine();
}

You could do something as simple as
for(tr = 1; tr < maxodd+1; tr += 2;)
{
//pseudoimplementation
/html/bod/table/tr[tr]/td[2]
/html/bod/table/tr[tr]/td[4]
}

The most naive case:
for(int i = 1; i < 6; i += 2) {
Console.WriteLine("html/body/table/tr[" + i + "]/td[2]");
Console.WriteLine("html/body/table/tr[" + i + "]/td[4]");
}

Related

Can't perambulate indexes in matrix the right way

Hello. I have this task to sum the numbers as shown. Tried everything I can, but still not the right answer. Can I have some guidance?
static void Main(string[] args)
{
string input = Console.ReadLine();
int n = (int)char.GetNumericValue(input[0]);
int m = (int)char.GetNumericValue(input[2]);
int[,] matrix = new int[n, m];
int sum = 0;
//fill matrix
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
matrix[i, j] = (j * 3 + 1) + i * 3;
}
}
for (int i = 0; i < matrix.GetLength(0) - 1; i+=1)
{
for (int j = 0; j < matrix.GetLength(1) - i; j+=1)
{
if (i % 2 == 0)
{
sum += matrix[i, j + i] + matrix[i + 1, j + 1];
}
}
}
Console.WriteLine(sum);
}
I think you would've a easier time hard coding the input (and naming them as "columns" and "rows" instead, much more readable).
What is the expected output? Not sure I'm following this sum. I'm guessing, 297? If so:
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i, j] + " ");
if(j == 5) Console.WriteLine();
if (matrix[i, j] % 2 != 0)
{
if (i == 0 || i == matrix.GetLength(0) - 1
|| j == 0 || j == matrix.GetLength(0))
{
sum += (matrix[i, j]);
}
else
{
sum += (matrix[i, j] * 2);
}
}
}
}

C#, Windows application, draw a square with side N

for school practice I have to make a windows application in C# that takes a input number and draws a square of X's with side N. I have to do it with a loops and i can't use any preset commands. (For example math.pow i cannot use) (I've included a picture of the assignment.) I've already mode this program in a console application and there it worked fine.
I think that i'm very close of solving it but can't figure out what the last step is. I would love to know what i'm missing and how i should solve this.
See the assignment
This is my code now:
int n;
n = int.Parse(txt_input.Text);
//upper part
for (int i = 0; i < n; i++)
{
lbl_output.Text = "X";
lbl_output.Text = "\n";
}
//middel part
for (int i = 0; i < n - 2; i++)
{
lbl_output.Text = "X";
for (int j = 0; j < n - 2; j++) lbl_output.Text = " ";
lbl_output.Text = "X";
lbl_output.Text = "\n";
}
//upper part
for (int i = 0; i < n; i++)
{
lbl_output.Text = "X";
lbl_output.Text = "\n";
}
Try this:
int n = int.Parse(txt_input.Text);
var sb = new StringBuilder();
for (int j = 0; j < n; j++)
{
sb.Append('X');
}
sb.AppendLine();
for (int i = 0; i < n - 2; i++)
{
sb.Append('X');
for (int j = 0; j < n - 2; j++)
{
sb.Append(' ');
}
sb.Append('X');
sb.AppendLine();
}
for (int j = 0; j < n; j++)
{
sb.Append('X');
}
lbl_output.Text = sb.ToString();
Try this;
int n = int.Parse(txt_input.Text);
//upper part
for (int i = 0; i < n; i++)
{
lbl_output.Text += "X";
}
lbl_output.Text += "\n";
//middel part
for (int i = 0; i < n - 2; i++)
{
lbl_output.Text += "X";
for (int j = 0; j < n - 2; j++)
lbl_output.Text += " ";
lbl_output.Text += "X";
lbl_output.Text += "\n";
}
//upper part
for (int i = 0; i < n; i++)
{
lbl_output.Text += "X";
}
You forget to use += which will append the text with the previous assigned text. Also you had some unnecessary new lines in your code.
Need to append to the string. There's a couple ways of doing this. Tho '+=' should work fine. += is short for variable = variable + newValue
int n = int.Parse(txt_input.Text);
string output = "";
for(int x = 0; x < n; x++) //rows
{
if (x == 0 || x == n-1) //first / last row all x
for(int y = 0; y < n; y++)
{
output += "x";
}
else //other rows
for(int y = 0; y < n; y++)
{
output += (y == 0 || y == n - 1) ? "x" : " "; //if first or last column "X" else " "
}
output += "\n"; //at the end of each row a return
}
lbl_output.Text = output;
You can see it run in the browser here: https://dotnetfiddle.net/wiYfjX
Use two loops. One for the width of the square and one for the height of the square.
Give this a try and replace the parameter from what is in the txt_input control. (Just place the function whereever you want in your code (button_click for example) instead of the form load.
private void Form1_Load(object sender, EventArgs e)
{
lblOutput.Text = GenerateSquare(5);
}
private string GenerateSquare(int n)
{
string square = "";
for (int w = 0; w < n; w++)
{
for (int h = 0; h < n; h++)
{
// top or bottom line
if (w == 0 || w == n - 1)
{
square += "x";
}
else // sides
{
if (h == 0 || h == n - 1)
{
square += "x";
}
else square += " ";
}
// change line
if (h == n - 1)
square += "\n";
}
}
return square;
}

Why do I get the error 'unreachable code detected'?

using System;
namespace G09 {
class Reverse {
static void Main()
{
Console.WriteLine(ReverseText(24));
Console.ReadKey();
}
static string ReverseText(int n) {
if (n < 1)
{
return "";
}
string index = "1";
string revIndex = "";
int count = 1;
string[] arr = new string[n];
for (int i = 0; i < n; i++)
{
arr[i] = index + revIndex;
for (int j = 0; j <= i; j++)
{
if (count > 8)
{
index = index + 0;
count = 0;
break;
}
index = index + (count++ + 1).ToString();
break;
}
revIndex = "";
for (int k = index.Length - 1; k >= 0; k--)
{
if (k == index.Length - 1)
{
continue;
}
revIndex += index[k];
}
}
return string.Join("\n", arr);
}
}
}
/tmp/csharp117013-18-5s54om.vh6mt2o6r/code.cs(10,41): warning CS0162:
Unreachable code detected
Can anyone tell me why this code is unreachable on line 23??
I can not understand... In visual studio it works fine, but in other shows error.
The j++ is pointless because of the break at the end, remove one of both
for (int j = 0; j <= i; j++) {
...
break;
}
You will never increment j because you break the iteration after the first loop
for (int j = 0; j <= i; j++) //<- Error here, j++ will never be reached
{
if (count > 8)
{
index = index + 0;
count = 0;
break;
}
index = index + (count++ + 1).ToString();
break; //you leave the for loop here
}
Your issue is with this for loop:
for (int j = 0; j <= i; j++) {
if (count > 8) {
index = index + 0;
count = 0;
break;
}
index = index + (count++ + 1).ToString();
break;
}
I'm guessing the for loop starts on line 23. Because you have all paths inside the for loop containing break statements the j++ part of the for loop will never be hit. The loop will execute once and break out before ever hitting the increment for variable j.

Chess board positions

How can i make this so the user types positions in console (table[4,5]) i want that user types that?
int[,] table = new int[8, 8];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if ((i + j) % 2 == 0)
{
table[i, j] = 0;
}
else
{
table[i, j] = 1;
}
}
}
Console.WriteLine("4 - king");
Console.WriteLine("3 - queen");
Console.WriteLine("4 - hunter");
table[4,5] = 2;
table[6,7] = 3;
table[2,2] = 4;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Console.Write(table[i, j] + " ");
}
Console.WriteLine();
}
What should i do to make this work?And if i type this it doesnt work:
Thats why i have to put figures in different rows or columns how do i fix this
table[4,5] = 2;
table[4,7] = 3;
table[2,2] = 4;
You are confusing the assignment operator "=" with the logical comparison operator "==". Your second line is just comparing table[4,5] with 2 and probably returning false.
Change it to:
table[4,5] = 2;
Also, even if you manage to assign a value to table[4,5], you will overwrite it in the next lines. You should move that line to the end of the first nested loop. Just before the second "for (int i = 0; i < 8; i++)"

Generate Alpha Numeric Sequence in C#

I want to generate a string sequence in c#.And this sequence starts with 1 and ends on ZZ. for e.g.:-
1-99
A0-A9
AA-AZ
B0-B9
BA-BZ
.....
....
Z0-Z9
ZA-ZZ
So this is a sequence that i wants to generate and the the length not more then 2 characters means the end of sequence is ZZ.
So Please help me out and if possible can we also do it in oracle.
Thanks in Advance.
Regards
Anil
You want a string like "123456789101112...99A0A1...ZZ", right?
This should fit:
string sequenceStr = "";
for (int i = 1; i < 100; i++)
sequenceStr += i.ToString();
for (int i = 0; i < 26; i++)
{
for (int j = 0; j < 36; j++)
{
sequenceStr += Encoding.UTF8.GetString(new byte[] { (byte)(i + 65) }); // A=65, B=66, ...
if (j < 10)
{
sequenceStr += j.ToString();
}
if (j > 9)
{
sequenceStr += Encoding.UTF8.GetString(new byte[] { (byte)(j + 55) });
}
}
}

Categories

Resources