Array search

C

cyrvb

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("================================================");
}
}

}
}
 
F

Frans Bouma [C# MVP]

cyrvb said:
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


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top