covertx

Hi all, I don't know if you've read my previous search regarding the searching of Arrays, but I think this additional information may help put this multiple array search into context...

Here is how I'm creating the array to store a number of other arrays:

Code Snippet

private char[][] rows = new char[10][];
private int iRows = 0;

private char[] char0 = "abcdefghijklmnopqrstuvwxyz".ToCharArray();

private char[] char1 = "zyxwvutsrqponmlkjihgfedcba".ToCharArray();


The character arrays featured above are just examples similar to the ones I'm using in my programme...

One the arrays have been initialised, I add them to the rows array like so:

Code Snippet

iRows = 0;
rows.SetValue(char0, 0); rows.SetValue(char1, 1);


I do this 10 times for 10 different arrays, starting for the 0th array you can see above. Now that I have my arrays in possition, how do I search them

I've been trying the BinarySearch class, but have had no luck using this! See below:

Code Snippet

object obj1 = c;
int retVal = Array.BinarySearch(rows[iRows], obj1);


I want to be able to search across these 10 arrays dynamicall so I'm using an integer called iRows to navigate through the 10 arrays... When I perform this search, I get an exception informing me that my array contains a null value, but I've no idea why!

Any help will be greatly appreciated!

Rob


Re: .NET Base Class Library BinarySearch...

mberseth

Hi Rob -

I tried to reproduce the problem using the following code segment, but I was unsuccessful:

Code Snippet

char[][] rows = new char[10][];

int iRows = 0;

char[] char0 = "abcdefghijklmnopqrstuvwxyz".ToCharArray();

char[] char1 = "zyxwvutsrqponmlkjihgfedcba".ToCharArray();

iRows = 0;

rows.SetValue(char0, 0);

rows.SetValue(char1, 1);

object obj1 = 'c';

int retVal = Array.BinarySearch(rows[iRows], obj1);

Console.WriteLine(retVal);

The value of retVal is 2 - as expected.

Would you mind posting a running example






Re: .NET Base Class Library BinarySearch...

decyclone

This code works fine :

Char[][] Rows = new Char[10][];

for (Int32 Index = 0; Index < Rows.Length; Index++)
{
Rows.SetValue(new String((Char)Index, 10).ToCharArray(), Index);
}

Char CharToSearch = (Char)1;

for (Int32 Index = 0; Index < Rows.Length; Index++)
{
Int32 SearchIndex = Array.BinarySearch(Rows[Index], CharToSearch);
if (SearchIndex > -1)
{
MessageBox.Show(String.Format("Match Found At Position [{0}][{1}]", Index, SearchIndex));
}
}






Re: .NET Base Class Library BinarySearch...

covertx

Here you are, the whole test function begins when a user presses a button:

Code Snippet

private char[][] rows = new char[2][];
private int iRows = 0;

private char[] char0 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
private char[] char1 = "ZYXWVUTSRQPONMLKJIHGFEDCBA".ToCharArray();

private void butSearch_Click(object sender, EventArgs e)
{
iRows = 0;
char[] chars = tb.Text.ToCharArray();
StringBuilder sb = new StringBuilder();

foreach (char c in chars)
{
object obj1 = c;
int retVal = Array.BinarySearch(rows[iRows], obj1);
if (retVal >= 0)
{
MessageBox.Show(retVal.ToString());
}
}
}


I hope this helps!




Re: .NET Base Class Library BinarySearch...

covertx

Hello! Thanks for your input and you code suggestions!

I've tried your code, but get exactly the same error... I've no idea why either!

Code Snippet

private void butSearch_Click(object sender, EventArgs e)
{
iRows = 0;
Char CharToSearch = (Char)1;

for (Int32 Index = 0; Index < rows.Length; Index++)
{
Int32 SearchIndex = Array.BinarySearch(rows[Index], CharToSearch);
if (SearchIndex > -1)
{
MessageBox.Show(String.Format("Match Found At Position [{0}][{1}]", Index, SearchIndex));
}
}
}


So the user will enter "A" for example and the index of the first array at "0" should be A, then if the user enters "AA" they should get the first indexes of both array0 and array1 resulting in the output "AZ"

But null values are still being found for some reason! :-(

Any ideas

Sorry to go on! Smile





Re: .NET Base Class Library BinarySearch...

covertx

Hi guys, the code now looks like this:

Code Snippet

private void butSearch_Click(object sender, EventArgs e)
{
iRows = 0;
char[] chars = tb.Text.ToCharArray();
foreach (char c in chars)
{
Char CharToSearch = (Char)c;

for (Int32 Index = 0; Index < rows.Length; Index++)
{
Int32 SearchIndex = Array.BinarySearch(rows[Index], CharToSearch);
if (SearchIndex > -1)
{
MessageBox.Show(String.Format("Match Found At Position [{0}][{1}]", Index, SearchIndex));
}
else
{
MessageBox.Show("NAAH MAAAN!");
}
}
}
}


But unfortunately does not work, do you know why theres a null value being returned




Re: .NET Base Class Library BinarySearch...

decyclone

This code works with me :

private char[][] Rows = new char[2][];
private int iRows = 0;

private char[] char0 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
private char[] char1 = "ZYXWVUTSRQPONMLKJIHGFEDCBA".ToCharArray();

private void button1_Click(object sender, EventArgs e)
{
iRows = 0;
Char CharToSearch = 'A';

Rows.SetValue(char0, 0);
Rows.SetValue(char1, 1);

for (Int32 Index = 0; Index < Rows.Length; Index++)
{
Int32 SearchIndex = Array.BinarySearch(Rows[Index], CharToSearch);
if (SearchIndex > -1)
{
MessageBox.Show(String.Format("Match Found At Position [{0}][{1}]", Index, SearchIndex));
}
}
}





Re: .NET Base Class Library BinarySearch...

covertx

To put this example into further context, I've created a simple Vigenere cypher programme out of my existing programme:

Code Snippet

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AERRPattern
{
public partial class FORMAERRP : Form
{
public FORMAERRP()
{
InitializeComponent();
}

private char[][] rows = new char[10][];
private int iRows = 0;

private char[] char0 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
private char[] char1 = "
BCDEFGHIJKLMNOPQRSTUVWXYZ0123456789A".ToCharArray();
private char[] char2 = "
CDEFGHIJKLMNOPQRSTUVWXYZ0123456789AB".ToCharArray();
private char[] char3 = "
DEFGHIJKLMNOPQRSTUVWXYZ0123456789ABC".ToCharArray();
private char[] char4 = "
EFGHIJKLMNOPQRSTUVWXYZ0123456789ABCD".ToCharArray();
private char[] char5 = "
FGHIJKLMNOPQRSTUVWXYZ0123456789ABCDE".ToCharArray();
private char[] char6 = "
GHIJKLMNOPQRSTUVWXYZ0123456789ABCDEF".ToCharArray();
private char[] char7 = "
HIJKLMNOPQRSTUVWXYZ0123456789ABCDEFG".ToCharArray();
private char[] char8 = "
IJKLMNOPQRSTUVWXYZ0123456789ABCDEFGH".ToCharArray();
private char[] char9 = "
JKLMNOPQRSTUVWXYZ0123456789ABCDEFGHI".ToCharArray();

private StringBuilder outputTest = new StringBuilder();

private void butEncrypt_Click(object sender, EventArgs e)
{
iRows = 0;
rows.SetValue(char0, 0); rows.SetValue(char5, 5);
rows.SetValue(char1, 1); rows.SetValue(char6, 6);
rows.SetValue(char2, 2); rows.SetValue(char7, 7);
rows.SetValue(char3, 3); rows.SetValue(char8, 8);
rows.SetValue(char4, 4); rows.SetValue(char9, 9);

int currentCharCode;
char currentChar;

StringBuilder sb = new StringBuilder();
char[] chars = tb.Text.ToCharArray();

foreach (char c in tb.Text)
{
currentChar = c;
currentCharCode = (int)currentChar;
currentCharCode -= 65;
if(currentCharCode >= 0 && currentCharCode <= 35)
{
sb.Insert(sb.Length, rows[iRows][currentCharCode]);
}
if (currentCharCode >= -49 && currentCharCode <= -40)
{
sb.Insert(sb.Length, rows[iRows][currentCharCode + 74]);
}

if (iRows < 10)
{
iRows++;
}
if (iRows == 10)
{
iRows = 0;
}
}

MessageBox.Show(sb.ToString());
}

private void butDecrypt_Click(object sender, EventArgs e)
{
iRows = 0;
char[] chars = tb.Text.ToCharArray();

foreach(char c in chars)
{
Char CharToSearch = 'A';

for (Int32 Index = 0; Index < rows.Length; Index++)
{
Int32 SearchIndex = Array.BinarySearch(rows[Index], CharToSearch);
if (SearchIndex > -1)
{
MessageBox.Show(String.Format("Match Found At Position [{0}][{1}]", Index, SearchIndex));
}
}
}
}
}
}


I don't know if it's the way I've structured my code early on in the programme, but it doesn't seem to work here!

Does this help you understand my code a little better




Re: .NET Base Class Library BinarySearch...

decyclone

Change your Form's constructor to :

public FORMAERRP()
{
InitializeComponent();

rows.SetValue(char0, 0); rows.SetValue(char5, 5);
rows.SetValue(char1, 1); rows.SetValue(char6, 6);
rows.SetValue(char2, 2); rows.SetValue(char7, 7);
rows.SetValue(char3, 3); rows.SetValue(char8, 8);
rows.SetValue(char4, 4); rows.SetValue(char9, 9);
}

And remove rows.SetValue(...) statements from butEncrypt's Click event. And the erroronly occured when you Run the application, type a value in TextBox and click Decrypt button, right If you click Encrypt button first, and then Decrypt, everything works fine.

The reason is your array didn't get initialized with SetValue() function until you press Encrypt button. Now you put that code in Form's constructor and everything works great.







Re: .NET Base Class Library BinarySearch...

Matthew Watson

What's supposed to go wrong there

I tried your code:

(1) Entered "abcdef" into the textbox (without the quotes).
(2) Clicked Encrypt button - it said "68AC"
(3) Then clicked Decrypt button - it says:

Match found at position [0][0]
Match found at position [9][27]

(and repeated that several times)


[EDIT] See what decyclone said.




Re: .NET Base Class Library BinarySearch...

decyclone

I don't know what's wrong with Array.BinarySearch(), so I made some major changes to the code. And it is working absolutely fine. Check it out. You should be able to understand it easily.

Code :


public partial class FORMAERRP : Form
{
public FORMAERRP()
{
InitializeComponent();

rows.SetValue(char0, 0); rows.SetValue(char1, 1); rows.SetValue(char2, 2);
rows.SetValue(char3, 3); rows.SetValue(char4, 4); rows.SetValue(char5, 5);
rows.SetValue(char6, 6); rows.SetValue(char7, 7); rows.SetValue(char8, 8);
rows.SetValue(char9, 9);
}

private String[] rows = new String[10];
private int iRows = 0;

private String char0 = "RSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQ";
private String char1 = "89ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567";
private String char2 = "PQRSTUVWXYZ0123456789ABCDEFGHIJKLMNO";
private String char3 = "6789ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
private String char4 = "NOPQRSTUVWXYZ0123456789ABCDEFGHIJKLM";
private String char5 = "456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123";
private String char6 = "LMNOPQRSTUVWXYZ0123456789ABCDEFGHIJK";
private String char7 = "23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ01";
private String char8 = "JKLMNOPQRSTUVWXYZ0123456789ABCDEFGHI";
private String char9 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";


private StringBuilder outputTest = new StringBuilder();

private void butEncrypt_Click(object sender, EventArgs e)
{
iRows = 0;

int currentCharCode;
char currentChar;

StringBuilder sb = new StringBuilder();
char[] chars = tb.Text.ToCharArray();

foreach (char c in tb.Text)
{
currentChar = c;
currentCharCode = (int)currentChar;

if (Char.IsLetter(c))
{
currentCharCode -= 65;
}
else if (Char.IsNumber(c))
{
currentCharCode -= 48;
currentCharCode += 26;
}
else
{
throw new ArgumentException("Invalid character in string : " + c.ToString());
}

sb.Append(rows[iRows][currentCharCode]);

iRows++;
if (iRows >= 10)
{
iRows = 0;
}
}
tb.Text = sb.ToString();
}

private void butDecrypt_Click(object sender, EventArgs e)
{
iRows = 0;
char[] chars = tb.Text.ToCharArray();
StringBuilder sb = new StringBuilder();

foreach (char c in chars)
{
Char CharToSearch = c;
Int32 SearchIndex = rows[iRows].IndexOf(c);

if (SearchIndex >= 26)
{
SearchIndex -= (26 - 48);
}
else
{
SearchIndex += 65;
}

sb.Append((Char)SearchIndex);

iRows++;
if (iRows >= 10)
{
iRows = 0;
}
}

tb.Text = sb.ToString();
}
}






Re: .NET Base Class Library BinarySearch...

covertx

Thank you so much decyclone!

Your help has been invaluble! Big Smile I really appreciate the care you've shown and the help you've given!

Take care

Rob




Re: .NET Base Class Library BinarySearch...

decyclone

Thanks for appreciation.