Tuesday, August 25, 2009

C and C++ Questions

This is a program to convert a Roman letter to its Decimal Equivalent :

#include
#include

#define maxnumber 10

int value[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
char Roman[][3] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};

//CCCLXIX=369

int Find(char key[]) {
int i;
for(i = 12; i>=0; i--) {
if(!strcmp(Roman[i],key)) return value[i];
}
return 0;
}
int Convert(char roman[]) {
int length = strlen(roman) - 1;
int i, val, sum = 0, find;
char tempRoman[4];

for(i = length; i>=0; ) {
find = 0;
if(i >= 1) {
tempRoman[0] = roman[i-1];
tempRoman[1] = roman[i];
tempRoman[2] = NULL;
val = Find(tempRoman);
if(val > 0) {
sum += val;
find = 1;
i -= 2;
}
}
if(find == 0) {
tempRoman[0] = roman[i];
tempRoman[1] = NULL;
sum += Find(tempRoman);
i--;
}
}
return sum;
}

int main() {
char roman[100];
// Insert a valid Roman number, all characters must be upper case
// Input range: up to 3999 ( MMMCMXCIX )

while(scanf("%s",roman) != EOF) {
printf("%d\n",Convert(roman));
}
return 0;
}


Basic Knowledge needed to understand this program :

1. U must know C ofcourse .
2. U must Also know how are decimal numbers represented using Roman letters .

How are they represented :

I : This represents 1 and Two I's represent 2 and three I's represent 3 . But this sequence cant be continued for representing four , five etc as it would take more I's . This Gives birth to

v : This represents 5 . placing an I in front of V means 4 . That is IV means 4 . Placing a smaller number in front of larger number indicates Subtraction and placing a smaller number after a larger number indicates Addition . Thus VI means 6[5+1] , VII means 7 , and VIII means 8 .

X : This represents 10 . Hope you found out how to represent 9 . It's IX ,as explainied before subtract I from X . The number of X's indicates the number of tens . Lets take XXIV . As there are 2 X's it indicates 20 and IV is 4. so its 24.

L : L means 50 . Based on what we've learnt so far it should be easy by now to predict how 40 will be represented . ya you are right . Its XL . 10(X) subtracted from 50(L) . And 60,70,80 are LX,LXX,LXXX .

C : C stands for Centum . It means 100 . So by previous explanations XC means 90. CCCLXIX means 369.

D : D stands for 500. Now you will be able to know how to represent 400 . Its simple CD.

M : M means 1000. This is mostly used to represent dates .

Thursday, August 20, 2009

Database questions

Which system tables contain information about privileges granted and privileges obtained ?

USER_TAB_PRIVS_MADE, USER_TAB_PRIVS_RECD are the tables which contains the above asked information .

What command is used to create a table by copying the structure of another table ?

CREATE TABLE .. AS SELECT command
Explanation:
To copy only the structure, the WHERE clause of the SELECT command should contain a FALSE statement as in the following.

CREATE TABLE NEWTABLE AS SELECT * FROM EXISTINGTABLE WHERE 1=2;
If the WHERE condition is true, then all the rows or rows satisfying the condition will be copied to
the new table.

Difference between stored procedure and Trigger :

Procedures can be called explicitely where as Triggers are automatically invoked when the action defined in Trigger is executed.

Triggers can also be enabled and Disabled.

Information related to stored procedures can be seen in USER_SOURCE and USER_OBJECTS(Current User) tables.
Where As
Information related to Triggers can be found in USER_SOURCE AND USER_TRIGGER(Current User) tables.






Datebase Questions

What Is The Use Of CASCADE CONSTRAINT ?

cascading is used for maintaining referencial integrity rules, which says that foreign key attributes values should be either subset of primary key values or null.

with cascading when parent is deleted child could be deleted/set null/set default according to cascade type.All this is for maintaing integrity.

The cascade option of the drop clause causes any dependent constraints also to be dropped.
When this clause is used with the DROP command, a parent table can be dropped even when a child table exists.

What is the difference between TRUNCATE and DELETE command ?

TRUNCATE is a DDL command whereas DELETE is a DML command. Hence DELETE operation can be rolled back, but TRUNCATE operation cannot be rolled back.

WHERE clause can be used with DELETE and not with TRUNCATE.

TRUNCATE resets the identity and DELETE does not .

If we use TRUNCATE command it will delete the physical structure of the table and retains the logical structure of same as for the next usage.
DELETE is used to delete a particular row from the table.

when we truncate a table then no trigger fired with associate table but in delete sts the trigger will be fire.

Truncate is fast and delete is not because delete have undo information .