Error
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.

int

How to Sort an Arrays of primitive types
Friday, 23 December 2011 00:21
To sort array of primitive types such as int, double or string use method Array.Sort(Array) with the array as a paramater. The primitive types implements interface IComparable, which is internally used by the Sort method (it calls IComparable.ComĀ­pareTo method). See example how to sort int array:


// sort int array
int[] intArray = new int[5] { 8, 10, 2, 6, 3 };
Array.Sort(intArray);
// write array
foreach (int i in intArray) Console.Write(i + " ");

//OUTPUT
2 3 6 8 10

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/XAjB9RinXZI/14309

 
List blocking processes in SQL server
Wednesday, 21 December 2011 01:41
We use this quick script to list those processes that are blocking other processes in SQL server. What you do with the information is up to you, but we suggest checking to ensure you're only locking when you need to (SET READ UNCOMMITTED or use NOLOCK, ROWLOCK hints if appropriate), and we also suggest you review your SQL setup: is your hardware fast enough? Have you separated out your logging, data and tempDB files onto separate volumes?

Usage:

ListBlocking [@KillOrphanedProcesses = 0]

If @KillOrphanedProcesses is set to 1 then the script will attempt to kill orphaned processes.



IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[ListBlocking]')
AND type in (N'P', N'PC'))
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[ListBlocking] AS'
GO

/*===================================================================

Description: Wrapper to sp_who2 to show only those processes that
are blocking.

Please see http://support.microsoft.com/kb/224453 for
more info on locking.

===================================================================*/

ALTER procedure ListBlocking
(
@KillOrphanedProcesses bit = 0
)
as

IF OBJECT_ID('tempdb..#Process') IS NOT NULL drop table #Process
IF OBJECT_ID('tempdb..#BlockingProcess') IS NOT NULL drop table #BlockingProcess

create table #Process (
SPID int,
Status varchar(500),
Login varchar(500),
Hostname varchar(500),
BlkBy varchar(50),
DBName varchar(500),
Command varchar(500),
CPUTime int,
DiskIO int,
LastBatch varchar(500),
ProgramName varchar(500),
SPID2 int,
RequestId int
)

CREATE TABLE #BlockingProcess (
BlockingProcessID int IDENTITY(1,1),
ProcessID varchar(20),
EventType varchar(100),
Parameters varchar(100),
EventInfo varchar(500),
CPUTime int,
DiskIO int,
TransactionCount int
)

DECLARE @BlockingPID varchar(20),
@CPUTime int,
@DiskIO int,
@TransactionCount int

-- Let SQL Server get us a list of what's going on
SET NOCOUNT ON
insert into #Process exec sp_who2

-- From this list of what's going on, get those items that are blocked, and loop through them
DECLARE ProcessCursor CURSOR FAST_FORWARD FOR
SELECT BlkBy, SUM(CPUTime) as CPUTime, SUM(DiskIO) as DiskIO
FROM #Process
WHERE ISNULL(BlkBy,'') <> ''
GROUP BY BlkBy
OPEN ProcessCursor

FETCH NEXT FROM ProcessCursor INTO @BlockingPID, @CPUTime, @DiskIO
WHILE @@FETCH_STATUS = 0
BEGIN

-- Only valid PIDs
if ISNULL(@BlockingPID, '') <> '' and @BlockingPID <> '0' AND @BlockingPID <> ' .'
BEGIN

-- For each blocked process get what information we can on the process and what's blocking it
SET @TransactionCount = 0

-- -2 = The blocking resource is owned by an orphaned distributed transaction.
IF SUBSTRING(@BlockingPID, 1, 2) = '-2'
BEGIN

-- For orphaned processes we need to get the Unit of work if we're to do anything with it
DECLARE @UnitOfWork varchar(50)
select top 1 @UnitOfWork = ISNULL(req_transactionUOW, '')
from master..syslockinfo
where req_spid = -2

if @KillOrphanedProcesses = 1
BEGIN
if @UnitOfWork <> '' exec('KILL ''' + @UnitOfWork + '''')

INSERT INTO #BlockingProcess (EventType, Parameters, EventInfo)
VALUES('', '', '- Killed UOW ' + @UnitOfWork + ' -')
END
ELSE
BEGIN
INSERT INTO #BlockingProcess (EventType, Parameters, EventInfo)
VALUES('', '', '- Orphaned. UOW = ' + @UnitOfWork + ' -')
END
END

-- -3 = The blocking resource is owned by a deferred recovery transaction.
ELSE IF SUBSTRING(@BlockingPID, 1, 2) = '-3'
BEGIN
INSERT INTO #BlockingProcess (EventType, Parameters, EventInfo)
VALUES('', '', '- deferred recovery transaction -')
END

-- -4 = Session ID of the blocking latch owner could not be determined at this
-- time because of internal latch state transitions.
ELSE IF SUBSTRING(@BlockingPID, 1, 2) = '-4'
BEGIN
INSERT INTO #BlockingProcess (EventType, Parameters, EventInfo)
VALUES('', '', '- Latch owner could not be determined -')
END

-- Nothing unusual here. A process is being blocked by another processes, so let's get the
-- info on the process that's doing the blocking
ELSE
BEGIN

SELECT @TransactionCount = open_tran FROM master.sys.sysprocesses WHERE SPID=@BlockingPID

INSERT INTO #BlockingProcess (EventType, Parameters, EventInfo)
EXEC ('DBCC INPUTBUFFER(' + @BlockingPID + ') WITH NO_INFOMSGS')
END

-- Add some aggregate info on the blocking processes
UPDATE #BlockingProcess
SET ProcessID = @BlockingPID,
CPUTime = @CPUTime,
DiskIO = @DiskIO,
TransactionCount = @TransactionCount
WHERE BlockingProcessID = SCOPE_IDENTITY()
END

FETCH NEXT FROM ProcessCursor INTO @BlockingPID, @CPUTime, @DiskIO
END

CLOSE ProcessCursor
DEALLOCATE ProcessCursor

-- Display the results
SELECT ProcessID, EventInfo, TransactionCount, CPUTime, DiskIO FROM #BlockingProcess

SET NOCOUNT OFF

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/FmZn5U-2Xqg/14271

 
Count total number of occurrences of string inside another string in SQL
Wednesday, 21 December 2011 01:21
I had a need to count the number of times a certain string appeared within a column in a SQL table. I came up with this simple function that may be of use to others.


-- Setup: Create a blank function if none exists. This allows us to
-- rerun this single script each time we modify this function

IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'dbo.com_CountString')
AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
EXEC dbo.sp_executesql @statement = N'create function dbo.com_CountString() RETURNS INT AS BEGIN RETURN '''' END'
go

-- Create the actual function

/*====================================================================================
Counts the number of times @SearchString appears in @Input.
====================================================================================*/
ALTER FUNCTION dbo.com_CountString(@Input nVarChar(max), @SearchString nVarChar(1000))
RETURNS INT
BEGIN
DECLARE @Count INT, @Index INT, @InputLength INT, @SearchLength INT
DECLARE @SampleString INT

if @Input is null or @SearchString is null
return 0

SET @Count = 0
SET @Index = 1
SET @InputLength = LEN(@Input)
SET @SearchLength = LEN(@SearchString)

if @InputLength = 0 or @SearchLength = 0 or @SearchLength > @InputLength
return 0

WHILE @Index <= @InputLength - @SearchLength + 1
BEGIN
IF SUBSTRING(@Input, @Index, @SearchLength) = @SearchString
BEGIN
SET @Count = @Count + 1
SET @Index = @Index + @SearchLength
END
ELSE
SET @Index = @Index + 1
END

RETURN @Count
END
GO


And finally The function can be called:

SELECT dbo.com_CountString('This is a string', 'is')

SELECT dbo.com_CountString(MyTable.MyColumn, 'search string')
FROM MyTable
WHERE MyTable.MyKey = @Key

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/ZLK4a-Sco8k/14265

 
GSM 7 bit encoding/decoding
Friday, 02 December 2011 03:39
GSM 7 bit encoding/decoding


package encoding;

/**
*
* Testing on the web site can be used:

* http://www.smartposition.nl/resources/sms_pdu.html

*
* @author Mlungisi Sincuba
*
*/
public class MluImpl {


static final int[] GSM7CHARS = {
0x0040, 0x00A3, 0x0024, 0x00A5, 0x00E8, 0x00E9, 0x00F9, 0x00EC,
0x00F2, 0x00E7, 0x000A, 0x00D8, 0x00F8, 0x000D, 0x00C5, 0x00E5,
0x0394, 0x005F, 0x03A6, 0x0393, 0x039B, 0x03A9, 0x03A0, 0x03A8,
0x03A3, 0x0398, 0x039E, 0x00A0, 0x00C6, 0x00E6, 0x00DF, 0x00C9,
0x0020, 0x0021, 0x0022, 0x0023, 0x00A4, 0x0025, 0x0026, 0x0027,
0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
0x00A1, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
0x0058, 0x0059, 0x005A, 0x00C4, 0x00D6, 0x00D1, 0x00DC, 0x00A7,
0x00BF, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
0x0078, 0x0079, 0x007A, 0x00E4, 0x00F6, 0x00F1, 0x00FC, 0x00E0,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
};


static final int[] BYTE_TO_CHAR_ESCAPED_DEFAULT = {
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, 0x000C, -1, -1, -1, -1, -1,
-1, -1, -1, -1, 0x005E, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
0x007B, 0x007D, -1, -1, -1, -1, -1, 0x005C,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, 0x005B, 0x007E, 0x005D, -1,
0x007C, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, 0x20AC, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1,
};
/**
* @param args
*/
public static void main(String[] args) {
String encoded = "C8329BFD065DDF723619";
String decoded = decode(encoded);
System.out.println(decode(encoded) + "-" + encoded);
System.out.println(encode(decoded) + "-" + decoded);

}

/**
*
* @param ascci
* @return
*/
public static String encode(String ascci){
String encoded = "";
int length = ascci.length();
int gsm7Length = GSM7CHARS.length;
for(int i = length; i > 0; i--){
char c = ascci.charAt((i-1));
for(int j = 0; j < gsm7Length; j++) {
if ((char)GSM7CHARS[j] == c) {
int num = GSM7CHARS[j];
String code = makeSevenBits(Integer.toBinaryString(num));
encoded+=code;
}
}
}
encoded = from7BitBinaryToHexReversed(encoded);
return encoded;
}

public static String decode(String hex) {
String binary = "";
String decoded = "";
int length = hex.length();
for(int i = length; i >= 2; i-=2){
String twos = hex.substring((i-2), i);//2 characters at a time on reverse direction
binary+=fromHexTo8BitBinary(twos);//Convert to 8 bit binary
}
int gsm7Length = GSM7CHARS.length;
for(int i = binary.length(); i >= 7; i-=7){
String seven = binary.substring((i-7), i);//Chop into 7 bits binary in reverse direction
int decimalOfSeven = Integer.parseInt(seven,2);

for(int j = 0; j < gsm7Length; j++) {
if (GSM7CHARS[j] == decimalOfSeven) {
decoded +=(char)decimalOfSeven;//Do translation
}
}
}

return decoded;
}

private static String from7BitBinaryToHexReversed(String binary){
String ret = "";
String temp = "";
int length = binary.length();
for (int i = length; i >= 8 ; i-=8) {//read in reverse direction
temp = binary.substring((i-8), i);//chop into 8 bits
int val = Integer.parseInt(temp, 2);//get decimal value of binary
String code = Integer.toHexString(val);
if (code.length() < 2){
code = "0" + code;
}
ret+=code;//Integer.toHexString(val);//Convert to hexadecimal
}
return ret.toUpperCase();
}

private static String makeSevenBits(String binaryStr){
String ret = "";
String zeros = "";
int length = binaryStr.length();
int appends = 7 - length;
for(int i = 0; i < appends; i++) {
zeros +="0";
}
ret = zeros + binaryStr;
return ret;
}

private static String fromHexTo8BitBinary(String hex){
String ret = "";
String binary = Integer.toBinaryString(Integer.parseInt(hex, 16));
int length = 8 - binary.length();
for (int i = 0; i < length; i++) {
ret +="0";
}
ret += binary;
return ret;
}

}

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/yWf1tjKKTpc/14041

 
Greedy Knapsack Algorithm (code in c++)
Sunday, 21 August 2011 01:03
// Greedy Knapsack


/*Greedy Knapsack Program
Source code created by S.Kaarthikeyan from
Computer Technology And Application Department from
Coimbatore Institute of Technology
*/

# include

int w[10],p[10],p_no[10],n,m;
float pwr[10];

template
void swap(T *a,int i,int j)
{
T temp;
temp=a[j];
a[j]=a[i];
a[i]=temp;
return;
}

void line()
{
cout<<"\n";
for(int i=0;i<80;i++)
cout<<"=";
cout<<"\n";
}

void display()
{
line();
cout<<"\n\t\t\t\tKnapsack\n";
line();
cout<<"\nNumber Of Products:\t"< line();
cout<<"\nSl.No\t\tWeight\t\tProfit\t\tPWR\n";
line();
for(int i=0;i cout< line();
}

void knapsack()
{
int u,i;
u=m;
float x[10];
for(i=0;i x[i]=0;
for(i=0;i {
if(w[i]>u)
break;
else
{
x[i]=1;
u=u-w[i];
}
}
if(i<=n)
x[i]=(float)u/w[i];
for(i=0;i {
for(int j=i+1;j {
if(p_no[i]>p_no[j])
{
swap(p_no,i,j);
swap(x,i,j);
swap(p,i,j);
}
}
}
float opt_solution=0;
cout<<"\nThe Order is\n\n";
cout<<"(";
for(i=0;i cout< cout<<")";
cout<<"\t\t(";
for(i=0;i {
cout< opt_solution=opt_solution+(p[i]*x[i]);
}
cout<<")";
cout<<"\n\nThe optimal solution is\t"<}


void main()
{
int i;
cout<<"\nEnter the truck capacity:\t";
cin>>m;
cout<<"\nEnter the Number of items:\t";
cin>>n;
cout<<"\nEnter weights for "< for(i=0;i {
p_no[i]=i+1;
cout<<"\nProduct "< cin>>w[i];
cout<<"\nProfit:\t";
cin>>p[i];
pwr[i]=(float)p[i]/w[i];
}
display();
for(i=0;i {
for(int j=i+1;j {
if(pwr[i] {
swap(p_no,i,j);
swap(w,i,j);
swap(p,i,j);
swap(pwr,i,j);
}
}
}
display();
knapsack();
cout<<"\n\n";
}


Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/onxRoLFwaSw/13551

 
Start
Prev
1


Page 1 of 3
Taxonomy by Zaragoza Online