So I've got an array of integer. I want to use a loop and exclude integers that makes the equation true. So that would be like
for (int n = 0; n < first9char.Length; n++ ) {
if (first9char[n] == clickValue) {
first9char[n] = first9char[n + 1];
But then it only changes the value that is equal to not changing whole array. So is there any way to do this?
I want to use it in this loop.
if (UserSquareMethod.clickedBoxes[0] == -1) {
MessageBox.Show("You have not selected any box");
} else {
int i = 0;
do {
if (textButtonArray[clickedBox[i]].Text == "" || clickValue == "") {
textButtonArray[clickedBox[i]].Text = clickValue;
textButtonArray[clickedBox[i]].Font = new Font(textButtonArray[clickedBox[i]].Font.FontFamily, 14, FontStyle.Bold);
}
else
{
textButtonArray[clickedBox[i]].Text += "," + clickValue;
textButtonArray[clickedBox[i]].Font = new Font(textButtonArray[clickedBox[i]].Font.FontFamily, 5, FontStyle.Regular);
string[] first9char = textButtonArray[clickedBox[i]].Text.Split(new string[] { "," }, StringSplitOptions.None);
for (int j = 1; j < first9char.Length; j++)
{
for (int k = j - 1; k >= 0; k--)
{
if (first9char[j] == first9char[k])
{
if (clearNumberClicked == true)
{
first9char = Array.FindAll(first9char, x => x != clickValue);
label2.Text = first9char[0];
//int n = 0;
//for (int p = 0; p < first9char.Length; p++)
//{
// if (first9char[p] != clickValue)
// {
// first9char[n] = first9char[p];
// n++;
// label2.Text += "," + first9char[n];
// }
// }
//for (int n = 0; n < first9char.Length; n++ ) {
//if (first9char[n] == clickValue) {
// first9char[n] = first9char[n + 1];
// for ( int p = 0; p < n; p++) {
//}
//}
//}
MessageBox.Show("Clear the number" + first9char[(first9char.Length - 1)] + "and " + clickValue + " " + first9char.Length);
}
else {
first9char[j] = "";
textButtonArray[clickedBox[i]].Text = first9char[0];
MessageBox.Show("You cannot enter the same number again!"+ first9char[j]+j);
for (int m = 1; m < (first9char.Length - 1); m++) {
textButtonArray[clickedBox[i]].Text += ","+ first9char[m];
}
}
}
}
}
if (textButtonArray[clickedBox[i]].Text.Length > 9)
{
textButtonArray[clickedBox[i]].Text = first9char[0] + "," + first9char[1] + "," + first9char[2] + "," + first9char[3] + "," + first9char[4];
MessageBox.Show("You cannot enter more than 5 numbers, please clear the box if you want to enter different number." + textButtonArray[clickedBox[i]].Text.Length);
}
}
i++;
}
while (clickedBox[i] != -1);
}
}
I would use LINQ for this:
first9char = first9char.Where(x => x != clickValue).ToArray();
It just means "pick the items that don't match". If you can't use LINQ for some reason, then just keep another counter, and make sure to only loop to n from there on in:
int n = 0;
for(int i = 0; i < first9char.Length; i++) {
if(first9char[i] != clickValue) {
first9char[n] = first9char[i];
n++;
}
}
Clean and efficient.
Related
int i = 0;
string[] sum = new string[256];
for (i = 1; i < 256; i++)
{
sum[i] = sum[i].ToString();
sum[i] = Convert.ToInt32(dt.Compute("Sum(BIN" + i.ToString() + ")", string.Empty));
if(sum[i] == 0)
{
dt.Columns.Remove("BIN"+i.ToString()+"");
}
}
There is a problem where you're trying to assign an int to sum[i], because sum is a string[], and you can't assign an int to one of the items.
Instead, it seems you want to get the return value from the method and save it in a variable so you can compare it. In order to do this, we can modify the code like:
string[] sum = new string[256];
for (int i = 1; i < 256; i++)
{
int result = Convert.ToInt32(dt.Compute("Sum(BIN" + i.ToString() + ")", string.Empty));
if (result <= 0)
{
dt.Columns.Remove("BIN" + i.ToString() + "");
}
}
This can be simplified further by using the return value of the method directly in the if condition. We can also use interpolated strings instead of concatenation:
string[] sum = new string[256];
for (i = 1; i < sum.Length; i++)
{
if(Convert.ToInt32(dt.Compute($"Sum(BIN{i})", string.Empty)) <= 0)
{
dt.Columns.Remove($"BIN{i}");
}
}
Here's my code.
CustomClass.cs
class ScoreBoard(){
private int m_lastCnt;
public ScoreBoard{
m_lastCnt = 0;
}
public void makeBoard(string history) {
string[] arrPartStr = history.Split(',');
int[] arrPart = new int[arrPartStr.Length];
for (int c = 0; c < arrPart.Length; c++)
{
int temp = 0;
if (arrPartStr[c][0] == 'P') temp = 100;
else if (arrPartStr[c][0] == 'B') temp = 200;
else temp = 300;
if (arrPartStr[c][1] == 'P') temp += 10;
if (arrPartStr[c][2] == 'P') temp += 1;
arrPart[c] = temp;
}
//var strTmp : String = strData;
//strTmp = "311|101|211|211|211|211|211|211|211|211|111|111|111|111|111|111|111|111|111"
//strTmp = strData.replace(/:/g,"");
int[,] arrTmp = new int[6100, 104];
}
}
Main Class i call the void method like this
ScoreBoard sb = ScoreBoard();
string history = "s ,o ,m ,e ,s ,t ,r ,i ,n ,g";
private void Start(){
sb.makeBoard(history);
}
How can i print my 2D array in my console?
I tried doing it like the for(int row){for(int col){}} but its not working i don't know why
Do you mean this?
for (int j = 0; j < arrTmp.GetLength(1); j++)
{
for (int i = 0; i < arrTmp.GetLength(0); i++)
{
var msg = "[" + i.ToString() + ", " + j.ToString() + "] = " + arrTmp[i, j].ToString();
Debug.Log(msg);
}
}
I got it to display:)
CustomClass.cs
int[,] arrTmp = new int[104, 6];
public int[,] GetBoard(){ return arrTmp }
MainClass.cs
int[,] arrayBigRoad = bsb.GetBoard();
for (int j = 0; j < arrTmp.GetLength(1); j++)
{
for (int i = 0; i < arrTmp.GetLength(0); i++)
{
var msg = "[" + i.ToString() + ", " + j.ToString() + "] = " + arrTmp[i, j].ToString();
Debug.Log(msg);
}
}
Thanks though Rodrigo . I'll mark yours as an answer
I wonder how to do or write a code that it will print out no one got 20 points and it has to write a number of line. Everything works except if (is20 == false). How to fix it?
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
double[,] points = new double[50, 5];
Random r = new Random();
for (int k = 0; k < 50; k++)
{
for (int j = 0; j < 5; j++)
{
points[k, j] = r.NextDouble() * 5 + 15.5;
if (points[k, j] > 20) points[k, j] = 20;
Console.Write("{0:f1}", points[k, j]);
Console.Write(" ");
}
Console.WriteLine();
}
bestEstimated(points);
Console.ReadLine();
}
public static void bestEstimated(double[,] points)
{
bool is20 = false;
for (int line = 0; line < 50; line++)
{
for (int column = 0; column < 5; column++)
{
if (points[line, column] == 20)
{
Console.WriteLine("20 points got: " + line + " competitor");
is20 = true;
break;
}
}
}
if (is20 == false)
{
Console.WriteLine("No one got 20 points: ");
}
}
}
}
You can set is20=false in inner loop in else part and check it inside outer loop after inner loop.
public static void bestEstimated(double[,] points)
{
bool is20 = false;
for (int line = 0; line < 10; line++)
{
for (int column = 0; column < 5; column++)
{
if (points[line, column] == 20)
{
Console.WriteLine("20 points got: " + line + " competitor");
is20 = true;
break;
}
else
{
is20=false;
}
}
if (!is20)
{
Console.WriteLine("20 points not got: " + line + " competitor");
}
}
if(is20 == false)
{
Console.WriteLine("No one got 20 points: ");
}
}
Just a wild guess from your comments below your question:
public static void bestEstimated(double[,] points)
{
var not20Points = new List<int>();
for (int line = 0; line < 50; line++)
{
bool is20 = false;
for (int column = 0; column < 5; column++)
{
if (points[line, column] == 20)
{
Console.WriteLine("20 points got: " + line + " competitor");
is20 = true;
break;
}
}
if (is20 == false)
{
Console.WriteLine("competitor" + line + " didnt get 20 points"); //also can print it here if ya want...
not20Points.Add(line);
}
}
if (not20Points.Count == 50)
{
Console.WriteLine("No one got 20 points");
}
else
{
Console.WriteLine("Those lines did get 20 points: " + string.Join(",", Enumerable.Range(0, 50).Except(not20Points)));
Console.WriteLine("Those lines didnt get 20 points: " + string.Join(",", not20Points));
}
}
(Updated my version, to only print stuff if atleast one column has 20 points)
I am working on an ASP application which is about creating time table (scheduling of classes) for my final semester project. I have used the logic from a basic console application program of graph colouring and incorporated into my project. Though the logic for a console application works as required, I am unable to do the same for my asp project.
try
{
int n = 10, i, j, max = 0, col, ct = 0, rt = 0, m, count = 2, g = 0;
int[][] a = new int[20][];
int[] c = new int[20];
int[] arr = new int[20];
for (int k = 0; k < 20; k++)
{
c[k] = 0;
arr[k] = 0;
a[k] = new int[20];
for (int l = 0; l < 20; l++)
{
a[k][l] = 0;
}
}
string num = null, num1 = "";
string display = "SELECT * FROM tbl_Subjects WHERE USN IN(SELECT USN FROM tbl_Student WHERE Semester='" + DropDownList1.Text + "')";
con.Open();
SqlCommand cmdSql = new SqlCommand(display, con);
SqlDataReader rs;
rs = cmdSql.ExecuteReader();
while (rs.Read())
{
//count1++;
for (int k = 1; k <= n; k++)
{
g = k - 1;
num = rs[k].ToString();
arr[g] = Convert.ToInt32(num);
num1 += arr[g] + " ";
}
num1 += "\n";
for (int k = 0; k < n; k++)
{
for (int l = (k + 1); l < n; l++)
{
if (arr[k] == 1 && arr[l] == 1)
{
a[k][l] = 1;
//a[l][k] = 1;
}
}
}
}
rs.Close();
TextBox3.Text = num1;
num1 = "";
for (int k = 0; k < n; k++)
{
for (int l = 0; l < n; l++)
{
num1 += a[k][l] + " ";
}
num1 += "\n";
}
for (i = 0; i < n; i++)
{
c[i] = 0;
//for(j=0;j<n;j++)
//scanf("%d",&a[i][j]);
}
c[0] = 1; c[1] = 2;
for (i = 2; i < n; i++)
{
for (j = 0; j < n; j++)
if (a[i][j] > 0)
{
m = 0;
for (col = 0; col < n; col++)
{ if (a[i][col] > 0)rt++; if (a[col][i] > 0)ct++; }
m = rt; if (ct > rt) m = ct;
if (m < 2) { if (a[0][i] > 0)c[i] = 2; else c[i] = 1; } else { c[i] = count; if (m > max) { max = m; count++; } }
rt = 0; ct = 0;
} if (c[i] < 1) if (c[i - 1] > 1) c[i] = 1; else c[i] = 2;
}
string result = "";
for (i = 0; i < n; i++)
{
result += "Subject[" + (i + 1) + "] = " + c[i] + "\n";
if (c[i]==1)
{
TextBox4.Text += "Subject 1 = 8:15 to 9:15 \n";
}
if (c[i] == 2)
{
TextBox4.Text += "Subject 2 = 9:15 to 10:15 \n";
}
if (c[i] == 3)
{
TextBox4.Text += "Subject 3 = 10:45 to 11:45 \n";
}
if (c[i] == 4)
{
TextBox4.Text += "Subject 4 = 11:45 to 12:45 \n";
}
if (c[i] == 5)
{
TextBox4.Text += "Subject 5 = 1:30 to 2:30 \n";
}
if (c[i] == 6)
{
TextBox4.Text += "Subject 6 = 2:30 to 3:30 \n";
}
if (c[i] == 7)
{
TextBox4.Text += "Lab 1 = 8-15 to 10:15 \n";
}
if (c[i] == 8)
{
TextBox4.Text += "Lab 2 = 10:45 to 12:45 \n";
}
if (c[i] == 9)
{
TextBox4.Text += "Lab 3 = 1:30 to 3:30 \n";
}
if (c[i] == 10)
{
TextBox4.Text += "Lab 4 = 8-15 to 9:15 \n";
}
}
con.Close();
TextBox2.Text = result;
}
catch (Exception ex)
{
Label2.Text = ex.Message;
}
In the above code, I'm reading the data i.e 0's and 1's of the adjacency matrix from the database onto which the graph colouring algorithm is applied. Once the colouring has been performed, results are printed on a textbox for time being.
The problem I am facing is, the colors are not being assigned and not able to generate the time table.
I have a []string retry and I have some strings inside of it let's say:
a 2 , a 3 , h 9, asd 123 and so on. They all have intervals between the letter and the integer. Now I need to get how long is the letter part (in this case "qwe 2" the letter length is 3 and the integer part is 2). I'm later using .substring from the specified index at which the letter part finishes and the integer one starts.
string[] s = new string[5];
List<string> retry = new List<string>();
int max = 1;
for (int i = 0; i < s.Length; i++)
{
s[i] = Console.ReadLine();
}
Console.WriteLine(" ");
Array.Sort(s);
for (int j = 0; j < s.Length; j++)
{
if (j > 0)
{
if (s[j] == s[j - 1])
{
max++;
if (retry.Any(x => x.Contains(s[j])))
{
retry.Add(s[j] + " " + max);
}
else
{
if (j <= s.Length - 1)
{
if (s[j-1] != s[j])
{
retry.Add(s[j] + " " + max);
}
else
{
retry.Add(s[j] + " " + max);
}
}
}
}
else
{
max = 1;
}
}
}
for (int j = 0; j < retry.ToArray().Length; j++)
{
for (int k = j + 1; k < retry.ToArray().Length; k++)
{
var a1=retry[j].Substring(0,1);
var a2 = retry[k].Substring(0,1);
if(a1==a2)
{
var b1 = retry[j].Substring(2);
var b2 = retry[k].Substring(2);
if(int.Parse(b1)>int.Parse(b2))
{
retry.Remove(a2 + " "+ b2);
}
else
{
retry.Remove(a1 + " " + b1);
}
}
}
Console.WriteLine(retry[j]);
}
Console.ReadKey();
This only works for 1 letter.
The code below should get the result as expected:
string [] entries = {"xyz 1","q 2","poiuy 4"};
for(int i=0;i<entries.Length;i++)
{
var parts = entries[i].Split(' ');
var txtCount = parts[0].Length;
Console.WriteLine(String.Format("{0} {1}", txtCount, parts[1]));
}
How about...
string[] test = new [] { "qwe 2", "a 2", "b 3", "asd 123" };
foreach (var s in test)
{
var lastLetterIndex = Array.FindLastIndex(s.ToCharArray(), Char.IsLetter);
var lastNumberIndex = Array.FindLastIndex(s.ToCharArray(), Char.IsNumber);
Console.WriteLine(s);
Console.WriteLine("Letter Length : " + (lastLetterIndex + 1));
Console.WriteLine("Number Length : " + (lastNumberIndex - lastLetterIndex));
}
Console.ReadKey();
This iterates through all of the strings and counts the length of the chars, and stores the value of the number, as well as its index. Note that this information is lost upon each iteration, but I'll leave it to you to worry about storing that if you need that information for longer than the duration of an iteration.
int letterLength = 0, number = 0, index = 0;
string[] testString = { "a 2", "a 3", "h 9", "asd 123", "sw", "swa23", "swag 2464" };
foreach (string str in testString)
{
letterLength = 0;
index = -1;
int i = 0;
for (; i < str.Length; i++)
{
char c = str[i];
if (Char.IsLetter(c)) letterLength++;
else if (Char.IsNumber(c)) break;
}
StringBuilder strBuilder = new StringBuilder();
for (; i < str.Length; i++)
{
char c = str[i];
if (index == -1) index = str.IndexOf(c);
strBuilder.Append(c);
number = Int32.Parse(strBuilder.ToString());
}
Console.WriteLine($"String: {str}\nLetter Length: {letterLength} Number: {number} Index: {index}\n");
}