Hi guys, for a University assignment (referral, eek!) I need to hash and
sort some data. Which isn't too bad, but then I need to write a program
displaying the results in C#. Here's the data I need to work with, and
my worked out values:
Hashing function H(Ln) = n%10
Probing function P(Ln) = max (1, n/10)
This tells me that the data should appear in the table as such:
0 -
1 - A1
2 -
3 - Y25
4 - O15
5 - E5
6 -
7 - S19
8 - R19
9 - E5
But, when I code it, the results tell me S19 appears in row 6 as opposed
to row 7! Please can you take a look at my code and tell me exactly
where I've gone wrong? Thanks.
C#
using System;
using System.Collections.Generic;
using System.Text;
namespaceReferralAssignment1Question1
{
publicclassReferralHashing
{
publicstruct Referral
{
//Define the variablespublicint value1;
publicstring withLetter;
publicReferral(int a, string b, string c)
{
value1 = a;
withLetter = b;
}
}
//Input the hashing and probing formulaspublicinthashing(int n)
{
int position;
position = n % 10;
return position;
}
publicintprobe(int n)
{
int position;
position = Math.Max(1, n / 10);
return position;
}
publicintwrap(int h, int p, int[] f)
{
int d;
d = h - p;
if (d < 0)
{
d = 10 + (h - p);
}
if (f[d] == 1)
{
d = d - p;
d = wrap(d, p, f);
}
return d;
}
publicstaticvoidMain(string[] args)
{
//Input the values to be processedint key = 0;
int pos = 0;
int h;
int p;
int[] flag = newint[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] value1 = newint[7] { 1, 9, 18, 19, 5, 15, 25 };
string[] withLetter = newstring[7] {"A1",
"I9",
"R18",
"S19",
"E5",
"O15",
"Y25"};
Referral[] bl = new Referral[10];
for (int i = 0; i < 10; i++)
{
bl[i] = new Referral();
}
ReferralHashing blh = new ReferralHashing();
for (int i = 0; i < 7; i++)
{
key = value1[i];
h = blh.hashing(key);
if (flag[h] == 1)
{
p = blh.probe(key);
pos = blh.wrap(h, p, flag);
}
else
{
pos = h;
}
flag[pos] = 1;
bl[pos].value1 = value1[i];
bl[pos].withLetter = withLetter[i];
//Display the results in the console
Console.WriteLine(bl[pos].withLetter + "'s position in the table: " + pos);
}
}
}
}
Answers
Know the answer? Post it. Somebody with the same question will find it here.
Answers
Know the answer? Post it. Somebody with the same question will find it here.