I have this adjacency matrix :
And I don't know if I can check if any of the nodes are not connected with the others. I mean, if it's alone, a row and column of zeros (for example, the first one, A,) should return false because simple connectivity does not exist.
public bool HayConectividadsimple()
{
bool respuesta = true;
for (int i = 0; i < Aristas.GetLength(0); i++)
{
for (int j = 0; j < Aristas.GetLength(1); j++)
{
if (i, j == 0)
return false;
}
}
return respuesta;
}
Hope you can help me.
Best regards,
From my understanding you're looking for a whole row of 0 and a whole column of 0. If you spot either then return false. Roughly something like this:
For each node..
check if it has an all 0 column
Check if it has an all 0 row
If both are true, return false
Return true otherwise.
So, that looks like this:
public bool HayConectividadsimple()
{
// For each node..
for (int i = 0; i < Aristas.GetLength(0); i++)
{
// Assume it's not connected unless shown otherwise.
bool nodeIsConnected=false;
// Check the column and row at the same time:
for (int j = 0; j < Aristas.GetLength(1); j++)
{
if (Aristas[i, j] != 0 || Aristas[j, i] != 0)
{
// It was non-zero; must have at least one connection.
nodeIsConnected=true;
break;
}
}
// Is the current node connected?
if(!nodeIsConnected)
{
return false;
}
}
// All ok otherwise:
return true;
}
Related
If I have a loop in a function, how do I return a value from within the loop?
In this example I'm using a loop to figure out if a number is prime. If I figure out the answer then I don't want the loop to continue. I just want to return it.
private int IsPrime (int startNumb , int endNumb)
{
bool bilPrima = true;
for (int i = startNumb; i<= endNumb; i++)
{
for (int j = 2; j <= i; j++)
{
if (i%j==0)
{
bilPrima = false;
break;
}
}
if (bilPrima)
{
bilPrima = true;
return i;
}
else
{
return 0;
}
}
}
If a function contains a loop, but the return value of the function can be determined before the loop has completed, you can just return the return value, and execution of the function will end.
So your function could look like this:
private bool IsPrime(int startNumb, int endNumb)
{
for (int i = startNumb; i <= endNumb; i++)
{
for (int j = 2; j <= i; j++)
{
if (i % j == 0)
{
return false;
}
}
}
return true;
}
One of the comments suggested that you might want to return a list of prime numbers. If that's the case then the answer would be completely different.
You could return an IEnumerable<int>. Within your function, create a List<int>. As you loop through values, every time you find a prime number add it to the list. Then at the end of the function, return the list.
You can do the same thing with yield return - I'm just trying not to overload you.
How to get Cell index after comparing values, for example i have this
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[value].Value.ToString() == radTextBox1.Text)
{
//If the value matches how to get the row index of that
}
}
This might do the job:
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == radTextBox1.Text)
{
dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];
}
}
If you are wanting to find the value in any cell then you need to use a nested loop.
dataGridView1.ClearSelection();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int c = 0; c < dataGridView1.Columns.Count; c++)
{
if (dataGridView1.Rows[i].Cells[c].Value.ToString() == radtextBox1.Text)
{
dataGridView1.Rows[i].Selected = true;
}
}
}
This will move through each row while checking all the columns for a value and will hilight any row where a cell has that value. Note that this code is case sensitive so "City" <> "city" but that is easily fixed using .ToUpper or .ToLower as needed.
One other thing to add, this code also is based off a DGV that has AllowUserToAddRows set to false. If you need the edit row, you either need to -1 from the count in the rows loops or check to ensure that the current row is false for .IsNewRow.
You found the Row you're looking for.
It's i variable in your code.
var requiredRowIndex = -1;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[value].Value.ToString() == radTextBox1.Text)
{
requiredRowIndex = i;
break;
}
}
if (requiredRowIndex != -1)
{
// It was found.
}
else
{
// It was not found.
}
You dont show us what is the value? It's actualy index of Cell you're looking for.
Excuse me for the silly question, but i can't manage to solve it.
How can i make something to happen every third time ?
Times(left number):
shipmentId=0
shipmentId=0
shipmentId=1
shipmentId=1
shipmentId=2
shipmentId=2 ....
int occurrence = 0;
int counter = 0;
foreach (var el in elmOrderData)
{
if (el.Name == "shipmentIndex")
{// we are entering here for every element that his name is "shipmentIndex"
el.SetValue(shipmentId);
secondTime++;
}
if ((secondTime % 2) == 0)
{// every third time we see "shipmentIndex"
secondTime = 1;
shipmentId++;
}
}
You could use a bool as in the following example. This will display messageboxes 1,3,5,7 and 9:
bool test = false;
for (int i = 0; i < 10; i++)
{
if (test)
MessageBox.Show(i.ToString());
test = !test;
}
Trying to piece together your notes - whether 'it' happens every time or not. How about this?
int occurrence = 0;
int counter = 0;
foreach(var a in list)
{
if(some_condition)
{
// do something..
occurrence++
if(occurrence % 2 == 0)
{
counter++
}
}
}
Why not just increment everytime and just divide by 2?
for(var i = 0; i<20; i++)
{
var j = i / 2; // or bit shift
//do work on j instead of i
}
i am writing a game and i have a simple enemy AI that only follows you, i need the enemys to not go on top of each other so i tried this code but it doesn't work they stil going on top of each other (except of maybe 2 who don't and i have no idea why).
here is the code that check's if they intersects
for (int i = 0; i < z.Length ; i++)
{
for (int j = 0; j < z.Length ; j++)
{
if (zombie[i].Bounds.IntersectsWith(zombie[j].Bounds) && i != j)
{
z[i].setAllowed(false);
}
else
{
z[i].setAllowed(true);
}
}
}
The code is inside a timer and setAllowed tells the zombie class weather the zombie can move or not.
Your "allowed" values are being overriden during the loop. For instance:
(1) i == 2, j == 1 => let's say that SetAllowed(false) is executed (collision, expected behavior)
(2) (next iteration) i == 2, j == 2 => i == j so SetAllowed(true) is executed (overriding correct behavior)
(3) Even when i != j, you can override the "allowed" value when a next zombie does not collide.
Try something like that:
for (int i = 0; i < z.Length ; i++)
{
bool allowed = true;
for (int j = 0; j < z.Length ; j++)
{
if (i == j)
continue;
if (zombie[i].Bounds.IntersectsWith(zombie[j].Bounds))
{
allowed = false;
break;
}
}
z[i].setAllowed(allowed);
}
Of course there can be other problems somewhere else in your logic, but this is a start from the code you provide.
i am using visual studio c# win form. . . i have 2d array of textboxes and i have another 2d array of valid solved sudoku i want to compare textbox's text to sudoku array but its not working.Here is my code:
private void Isvalid()
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (copy[i, j] == textbox[i, j].Text)
isvalid = true;
}
private void check()
{
Isvalid();
if (isvalid)
MessageBox.Show("NO");
else
MessageBox.Show("YES");
}
Can anyone plz help me. . .
THANx in Advance. . .
Thanx to all who answerd. . .
You do not (re)set the isvalid variable to false, if the arrays are not equal.
You pass the result of the Isvalid method through a shared variable. Your code will be much clearer if you pass the result of the comparison as the method result.
private bool Isvalid()
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (copy[i, j] != textbox[i, j].Text) {
return false; // If one is not equal, the two arrays differ
}
}
}
return true;
}
Now you can test for:
if (Isvalid()) {
// your code here
}
Even better will be if you pass the two arrays as arguments.
I would put a breakpoint on the
if (copy[i, j] == textbox[i, j].Text)
line and visually see if what you see in the Text box is what the array contains.
It might be as simple as needing to do a case-insensitive comparison.
textbox[i, j].Text
here you need to convert the values in the textbox matrix to integer before comparing. That will solve the problem.
Also you need to set invalid as false and after making it true break out of loop
Try this:
private void Isvalid()
{
isvalid = true;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (copy[i, j] != textbox[i, j].Text)
{
isvalid = false;
return;
}
}
In your routine, you'd set isvalid to true whenever one number matches. You'd rather need to set it to false if a number doesn't match.