Wednesday, December 30, 2009

Insert Ignore and Replace in MySql


Source

Use INSERT IGNORE rather than INSERT. If a record doesn't duplicate an existing record, MySQL inserts it as usual. If the record is a duplicate, the IGNORE keyword tells MySQL to discard it silently without generating an error.

Following example does not error out and same time it will not insert duplicate records.

mysql> INSERT IGNORE INTO person_tbl (last_name, first_name)
-> VALUES( 'Jay', 'Thomas');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT IGNORE INTO person_tbl (last_name, first_name)
-> VALUES( 'Jay', 'Thomas');
Query OK, 0 rows affected (0.00 sec)

Use REPLACE rather than INSERT. If the record is new, it's inserted just as with INSERT. If it's a duplicate, the new record replaces the old one:

mysql> REPLACE INTO person_tbl (last_name, first_name)
-> VALUES( 'Ajay', 'Kumar');
Query OK, 1 row affected (0.00 sec)
mysql> REPLACE INTO person_tbl (last_name, first_name)
-> VALUES( 'Ajay', 'Kumar');
Query OK, 2 rows affected (0.00 sec)

INSERT IGNORE and REPLACE should be chosen according to the duplicate-handling behavior you want to effect. INSERT IGNORE keeps the first of a set of duplicated records and discards the rest. REPLACE keeps the last of a set of duplicates and erase out any earlier ones.

Monday, December 28, 2009

mysql command prompt paging options

mysql --pager=more
mysql --pager=less

or
Source

mysql command prompt auto completion

mysql>\#

Wednesday, December 16, 2009

Oracle trigger question

create or replace trigger trig1
before insert on emp1
for each row
begin
insert into emp1(empid) values (seq1.nextval);
end;
OR
create or replace trigger trig2
before insert on emp1
for each row
begin
select seq1.nextval into :new.empid from dual;
end;
/

Answer : Second one.
First one will result in this :
SQL> insert into emp1 (empid) values (1);
insert into emp1 (empid) values (1)
*
ERROR at line 1:
ORA-00036: maximum number of recursive SQL levels (50) exceeded
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512: at "SCOTT.TRIG1", line 2
ORA-04088: error during execution of trigger 'SCOTT.TRIG1'
ORA-06512

Tuesday, December 15, 2009

Finding whether a number is blessed or not

Problem

1,2,3,4,5,6,....

initialize
Jump=2
then 2,4,6,8,.......

remaining 1,3,5,7,9,11,.......

Jump=3
then 5,11,..........gets removed

remaining: 1,3,7,9,13,15

We carry on for jump infinitely here 1.3 are blessed as they will not be removed. Now, given a number n propose a algorithm to find out whether it is blessed number or not.

Solution :
#include
int counter = 2;
int isBlessed(int k)
{
//printf("k = %d counter = %d\n",k,counter);
if(k< counter) return 1;

if(k%counter == 0)
{
return 0;//it's not blessed
}
else
{
k = k - k/counter;
++counter;
return isBlessed(k);
}
}


int main()
{
int i;
for(i=0;i<25;i++)
{
counter = 2;
if(isBlessed(i))
printf("%d is blessed\n",i);
else
printf("%d is not blessed\n",i);
}
}

Monday, December 14, 2009

Generating readable assembly with gcc

Lifted from here.

>
gcc -g -c test.c
> objdump -d -M intel -S test.o

test
.o: file format elf32-i386


Disassembly of section .text:

00000000
:
#include

int main(void)
{
0: 55 push ebp
1: 89 e5 mov ebp,esp
3: 83 e4 f0 and esp,0xfffffff0
6: 83 ec 10 sub esp,0x10
puts
("test");
9: c7 04 24 00 00 00 00 mov DWORD PTR [esp],0x0
10: e8 fc ff ff ff call 11 <main+0x11>

return 0;
15: b8 00 00 00 00 mov eax,0x0
}
1a: c9 leave
1b: c3 ret

Brace matching : Flex Builder

Source
A feature forever undocumented in Flex Builder is the ability to jump directly to matching braces. To use it, simply place your cursor to the left on an { or the right of an } and press Ctrl/Cmd+shift+P. For the JDT nerds out there, it’s the same key command. Enjoy!

Sunday, December 13, 2009

Finding which place belongs to which district using Ruby

I have a csv file which contains a list of locations with latitude and longitude.
I have a kml file which contains names of districts and the co-ordinates
of their boundary.
I have to find out which location belongs to which district using ruby.
Here is the ruby code, kml(6.33MB) and csv(4.05 MB) file.

Saturday, December 12, 2009

Iterative/Non-recursive version of pre order/in order traversal of a binary tree

struct node
{
int data;
int data1;
int data2;
struct node* left;
struct node* right;
};
struct node** stack;
int curr = -1;
void push(struct node* node)
{
++curr;
stack[curr] = node;
}

struct node* pop()
{
return stack[curr--];
}
void visit(struct node* curr)
{
printf("%d ",curr->data);
}

void preOrderiter(struct node* root)
{
printf("*************\n");
struct node* tmp = root;
while(curr != -1 || tmp)
{
while(tmp)
{
push(tmp);
visit(tmp);
tmp = tmp->left;
}
tmp = pop();
tmp = tmp->right;
}
printf("*************\n");
}


void inOrderiter(struct node* root)
{
printf("*************\n");
struct node* tmp = root;
while(curr != -1 || tmp)
{
while(tmp)
{
push(tmp);
tmp = tmp->left;
}
tmp = pop();
visit(tmp);
/*if(!tmp->right)
{
printStack();
}
else*/
tmp = tmp->right;
}
printf("*************\n");
}
int main()
{
stack = malloc(10*sizeof(struct node*));
}

Wednesday, December 9, 2009

C Dictionary

#include

struct node
{
char data;
struct node* next;
int numChildren;
char isTerminal;
};

int main()
{
struct node* aroot = malloc(sizeof(struct node));
struct node* tmp;
int i = 0;
aroot->data = 'a';
aroot->isTerminal = 1;
aroot->numChildren = 0;
aroot->next = malloc(sizeof(struct node));
++(aroot->numChildren);
aroot->next->data = 'n';
aroot->next->isTerminal = 1;
aroot->next->numChildren = 0;
tmp = (struct node*) realloc(aroot->next,aroot->numChildren + 1);
if(tmp)
aroot->next = tmp;
++(aroot->numChildren);
(aroot->next[1]).data = 'm';
(aroot->next[1]).isTerminal = 0;
//printf("%c\n",(aroot->next[0]).data);
//printf("%c\n",(aroot->next[1]).data);
for(i=0;inumChildren;i++)
{
//if(aroot->next[i].isTerminal)
printf("%c\n",(aroot->next[i]).data);
}
}

A whiteboard built using Flex/PHP/MySql

Here.

Maximum contiguous subarray problem

View and Edit here.
Copied verbatim from here.

Sunday, December 6, 2009

Grep for space and new line

I had to grep the following pattern followed by a new line :
"belongs to "
so it is
grep belongs[[:space:]]to[[:space:]]*$

I had to use '*' as I guess there are more characters than
space at the end.

Friday, December 4, 2009

Quick Sort in C by Kernighan Ritchie

void pArr(int v[],int left,int right)
{
int i;
for(i=left;i<=right;i++) printf("%d ",v[i]); printf("\n"); } void swap(int v[],int i,int j) { int tmp; tmp= v[i]; v[i] = v[j]; v[j] = tmp; } void qsort(int v[],int left,int right) { int i,last; pArr(v,left,right); if (left >= right)
return;
swap(v,left,(left+right)/2);
last = left;
for(i = left + 1;i<=right; i++)
if(v[i] < v[left])
swap(v,++last,i);
swap(v,left,last);
qsort(v,left,last - 1);
qsort(v,last + 1,right);
}

int main()
{

int a[8] = {1,5,8,11,3,4,2,3};
pArr(a,0,7);
qsort(a,0,7);
pArr(a,0,7);
}

A simple hash implementation in C

#include
#include

struct node{
int key;
char *value;
struct node* next;
};

struct node* hash[5];

char* m_strdup(char* tmp)
{
int len = strlen(tmp);
char* newmem = malloc(sizeof(char)*len);
if(!newmem)
{
fprintf(stderr,"problem in memory allocation");
exit(0);
}
strcpy(newmem,tmp);
return newmem;
}

void traverse(struct node* root)
{
printf("traversing\n");
while(root)
{
printf("%s",root->value);
root = root->next;
}
printf("\n");
}
void addnew(int value)
{
int key = value%5;
char tmp[10];
int counter = 0;
tmp[0] = 'a' + value;
tmp[1] = 0;
struct node* curr;
if(!hash[key])
{
hash[key] = malloc(sizeof(struct node));
hash[key]->value = m_strdup(tmp);
hash[key]->next = NULL;
printf("added at %d - %d\n",key,counter);
}
else
{
curr = hash[key];
while(hash[key]->next)
{
hash[key] = hash[key]->next;
counter++;
}
hash[key]->next = malloc(sizeof(struct node));
hash[key]->next->value = m_strdup(tmp);
hash[key]->next->next = NULL;
hash[key] = curr;
printf("added at %d - %d\n",key,counter);
}
}

void lookup(int value)
{
int key = value%5;
int counter = 0;
char tmp[10];
tmp[0] = 'a' + value;
tmp[1] = 0;
struct node* curr = hash[key];
while(curr)
{
if(strcmp(curr->value,tmp)==0)
{
printf("found at %d - %d\n",key,counter);
break;
}
curr = curr->next;
counter++;
}
//hash[key] = curr;
}

int main()
{
int i;
for (i=0;i<5;i++)
hash[i] = NULL;
addnew(1);
addnew(2);
addnew(22);
addnew(12);
addnew(3);
addnew(13);
traverse(hash[3]);
lookup(1);
lookup(2);
lookup(3);
lookup(22);
lookup(12);
lookup(13);
}

Friday, November 27, 2009

Oracle trigger if inserting or updating

create trigger ins_or_upd before insert or update of salary on newemp
for each row
begin
if inserting then
insert into emp_audit values (:new.salary);
else
insert into emp_audit values(:old.salary);
end if;
end;
/

Oracle difference between before and after triggers

Try to compile 1. and 2. to see the difference:

1.
create or replace trigger upd_trigger
before update of salary on newemp
for each row
begin
if :new.salary < :old.salary then
:new.salary := 1000;
end if;
end;
/

2.

create or replace trigger upd_trigger
after update of salary on newemp
for each row
begin
if :new.salary < :old.salary then
:new.salary := 1000;
end if;
end;
/

However when you compile 3. and 4. you
will see no difference. And both these triggers
will have the same effect, i.e. they won't allow
the corresponding updates to take place.

Also, in 3. and 4. even if a single row has
its new salary < old salary, none of
the updates will be executed.


3.
create or replace trigger upd_trigger
before update of salary on newemp
for each row
begin
if :new.salary < :old.salary then
raise_application_error(-20000,'not allowed');
end if;
end;
/

4.

create or replace trigger upd_trigger
after update of salary on newemp
for each row
begin
if :new.salary < :old.salary then
raise_application_error(-20000,'not allowed');
end if;
end;
/

Thursday, November 26, 2009

Oracle creating/removing new user

create user scott1 identified by tiger;
grant create session to scott1;
grant create table to scott1;
grant unlimited tablespace to scott1;

Drop user scott1 casecade;

Copying a table from another user's account :
create table booking as select * from scott.booking;

Wednesday, November 25, 2009

Sample oracle query

Table -> innings_runs (id Number, runs Number)

What to do : Select player ids who have made more than
100 runs without ever making 50 runs in a single innings.

ID RUNS
---------- ----------
1 48
1 48
1 48
2 50
2 51


select distinct(id) from innings_runs
where id in
(select id from (select id,sum(runs) as sum1 from innings_runs group by id having(sum(runs) > 100)))
and runs < 50
;

Monday, November 16, 2009

Cursors In Oracle

declare
ans char(1);
begin
insert into newemp (empid,empname,job) values (109,'xyz','hr');
savepoint one_new_rec_ins;
update newemp set empname = 'abc' where empid = 109;
savepoint one_new_rec_upd;
delete from newemp where empid = 109;
ans := '&complete_rollback';
if ans = 'y' then
rollback to one_new_rec_ins;
elsif ans = 'n' then
rollback to one_new_rec_upd;
else
commit work;
end if;
end;

Parametrized Cursors

declare
cursor get_empl(salamt Number) is
select * from newemp where
salary > salamt and job <> 'President';
emprec newemp%rowtype;
begin
open get_empl(&amount);
dbms_output.put_line('records fetched are');
loop
fetch get_empl into emprec;
exit when get_empl%notfound;
dbms_output.put_line(emprec.empid||' '||emprec.empname||' '||emprec.job||' '||emprec.salary);
if emprec.job = 'manager'
then
update newemp set salary = salary + 500 where empid = emprec.empid;
elsif emprec.job = 'accountant' then
update newemp set salary = salary + 750 where empid = emprec.empid;
end if;
end loop;
close get_empl;
end;

Sunday, June 28, 2009

Drupal : HTTP request status fails

HTTP request status Fails
Your system or network configuration does not allow Drupal to access web pages, resulting in reduced functionality. This could be due to your webserver configuration or PHP settings, and should be resolved in order to download information about available updates, fetch aggregator feeds, sign in via OpenID, or use other network-dependent services.


go to: C:\Windows\System32\drivers\etc
and open "hosts"

put an # in front of ::1

This did it for me.
I use Windows Vista.

Sunday, April 19, 2009

Test solutions :

Test solutions :

Tables :
teachers(teacher_id,teacher_name)
courses(course_id,teacher_id,num_of_students)
students(roll_no,stu_name)
stu_courses(roll_no,course_id)

(a) Teachers not teaching any courses :
Select teacher_name from teachers where teacher_id not in (select distinct(teacher_id) from courses);
OR
Select teacher_name from teachers left join courses ON(teachers.teacher_id = courses.teacher_id) where course_id is NULL;
 
(b) Students not taking any courses :
Similar to part (a).

(c) Teachers taking maximum courses :
select a.teacher_name as 'Teacher', a.teacher_id as 'Teacher Id', count(b.course_id) as 'Num of Courses' 
from teachers a
inner join courses b on a
.teacher_id = b.teacher_id
group by b.teacher_id
order
by count(b.course_id) desc
limit
1
OR
mysql> select teachers.teacher_name,tmp1.teacher_id,tmp1.cnt from (select max(tm
p.cnt) as tmpMax from (select teacher_id,count(teacher_id) as cnt from courses g
roup by teacher_id) as tmp) as tmp2,(select teacher_id,count(teacher_id) as cnt
from courses group by teacher_id) as tmp1,teachers where tmp1.cnt = tmp2.tmpMax
and teachers.teacher_id = tmp1.teacher_id;

(d) Students taking maximum courses :
similar to part (c)

(e) names of teachers teaching maximum number of students :

mysql> select max(tmp1.sum1) from (select teacher_id,sum(num_students) as sum1 f
rom courses group by teacher_id) as tmp1;

Sunday, April 5, 2009

Normalization

Misc

1. Vim : Decrement number : Ctrl-X

2. Flex datagrid automatic sort :
someDataGrid.dispatchEvent
(
new DataGridEvent
(
DataGridEvent.HEADER_RELEASE,
false,
true,
0, // The zero-based index of the column to sort in the DataGrid object's columns array.
null,
0,
null,
null,
0
)
);
3. Vim - Adding text before or after visually selected block(rectangle) :

Suppose you have selected a rectangle (using Ctrl-v), you can insert text in front of it by typing I (switch to insert mode) and inserting your text. As soon as you leave the insert mode, the text will be added to all the other selected lines. Use A to enter text after the selection.

Thursday, April 2, 2009

Ejecting the CD Drive with PHP

Soruce


<?php

//create an instance of Windows Media Player

$mp = new COM("WMPlayer.OCX");

//ejects the first cd-rom on the drive list

$mp->cdromcollection->item(0)->eject();



?>

Saturday, March 28, 2009

inserting a billion numbers in a mysql table

mysql.exe -u root -p < proc.sql

proc.sql contains :

use mbm_db;

delimiter //

create procedure sp4()

begin

declare i int;

set i = 0;

while(i < 1000000000) do
set i = i+1;
insert into test_table values(i);
end while;
end;
//

delimiter ;
call sp4();

Sunday, March 15, 2009

Merging two files line by line on unix/linux

Source
for eample
file1:
2 3 0
6 2 0
2 3 1
5 3 1
6 9 1file2
2 2 2
2 2 2
paste -d " " file1 file2 >"outputfile1"
outputfile1
2 3 0 2 2 2
6 2 0 2 2 2
2 3 1
5 3 1
6 9 1

Wednesday, January 28, 2009

Wipeer set as default page for new tabs in firefox

Recently I installed Wipeer, but for no use, as I had to uninstall it soon.
But it set a particular search page as default on Firefox.
To remove that I had to delete a folder named "extensions" from
my firefox profile.
Here is how to locate your firefox profile.

Saturday, January 24, 2009

Some useful vim commands

qa
do stuff(my favorite : copy paste a number and increment with Ctrl-A
qa
@a to repeat the stuff
-------
* for searching words under cursor
--------
shift i for inserting at the beginning of the line
------
Ctrl-v to select a rectangular block
--------
but It's All Text!

Wednesday, January 14, 2009

Oracle 10g username password

While installing Oracle 10g I gave orcl orcl as uname password for something.
After installation, and hell lot of effort, figured out that, for running sqlplus
username : system
password : orcl
host string :
oh yeah, don't give anything in the host string field.

Sunday, January 11, 2009

flex sorting a datagrid column numerically rather than alphabetically

http://flextricks.wordpress.com/2007/02/03/sorting-numeric-values-in-a-flex-datagrid/

flex datagrid restricting a column to have numeric values

http://weblogs.macromedia.com/pent/archives/2008/05/index.html

flex datagrid with total max average etc

http://seanhess.user.openhosting.com/demos/footerdg/Test.html
extends the work of
http://blogs.adobe.com/aharui/FancyBorders/dg.swf

Inverse ,determinant ,adjoint for NxN matrix

Source URL


With slight modification. Number of
rows will be input by user and adjoint
will also be printed out.


#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#include<errno.h>
#include <iostream.h>

float determinant(float **matrix,
int size,
int * used_rows,
int * used_cols,
int depth);
float coefficient(float **matrix,int size, int row, int col);
void print_matrix(FILE * fptr,float ** mat,int rows, int cols);
float test_data[8][8] = {
{4.0, 2.0, 4.0, 5.0, 4.0, -2.0, 4.0, 5.0},
{4.0, 2.0, 4.0, 5.0, 3.0, 9.0, 12.0, 1.0 },
{3.0, 9.0, -13.0, 15.0, 3.0, 9.0, 12.0, 15.0},
{3.0, 9.0, 12.0, 15.0, 4.0, 2.0, 7.0, 5.0 },
{2.0, 4.0, -11.0, 10.0, 2.0, 4.0, 11.0, 10.0 },
{2.0, 4.0, 11.0, 10.0, 3.0, -5.0, 12.0, 15.0 },
{1.0, -2.0, 4.0, 10.0, 3.0, 9.0, -12.0, 15.0 } ,
{1.0, 2.0, 4.0, 10.0, 2.0, -4.0, -11.0, 10.0 } ,
};
#define ROWS 8
void print_adjoint(FILE* fptr,float ** mat,int rows,int cols,float det)
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
fprintf(fptr,"%10.4f ",mat[i][j]*det);
}
fprintf(fptr,"\n");
}
fflush(fptr);

}

int main(int argc, char **argv)
{

float **matrix;
float **inverse;
int rows,i,j;
float determ;
int * used_rows, * used_cols;

//rows = ROWS;
cout << "Input number of rows" << endl;
cin >> rows;
cout << "Now enter the input values" << endl;
/* Allocate markers to record rows and columns to be skipped */

/* during determinant calculation */
used_rows = (int *) malloc(rows*sizeof(*used_rows));
used_cols = (int *) malloc(rows*sizeof(*used_cols));

/* Allocate working copy of matrix and initialize it from static copy */
matrix = (float **) malloc(rows*sizeof(*matrix));
inverse = (float **) malloc(rows*sizeof(*inverse));
for(i=0;i<rows;i++)
{
matrix[i] = (float *) malloc(rows*sizeof(**matrix));
inverse[i] = (float *) malloc(rows*sizeof(**inverse));
for(j=0;j<rows;j++)
//matrix[i][j] = test_data[i][j];
cin >> matrix[i][j];
}

/* Compute and print determinant */
printf("The determinant of\n\n");
print_matrix(stdout,matrix,rows,rows);
determ=determinant(matrix,rows,used_rows,used_cols,0);
printf("\nis %f\n",determ);
fflush(stdout);
assert(determ!=0);

for(i=0;i<rows;i++)
{
for(j=0;j<rows;j++)
{
inverse[j][i] = coefficient(matrix,rows,i,j)/determ;
}
}

printf("The inverse is\n\n");
print_matrix(stdout,inverse,rows,rows);
printf("\n\nThe adjoint is\n\n");
print_adjoint(stdout,inverse,rows,rows,determ);

return 0;
}

float determinant(float **matrix,int size, int * used_rows, int * used_cols, int depth)
{
int col1, col2, row1, row2;
int j,k;
float total=0;
int sign = 1;

/* Find the first unused row */
for(row1=0;row1<size;row1++)
{
for(k=0;k<depth;k++)
{
if(row1==used_rows[k]) break;
}
if(k>=depth) /* this row is not used */
break;
}
assert(row1<size);

if(depth==(size-2))
{
/* There are only 2 unused rows/columns left */

/* Find the second unused row */
for(row2=row1+1;row2<size;row2++)
{
for(k=0;k<depth;k++)
{
if(row2==used_rows[k]) break;
}
if(k>=depth) /* this row is not used */
break;
}
assert(row2<size);

/* Find the first unused column */
for(col1=0;col1<size;col1++)
{
for(k=0;k<depth;k++)
{
if(col1==used_cols[k]) break;
}
if(k>=depth) /* this column is not used */
break;
}
assert(col1<size);

/* Find the second unused column */
for(col2=col1+1;col2<size;col2++)
{
for(k=0;k<depth;k++)
{
if(col2==used_cols[k]) break;
}
if(k>=depth) /* this column is not used */
break;
}
assert(col2<size);

/* Determinant = m11*m22-m12*m21 */
return matrix[row1][col1]*matrix[row2][col2]-matrix[row2][col1]*matrix[row1][col2];
}

/* There are more than 2 rows/columns in the matrix being processed */
/* Compute the determinant as the sum of the product of each element */
/* in the first row and the determinant of the matrix with its row */
/* and column removed */
total = 0;

used_rows[depth] = row1;
for(col1=0;col1<size;col1++)
{
for(k=0;k<depth;k++)
{
if(col1==used_cols[k]) break;
}
if(k<depth) /* This column is used - skip it */
continue;
used_cols[depth] = col1;
total += sign*matrix[row1][col1]*determinant(matrix,size,used_rows,used_cols,depth+1);
sign=(sign==1)?-1:1;
}
return total;

}

void print_matrix(FILE * fptr,float ** mat,int rows, int cols)
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
fprintf(fptr,"%10.4f ",mat[i][j]);
}
fprintf(fptr,"\n");
}
fflush(fptr);
}


float coefficient(float **matrix,int size, int row, int col)
{
float coef;
int * ur, *uc;

ur = (int*)malloc(size*sizeof(matrix));
uc = (int*)malloc(size*sizeof(matrix));
ur[0]=row;
uc[0]=col;
coef = (((row+col)%2)?-1:1)*determinant(matrix,size,ur,uc,1);
return coef;
}

Thursday, January 8, 2009

Bunch tag

I don't know if this feature is already available with
the current DAM vendors, but it would be nice to tag
a bunch(may be hundreds or thousands) of images, movie
clips with a single tag simultaneously.

C code for selection Sort

#include <iostream.h>



void printArray(int a[])

{

 int k;

 for(k=0;k<10;k++)

         cout << a[k] << " ";

 cout << endl;

}



int main()

{

        int a[10] = {1,3,2,4,8,7,5,10,9,6};

        int i,j;

        printArray(a);

        int min,pos;

        for(i=0;i<9;i++)

        {

                min = a[i];

                pos = i;

                for(j=i;j<9;j++)

                {

                  if(a[j] < min)

                  {

                     min = a[j];

                     pos = j;

                  }      

                }

                int t = a[i];

                a[i] = a[pos];

                a[pos] = t;

        }





printArray(a);



}

C code for insertion sort

#include <iostream.h>





void insert(int a[],int i,int j)

{



        //function for inserting ith element at jth position

        int t = a[i];

        int k;

        for (k=i;k>j;k--)

                a[k] = a[k-1];

        a[j] = t;

}



void printArray(int a[])

{

 int k;

 for(k=0;k<10;k++)

         cout << a[k] << " ";

 cout << endl;

}



int main()

{

        int a[10] = {1,3,2,4,8,7,5,10,9,6};

        int i;

        printArray(a);

        for(i=0;i<9;i++)

        {

                if(a[i] > a[i+1])

                {

                        cout << "conflict here" << a[i] << ">" << a[i+1] << endl;

                        //conflict exists

                        //find the appropriate position for i+1 th element

                        int j,pos;

                        for(j=0;j<i+1;j++)

                        {

                           if(a[j] > a[i+1])

                           {

                                   pos = j;

                                   insert(a,i+1,pos);

                                   printArray(a);

                                   break;

                           }

                        }

                                

                }

        }



printArray(a);



}

C code for bubble sort

#include <iostream.h>



int main()

{

        int a[6] = {5,19,11,18,27,22};

        int j;

        for(j=4;j>=0;j--)

                for(int i=0;i<=j;i++)

                {

                        if(a[i] < a[i+1])

                        {

                                int tmp = a[i];

                                a[i] = a[i+1];

                                a[i+1] = tmp;

                        }

                }

        for(j=0;j<6;j++)

                cout << a[j] << " " ;

        cout << endl;

}

Blog Archive