Click here to Skip to main content
15,887,928 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,
I have arrays, To illustrate;

C#
reached==false;
int [,] aaa = {{1,6},{9,6},{9,13},{12,16}};
int [,] bbb = {{6,7},{6,11},{6,2},{16,11}};
int [,] ccc = {{11,6},{7,6},{11,16}};


What i want to do is to start from aaa integer to manage to go to c[x,1] in some spesific
rules which i will express in below lines:

(x,y,z are int variables coming from for loops)
if aaa[x,1]==bbb[y,0] ---- first condition is met. If it is met continue with searcing next array. Namely, if bbb[y,1]==ccc[z,0] is met, all conditions will be met.
To show it in our arrays with an example,

{1,6} -> {6,11} -> {11,16} //can go from aaa[0,1] to ccc[2,1]

So the direction is 6->11->16 is okay. reached==true;
One more condition, every number can be used one time.

ex: if it goes in this way : {1,6}-->{6,7}-->{7,6} | 1->6->7->6 is not valid. reached==false; Because it contains "number 6" two times.

What i want to do exactly is that after completing finding any correct series,
two things to be done :
reached== true; break; //exit for loop

How can i do all these things with c#?

Thanks in advance!
Posted
Comments
[no name] 18-Aug-12 12:06pm    
So instead of doing as you were advised to do, you repost this without trying anything. This stinks of homework. Regardless of whether it is homework or not you need to at least try to do something yourself. To answer your only question, "How can i do all these things with c#"... you use for loops, booleans and the break statement.
tyk37 18-Aug-12 12:15pm    
I'm trying to solve this issues for days. I always use this website as a last resort. I am very preplexed. I can not solve it.
Richard MacCutchan 18-Aug-12 12:28pm    
Which part can you not understand? This is just a simple matter of comparing two integers, the first one from one array and the second from the other array. Once you have a match you swap numbers and continue on the last array. Try writing out the individual steps using pen and paper and then convert that to code; you'll be amazed how much you can learn in the process.
tyk37 18-Aug-12 15:06pm    
it is not just three integer arrays, its number can be 10 int arrays according to another for loop. I have to compare 1 and 2 if true, 2and 3 if true 3and 4 etc etc. i have to use a lot of loop inside loop. That makes me confused.
Richard MacCutchan 18-Aug-12 15:33pm    
It does not matter how many arrays there are, or how many elements in each array, the same rules apply; it just takes longer to complete.

Sounds like you are working your way through something like this: Combinatorics and Graph Theory[^]

Don't do it like this
int [,] aaa = {{1,6},{9,6},{9,13},{12,16}};
int [,] bbb = {{6,7},{6,11},{6,2},{16,11}};
int [,] ccc = {{11,6},{7,6},{11,16}};


You need a set of nodes, each identified by an id and edges between connected nodes. Start out by creating a Node class, then an Edge class, add the nodes to a dictionary (for faster lookup), construct the graph based on your information.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Aug-12 12:43pm    
Good idea, my 5.

Am I along or not? Do you agree that the question is not fully and correctly formulated, in terms of required algorithm? I also gave OP some advice, but still based on the declaration you dislike. Please see my answer.

You know, it strongly depends on the purpose which we don't know. In general, the array declarations are legitimate and can be used, without the more flexible collections with nodes and edges...

--SA
Espen Harlinn 18-Aug-12 13:08pm    
Thank you, Sergey - and yes you are right, I just feel that going for a graph is cleaner :-D
Wendelius 19-Aug-12 5:51am    
Good answer.
Espen Harlinn 19-Aug-12 5:59am    
Thank you, Mika :-D
This formulation is better, but you ignored part of my advice: you should have formulated it in formal way as in mathematics. You still try to explain it all on examples. Still not clear. Use some undefined terms like "go from to", "specific rules".

Do you know the solution algorithm, so you could do in on paper? If so, just move it into code.

However, if you have a problem in pure programming, I suspect you could miss just one detail, how to determine loop variable ranges (bad term, but it's widely used; better term would be "domains").

Here is the thing:
http://msdn.microsoft.com/en-us/library/system.array.aspx[^],
http://msdn.microsoft.com/en-us/library/system.array.getlowerbound.aspx[^],
http://msdn.microsoft.com/en-us/library/system.array.getupperbound.aspx[^].

For example:
C#
int aaa0Lower = aaa.GetLowerBound(0); //0
int aaa0Upper = aaa.GetUpperBound(0); //1
int aaa1Upper = aaa.GetUpperBound(1); //3
//...
int ccc0Upper = ccc.GetUpperBound(0); //1
int ccc1Upper = ccc.GetUpperBound(1); //2


Look at other properties and method of this class, this is useful to know.

Are you getting the idea. Anything else? Nested loops, breaks, variable scopes? Elementary notions, but you need to know it all. If this is still the algorithm, you still did not explain everything clearly and formally. And translation of those vague statements into the formal system is almost the same as programming. Learn one and you will do well in another.

—SA
 
Share this answer
 
v3
Comments
Espen Harlinn 18-Aug-12 12:47pm    
Yes, it could have been better formulated ;)
Sergey Alexandrovich Kryukov 18-Aug-12 12:53pm    
Thank you, Espen.
I think we gave some different ideas, now it's OP's turn to do some work...
--SA
Espen Harlinn 18-Aug-12 12:54pm    
Right ;)
Wendelius 19-Aug-12 5:52am    
My 5
Sergey Alexandrovich Kryukov 19-Aug-12 20:09pm    
Thank you, Mika.
--SA
C#
static void Main(string[] args)
{
    int[,] aaa = { { 1, 6 }, { 9, 6 }, { 9, 13 }, { 12, 16 } };
    int[,] bbb = { { 6, 7 }, { 6, 11 }, { 6, 2 }, { 16, 11 } };
    int[,] ccc = { { 11, 6 }, { 7, 6 }, { 11, 16 } };


    int i, j, k;
    for (i = 0; i <= aaa.GetUpperBound(0); i++)
        for (j = 0; j <= bbb.GetUpperBound(0); j++)

            if (aaa[i, 1] == bbb[j, 0])
            {
                for (k = 0; k <= ccc.GetUpperBound(0); k++)
                    if (ccc[k, 0] == bbb[j, 1])
                    {
                        int[] direction = { aaa[i, 1], bbb[j, 1], ccc[k, 1] };
                        if (direction[0] != direction[2] &&
                             direction[1] != direction[2] &&
                             direction[0] != direction[1])

                            Console.WriteLine(string.Format("{0},{1} > {2},{3} > {4},{5}", aaa[i, 0], aaa[i, 1], bbb[j, 0], bbb[j, 1], ccc[k, 0], ccc[k, 1]));
                    }
            }
    Console.ReadKey();
}

The output will be:
1,6 > 6,11 > 11, 16
9,6 > 6,11 > 11,16
12,16 > 16,11 > 11,6

It takes an hour to solve it.
Accept it if I give you what you needed.
 
Share this answer
 
v2
Comments
tyk37 19-Aug-12 9:23am    
Thank you so much. This is exactly what i want to do.
tyk37 19-Aug-12 13:52pm    
One more question, i wonder why dont you use curly brackets after for loops?
I couldnt understand what is for this kind of shorthand loops.
[no name] 19-Aug-12 14:01pm    
If your loop has just one condition or one line of code, you can do that.
tyk37 20-Aug-12 7:12am    
In the following part of my project, i encountered some problems. int Arrays raised 9 pieces. In this regard, directions raised dramatically.
int[,] aaa , bbb,ccc,ddd,eee,fff,ggg,hhh,iii.
Should i maintain the same coding logic or i do write it another way?
If you show me a way of out of this, i would be very grateful.
tyk37 19-Aug-12 13:59pm    
i figured it out :)
I think it applies only first statement block until the last loop value. With the last one, it applies the second one ..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900