Showing posts with label Coding puzzles. Show all posts
Showing posts with label Coding puzzles. Show all posts

Thursday, January 22

C code implementing a simple DFA



Hello freinds, 

Here I am posting my C implementation of DFA shown below. This DFA will accept any string containing 'a's and 'b' and last symbol should be 'a'.


#include <stdio.h>

#define TOTAL_STATES 2
#define FINAL_STATES 1
#define ALPHABET_CHARCTERS 2

#define UNKNOWN_SYMBOL_ERR 0
#define NOT_REACHED_FINAL_STATE 1
#define REACHED_FINAL_STATE 2


enum DFA_STATES{q0,q1};
enum input{a,b};
int Accepted_states[FINAL_STATES]={q1};
char alphabet[ALPHABET_CHARCTERS]={'a','b'};
int Transition_Table[TOTAL_STATES][ALPHABET_CHARCTERS];

int Current_state=q0;
void DefineDFA()
{

    Transition_Table[q0][a] = q1;
    Transition_Table[q0][b] = q0;
    Transition_Table[q1][a] = q1;
    Transition_Table[q1][b] = q0;
}

int DFA(char current_symbol)
{
int i,pos;
    for(pos=0;pos<ALPHABET_CHARCTERS; pos++)
        if(current_symbol==alphabet[pos])   
            break;//stops if any character other than a or b
    if(pos==ALPHABET_CHARCTERS)
         return UNKNOWN_SYMBOL_ERR;
    for(i=0;i<FINAL_STATES;i++)
 if((Current_state=Transition_Table[Current_state][pos])
==Accepted_states[i])
            return REACHED_FINAL_STATE;
    return NOT_REACHED_FINAL_STATE;

}


int main(void)
{

    char current_symbol;
    int result;
 
    DefineDFA();    //Fill transition table
 
    printf("Enter a string with 'a' s and 'b's:\n
                Press Enter Key to stop\n");
 
 
    while((current_symbol=getchar())!= '\n')
        if((result= DFA(current_symbol))==UNKNOWN_SYMBOL_ERR)
            break;
    switch (result) {
    case UNKNOWN_SYMBOL_ERR:printf("Unknown Symbol %c",
  current_symbol); 
 break;
    case NOT_REACHED_FINAL_STATE:printf("Not accepted"); break;
    case REACHED_FINAL_STATE:printf("Accepted");break;
    default: printf("Unknown Error");
    }

    printf("\n\n\n");

    return 0;
}


Thursday, August 29

Trick to Find Cube Root of a number without Calculator








Today I 'm gonna share a simple  trick to find the Cube Root of any number (perfect Cubes only) without the need of any calculator..!! A sample C program is also providing to  prove this trick will work correctly for all perfect cubes.!!
Here We Go..!!  :)








Trick Used:

We will follow these steps to find the cube root of a number:

1. Ignore the last 3 digits of the number. Let remaining number be “Part1”.
2. From the above table check which number’s cube is less than or equal to ‘Part1’. It will be left part of our answer. Let it be “L”.
3. Let Right part of our answer be “R”. It will be determined from the above table. For Ex. If last digit of question is 7, then last digit of our answer will be 3
4. Our answer will be L|R.


Example 1
Suppose we want to find the cube root of 21952.
Step 1: Ignore last three digits of 21952. Remaining number left is ’21′.
Step 2: From the above table cube of ’2′ is less than ’21′. Therefore left part (L) of our answer will be ’2′.
Step 3: Since last digit of ‘21952‘ is ‘2‘. Therefore right part (R) of our answer will be ’8′. (Because Last digit of ‘cube of 8′ is 2. see table above.)
Step 4: Our Answer will be L|R. Therefore Answer will be 2|8 = 28.

PROOF

This C program verifies  this trick that it will work correctly for all perfect cubes.!!

#include<stdio.h>

int main()

{
 int cube[]= {0,1,8,27,64,125, 216,343 ,512 ,729 };
 int last_digit[]={0,1,8,7,4,5,6,3,2,9};
 int i=0,j=0, num, part1,part2, ans1, ans2,flag;


flag =1;
for (i=0 ;i<44;i++)
{
num =i*i*i;                  // eg : 21952
part1 =  num/1000;           // 21
for (j=0;j<10;j++)

if (cube[j]>part1)           // 27>21
 break;                      //j=3
  ans1 =j-1;                 // ans1 =2

part2 = num%10;              //2
for (j=0;j<10;j++)
if(part2 == last_digit[j]) break;

ans2 = j;                    //8

ans1= ans1*10 +ans2;         //28

if (i!=ans1)  flag = 0;

}    
 if (flag) printf ("VERIFIED");
else printf ("ERROR OCCURED");

      return 1;
      }




Hope this post was useful for you.!!
Your feedback and suggestion are always welcome..!! Keep in touch.
hugs :)



Tuesday, June 12

ECIL Graduate Engineer Trainee (GET) recruitment 2012 Computer (CSE) Questions and Answers





Hi Friends,


Here I'm posting the questions asked in ECIL (GET) exam, held on 2012 August 10 , for Computer science, and its detailed solution.
All the questions and answers are collected from memory with the help of  friends..!!!
Hope this will be useful for the preparation for various PSU exams.



Feel free to comment your feedback and doubts ..!!


Here we Go..!! :)


ECIL (GET)
Computer science Stream
Total No Questions: 50
Time Duration: 2 Hrs



(1 )"Bit Stuffing" is used for?
a. Code transparency
b. Synchronization between sender and receiver
c. Data Compression
d. Data Encryption 

Ans: (B) Bit stuffing means inserting a few binary digits to a transmission unit if synchronization pattern is found in data part.The receiver knows how to detect and neglect the stuffed bits.



(2) If inorder traversal of a binary tree is EACKFDBG, what will be its preorder travesal?


(3)Consider the program


main()
{
int x=3, y=7;
modify(x,y,y);
print x;
print y;
}


modify( int x, int y, int z)
{
y= y*2;
z=x+y+z;
}
What will be the output of the program if the arguments are passed to modify through call by reference?

Ans: Since pass by reference is used, After executing,y= y*2; actual values will be x=3, y=14
After this step z=x+y+z; x=3 and y=31
so 3 31 will be printed.




(4) Calculate the Hamming Distance between 010 and 001:
a. 0
b. 1
c. 2
d. 3
Ans:c The Hamming Distance denotes the difference between two binary strings. In the given question difference can be found in 2 positions only. Hence 2 is the hamming distance.



(5)Which of the following is the correct syntax of destructor?
a. x()~
b.x~()
c. x(~)
d.~x()
Ans: (d) ~x() the correct syntax of destructor



(6) How many times the "for" loop in given code fragment will be executed?


main()
{
int i=5;
for( ; scanf("%s", &i) ; printf( "%d", i ));
}


a. for loop never will be executed.
b. for loop will be executed only once.
c. for loop will be executed 5 times
d. for loop will be executed infinite times.

Ans:(d) for loop will be executed infinite times.





(7)Which among the following can input / delete data from both ends , not from the middle?
a. stack
b. linked list
c. queue
d. dequeue

Ans
(d). dequeue





(8)Which among the following involved full code?
a. Black box Test
b. White box Test
c. Integration Test
d. Performance Test

Ans B



(9)What is aliasing?
a. Giving same name to different type of variables.
b. Giving same name for same type of variables.
c. Giving different names for same type of variables
d. Giving different names for same memory location.

(d). Giving different names for same memory location.



(10)float x=4.204 , y=2.103
We can calculate x mod y as:


a. x%y
b. modf(x,y);
c. fmod (x,y);
d. we cannot calculate modulus of two float values in C

Ans C



(11)The type of class accessing primitive data types as objects is called?
a. storage
b. friend
c. virtual
d. wrapper

Ans: D

(12)Which pin is used in 8086 to achieve Mutual exclusion in critical section ?
a. HOLD
b. WAIT
c. RQ/GT
d. LOCK

Ans: d



(13)Which the following operator is used to turn off a particular bit in a byte?
a. ~
b. &&
c. &
d. !

Ans: c

(14) Which among the following correctly explains behavior of a stack?
a. Last element is removed first.
b. Access middle element first
c. Variables can be inserted in any order.
d. variables can be deleted in any order.

Ans: a



(15)Which of the following protocol is used for secure data transfer?
a. HTTP
b. SNMP
c. HTTPS
d. TCP

ans : C



(16) Which of the following is NOT related with inter process communication?
a. pipe
b. semaphore
c. stack
d. mailbox

Ans:C

(17)What is the size of "long double"?
Ans: 10 bytes



(18)What will be the value of FFFF it is a signed integer and is represented by sign magnitude format ?
a. 655536
b. -1
c. -32267
d. 32267

In sign magnitude format the first bit represents sign and it is 1; number is negative. Other bits represent magnitude and its value is 2^15-1. So the answer is -(2^15-1)= -32767



(19) What is the meaning of int (*ptr)[10]
a. pointer to an array of 10 integers.
b. pointer to 10 integer variables
c. array of 10 integer pointers.
d. Array of 10 integers.

Ans :C

(20) In Go - Back N Error control mechanism , after sending N frames, at most how many acknowledgements can be send by the receiver?
a. 0
b. 1
c. N
d. N-1

Ans: C



(21)In a balanced binary tree of N nodes , the difference between left subtree and right subtree will be at most:


a.0
b. 1
c. 2
d. N
Ans: B



(22) Predict the output


main()
{
enum example={ "first", " bin", "akt"};
enum example s1, s2 ,s3;
s1= example.first;
s2= example.akt;
s3= example.bin;
printf("%d %d %d", s1, s2 ,s3);
}


a. 012
b.210
c.021
d.120


Ans: C


(23)Which of the following is NOT true about RS 485?


(24)Which of the following represented by the term Base FX?
a. Thin wire Ethernet
b. Thick wire Ethernet
c. Twisted pair Ethernet
d. Optical Fiber
Ans: D

(25)Which of the following is NOT related with Transport Layer?
a. error control
b. flow control
c. routing


(26)If in a given cache system, 99.99% is the hit ratio ,and the processing time for hit and miss events are 1 microseconds amd 1 milliseconds respectively, the calculate the average processing time?




(27)If 192.97.28.91 and 192.97.28.131 are connected in same network, then which of the following cannot be used for subnet masking?


a. 255.255.255.0
b. 255.255.255.192
c. 255.255.255.126
d. 255.255.255. 224


(28) Which of the following is the property of bipartite graphs of N nodes?
a. There will be N edges
b. There will be only one odd cycle.
c. There will be No odd cycle
d. There will be N^2 Edges.


(29)Which among the following is the statement for dynamic allocation of characters?
Ans: char *p= (char *) malloc[20];


(30) Float to integer conversion is perfectly done in which statement?
float x;
Ans : int i=(int)(x+0.5)






(31)Which is true about "isotopes"?
a. Number of protons is equal
b. Number of neutrons is equal
c. Number of protons is different
d. Number of both protons and neutrons are equal.




(32)Which operator is used to declare member functions outside the class body?
Ans ::


(33)The member elements can be accessed by the pointer variable of a structure using the operator?
Ans: ->


(34)In UML notation which diagram is used to depict the interaction between software and hardware?
Ans: Deployment Diagram




(35)In 8085, ALE signal is used. but it is not is 6086. Why?
a. In 8085 multiplexing is used for communication but not in 6086
b. In 8085 only IO mapped IO is used whereas in 6086 only memory mapped IO is used.


Ans a


(36)If the given sampling frequency is 10 MHz , then for successful transmission bandwidth should be?
a. 5 MHz
b. 10 MHz
c. 20 MHz
d. 100 M Hz

Ans (a)The Nyquist–Shannon sampling theorem states that perfect reconstruction of a signal is possible when the sampling frequency is greater than twice the maximum frequency of the signal being sampled.

Bandwidth = sampling frequency /2= 10/2= 5 MHz



(37)What is the use of "segmentation" in a computer system?


(38) In CSMA/ CD, vulnerable time may be:
a. Round trip time.
b. Time to transmit one complete frame
c. Time to transmit two complete frames
d.Time to reach at the destination.




(39)Which among the following is Non - Linear?
a. Low pass filter
b. Rectifier.
c. High pass filter
d. All the above

Ans: d) A non linear circuit does not have a linear relationship between current and voltage



(40)What is the purpose of Rectifier?
a. To convert DC to AC
b. To convert AC to DC
c. To eliminate Noise.
d. To eliminate DC component.


Ans :B

(41)Associative memory mapping is free from
a. conflict collision
b. capacity collision

Ans :a

 

(42) Address length of IPV4 and IPV6?
Ans: 4 bytes, 16 bytes




(43) In selection sort, after 42 successful iteration of main loop , at least how many elements will be sorted?


a. 41
b. 42
c. 21
d. 43

Ans : b

(44)which one of the following is a hashing algorithm?
a)SHA
b)AES
c)
DES


Ans :A

(45). Aggregate function to count number of rows?

(46). Diagrams to represent Relational database?


(47) what is crc in error control mechanism? 


a) dividend 
b) remainder
c) divisor 
d) quotient 

Ans : b



All the questions were collected from memory.


If you know any other questions, feel free to comment it here..!!!! :)