By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,631 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,631 IT Pros & Developers. It's quick & easy.

Array search

P: n/a
Hello,

I'm a very very newbie in C# I did start 2 days ago, I get Visual
Stuido 2005 C#

I try to understand how to manage the arrays
I did write this

int[] tempo=new int[3];
int[,] list = { { 1, 2, 3 } , { 11,12,13} };
int[] search = { 15, 123, 12 };

I have to goals here

My main goal is to find if values in array search exist in list[0]
and list[1]

My only idea, with my knowledge, is

- 1st to copy all the value of the multi-dimensional array list[1,] to
a tempo array

- to search in the temporary array, all the value of search, I
programmed the method contains for that.

To be frank I'm sure there is much simpler than this.

Thank you fro your help

namespace loto_Sharp
{

class Program
{
// Search value of array search in the temporary array tempo
private static bool contains(int[] tempo,int[] search)
{
Array.Sort(tempo);
Array.Sort(search);

for (int bcl = 0; bcl < tempo.GetLength(0); bcl++)
{
System.Console.Write("Cherche {0}", search[bcl]);
if (Array.BinarySearch(tempo, search[bcl]) >= 0)
{
System.Console.WriteLine(" - {0} Found",
search[bcl]);
return true;
}
else
{
System.Console.WriteLine(" - {0} Not found",
search[bcl]);
}
}
return false;
}
static void Main(string[] args)
{
int[] tempo=new int[3];
int[,] list = { { 1, 2, 3 } , { 11,12,13} };
int[] search = { 15, 123, 12 };

for (int bcl1 = 0; bcl1 < list.GetLength(0); bcl1++) //
from 0 to 1 there is 2 array in list
{
for (int bcl = 0; bcl < list.GetLength(1); bcl++) //
from 0 to 2 there is 3 elements in each array of list
{
tempo[bcl] = list[bcl1, bcl]; // I do convert a
multi array into a single array
System.Console.WriteLine("tempo[{0}]={1}", bcl,
tempo[bcl]);
}

if (contains(tempo, search))
System.Console.WriteLine("= FOUND ="); else
System.Console.WriteLine("= not FOUND ="); ;
System.Console.WriteLine("======================== ========================");
}
}

}
}
Jan 24 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
cyrvb wrote:
Hello,

I'm a very very newbie in C# I did start 2 days ago, I get Visual
Stuido 2005 C#

I try to understand how to manage the arrays
I did write this

int[] tempo=new int[3];
int[,] list = { { 1, 2, 3 } , { 11,12,13} };
int[] search = { 15, 123, 12 };

I have to goals here

My main goal is to find if values in array search exist in list[0]
and list[1]

My only idea, with my knowledge, is

- 1st to copy all the value of the multi-dimensional array list[1,] to
a tempo array

- to search in the temporary array, all the value of search, I
programmed the method contains for that.

To be frank I'm sure there is much simpler than this.


you can save yourself the binary search by using a Dictionary<int,
object> and store all ints in list 0 and list 1 in the key (and null in
the value) then do a dictionary.ContainsKey(value) for each value in
your array.

FB
--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Jan 24 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.