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.
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.
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;
}
What I want to do is compare two of the same variable in a structure.
For example I have a structure like so:
struct player
{
public string name;
public int number;
}
static player[] players = new player[3];
and what I want to do is compare the numbers, so that if two players have the same number, something will happen.
This is what I tried, however it would always say two numbers were the same because it would compare two of the same
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
if (players[i].number == players[j].number)
{
Console.WriteLine("Same");
Console.ReadLine();
}
else
{
Console.WriteLine("Not");
Console.ReadLine();
}
}
Hopefully you understand what I mean.
Any help would be really appreciated!
Thanks
Problem is in your loop variables i and j starting both at index zero. Then you are comparing element zero to element zero and therefore the condition is true.
Update this line:
for (int j = 0; j < length; j++)
to this:
for (int j = i + 1; j < length; j++)
Edit
To be more precise. The condition evaluates to true not only for the first element, but for each element when i and j are the same. This solution bars both control variables from having the same value in any iteration.
Simple, just add a check to make sure you aren't comparing the same index, because this is the same object:
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
if (i == j) continue;
if (players[i].number == players[j].number)
{
Console.WriteLine("Same");
Console.ReadLine();
}
else
{
Console.WriteLine("Not");
Console.ReadLine();
}
}
Use a Class, and do it using Linq:
public class Player
{
public string Name { get; set; }
public int Number { get; set; }
}
Then in the other class have this method to cross-check
private void Match()
{
var players = new Player[3].ToList();
foreach (var found in players.ToList().Select(player => players.FirstOrDefault(p => p.Number == player.Number)))
{
if (found != null)
{
Console.WriteLine("Same");
Console.ReadLine();
}
else
{
Console.WriteLine("Not");
Console.ReadLine();
}
}
}
I am reading a csv file which has column names in first line and values in line >1. I need to get the position of the column name. The only way I can think of is to do either switch or ifs. I read it somewhere that in my case , it is faster (better) to do the ifs. However the file has many columns (~120). Just wondering if there is an alternative(s).
private static void Get_Position(string line, performance p)
{
string[] line_split = line.Split(',');
for (int i = 0; i < line_split.Length; i++)
{
if (line_split[i].Contains(#"(0)\% Processor Time"))
{
p.percore[0] = i;
}
else if (line_split[i].Contains(#"(1)\% Processor Time"))
{
p.percore[1] = i;
}
else if (line_split[i].Contains("Private Bytes"))
{}
else if (line_split[i].contains("DPC")
{
}
//on and on and on with else ifs
What is preventing you from using a loop?
for (int i = 0; i < line_split.Length; i++)
{
for(var j = 0; j < 120; j++)
{
if(line_split[i].Contains(#"(" + j + ")\% Processor Time"))
{
p.percore[j] = i;
}
}
...
To maintain the same functionality as if else if then you could use a break inside the conditional.
Edit: The edit now made it clear that there is no clear pattern to the string in contains. Still, if you are writing out 120 if/else if statements you should store what you will be looking for in some type of collection. For example, a List would work. Then access the index j of the collection in your loop:
...
var listOfSearchItems = new List<string>() { "Private Bytes", "DPC" };
for (int i = 0; i < line_split.Length; i++)
{
for(var j = 0; j < 120; j++)
{
if(line_split[i].Contains(listOfSearchItems[j])
{
p.percore[j] = i;
}
}
...
loop one
{
looptwo
{
if(condition=true)
{
reset values//restart both loops
}
}
}
and possibilities for reset values is 3
basically i want to compair two matrices
a= 1 2 3 4
1 2 3 4
b= 3 4 5 6
4 6 7 8
and when row 1 of a[] is matched with row 1 of b[].....i will add these rows and a[]
become = 2 4 6 8
for(i=0;i<rows;i++)
for(j=0;j<columns;j++)
{
a[i]=a[i]+b[i,j]
}
and again find my maches from restart with new a[] Matrix
and i have to insure that all rows of b[] matrix are checked with a[] which are 3 in this case
You have to use goto to break out of multiple loop levels in C#. For example:
RESTART:
while (a) {
while (b) {
if (that_other_thing)
goto RESTART;
}
}
Well, you don't have to use goto but the alternative might be using a bunch of flag variables to indicate that a restart is required. And that code will probably be pretty hard to follow.
The best choice here is to move the loops into their own method, and return from inside the inner loop. Example:
public void MyMehod(){
loop one{
looptwo{
if(condition=true){
return;
}
}
}
}
If this is not possible for some reason, you can use a bool value that you set in the inner loop to bail out of all of them, but this is a bit more messy:
bool endloop = false;
while(!endloop){
while(!endloop){
if(condition){
endloop = true;
}
}
}
For a while loop it looks ok, but even more messy for a for loop or a foreach loop.
Start:
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if(j == 5)
goto Start;
}
}
Although structuring your code in a way to not use a goto is a much better approach...
If you can guarantee that you will have a condition that will tell you that you don't need to restart, you could wrap the whole thing in one more loop.
bool keepLooping = true;
while (keepLooping)
{
keepLooping = false;
for (int x = 0; x < maxx; x++)
{
for (int y = 0; y < maxy; y++)
{
if (DoSomething(x, y))
{
keepLooping = true;
break;
}
}
if (keepLooping)
{
break;
}
}
}
If you are checking a list for duplicates and modifying them do make all entries unique, you might do something like this (assuming string values):
List<string> a = GetNamesFromeSomewhere();
bool duplicateFound = true;
while (duplicateFound )
{
duplicateFound = false;
for (int x = 0; x < a.Length; x++)
{
for (int y = x + 1; y < a.Length; y++)
{
if (a[x].Equals(a[y]))
{
//Change a[y], but now we have to recheck for duplicates...
a[y] += "_";
duplicateFound = true;
break;
}
}
if (duplicateFound)
{
break;
}
}
}
if you use numeric loop variables like i and j you can just reset the values
e.g.
for (i=0; i<10; i++) {
for (j=0; j<10; j++) {
if (var[i][j] == 'x') {
i=0; j=0; break;
}
}
}
you can also use the method approach as suggested earlier
void someFunction(params) {
for (i=0; i<10; i++) {
for (j=0; j<10; j++) {
if (var[i][j] == 'x') {
someFunction(params)
return;
}
}
}
}