Showing posts with label C PROGRAMS. Show all posts
Showing posts with label C PROGRAMS. 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;
}


Monday, July 16

EIL (Engineers India Ltd) Exam questions and answer key

Hi reader,

Here I'm posing some questions asked and its solution / key in EIL Management
Trainee recruitment test in July 2012.

All the information is collected from my freinds..!!!

The Test comprises of 150 questions eacxh carrying 1 mark . for each wrong answer 0.25 marks will be deducted.
Technical section(100 qustions)


General Awareness
Aptitude questions

English.(Non technical 50 questions)



General awareness section had some questions based on current affais , especiall on
April 2012( note Exam held on July 2012)
Sample:
* Name of the British Citizn caputred by Maoists recently.
* Richter scale reading of the drastic earthquake hit in Sumara islends recently

 ( 8.7, 8, 7.1, None of these)
*Name of the satellite successfully launched in April 2012
* Name the Indian company successfully dveloped medicine for meningitis.

* During which period first five year plan was established?(1952-56)
* Indiragandhi award was bagged this year by?


..and some more...



English section comprised of this pattern of questions
* Spot the error.
* Pick the synonym
*Reading comprehension




Aptitude section contained questions from
*Profit percentage
*Ralative velocity
*Circular arrangement of 6 persons form 6 different countries.
*Rate of work done.




Here I'm posting some of the etchnical questions asked.




1.The datastructure in which values can be added form bith ends, not fron the
middle?
-- Dequeue


2. In a circular linked list if a new node is insertted, how many links will get modified?
(0  ,  1,  2 ,  3)



3. Find the hamming distance between 010011 and 101011
4. In software tesing which path will give maximum performance?
*Hamiltonian path
*Eulearian path
*BFS
*DFS



5. Which of the folloing will give the minimal path between a node S to other nodes in
a connected unweighted, undirested graph?
*Dijikstra algorith
*Warshalls algorithm



6. Which of the following is false about C?
*C has no I/O functions
*C has no File functions
* None of these



7. In a C progra main( int argc, cahr argv[]),
argv[0] indicates?

Ans-- Name of the C program

8. In a C code exit() will act for
Ans-- to exit from the current working function.


9. The uninitialized automatc variable in C contains?
Ans-- garbage value




10. The variables that ca be accessed fro any part of the program is called?
Ans-- Global variable



11. the unmodulated wave in data transmission is called?
- Carrier wave



12. End to end connection, error detection error correction are donr by?
-- Transport Layer



13.Encryption and Decrytption are done by?
-- Presentation Layer



14. what is Thrashing?


15. What is the maximum number of processes which ensure that no dead lock will
occur, if there are 6 tapes and each process demand for at most 2 tapes.
Ans: 3



16. Bankers alogithm deals with?
-- Deadlock avoidance.


17. Spot the function of Type IV Firewall


18. What is Trojan?

19. what is Worm?

20. readability of a code can be enhance by?
-- Giving meaningful names to variables.



21. Any HTML code starts with?
<html>




22. We can align text in a teble o left by which code?

23. www belongs to which category?
*Local server
*Client server
*3 - tier architectre



24.Resolution of an image doesnt depend on?
-- bandwidth



25. Plotter image quality depends on repeatability and__________?

26. what is data warehosing?

27. File is?
collection of data



28. Phone number is an example of_?
Field


29. If the input is larger than the field , then..
Ans-- actual input will be shortened.



30. Which of the following will  depend on a high speed internet?
Email


31. In a complete binary tree, if there are N leaves, total number of internal  nodes will
be?
2N-1




32. If a inary tree is of 4 levels, total number of nodes will be
15


33. For a 3-ary tree, if there are 6 internal nodes, toal number of leaves will be?

34. What is  the maximum number of fulll duplex connection with 10 hubs.

35. Networking will not depend upon
stateless end hubs




36. snowflake schema is related with?
Dimension




37 Hub is related with?
Star Topology


38. One token policy is strictly followed by?
IEEE or FDDI




39. What is the advantage of OOP concept?

40. What is data hiding in C++



41. In a Lan connection each device is conneted using a cable and._________?
Interface card.


42. if new() is failed it will return?
*value 0
*NULL
*Exception
*Unexpected results




43. The return value of destructor is
-- Destructor has no return value




44return type of this function   int fun ( char t, char u)
ans-- int  



45. In stop and wait protocol, for sending N frames how many acknowledgemnets are
there?
Ans -- N



46. QPSK with 16 levels, 4600bps the rate will be?

47. How many bit address are used in TCP/IP connction?




Do you know any other question??  Please comment here..!! :)

Tuesday, June 19

Pointers (Lecture Notes) : CUSAT s1s2 Questions




Hi friends,

Here I'm posting some important concepts asked in CUSAT B Tech S1 S2 C programming examination. All the concepts are descried here in very briefly so that you can grasp it quickly.
Here we go..!!! :)



Pointer

· Pointer is a variable that represents the location of a data item.

· Address operator (&) : It evaluates the address of its operand.

· Indirection Operator(*): Used to access data item referenced by a pointer.

Example:

Main()

{

Int a=3;

Int *ptr;

Ptr=&a;

Printf(“%d %u” , *ptr, ptr);

}

Pointer to an array

· A pointer can be used to reference an array.

· The name of an array will work as the pointer to its first element.

Main()

{

Int a[]= { 1, 2 , 3, 4};

Int *ptr =a;

For(int i=0; i<4;i++)

{

Printf(“%d”,* ptr+i);

}

Arrays of Pointers

· A group of pointers are stored in an array.

· Arrays of pointers can be used to store multidimensional arrays

Datatype *array[size];

Structures:

· A collection of heterogeneous elements.

· In structure memory will be allocated for each element.

Struct student {

Char name[20];

Int mark;

};

Union

· Similar to structures, A collection of heterogeneous elements.

· Memory will be allocated for largest member only. Other members have to share this memory.

union student {

Char name[20];

Int mark;

};





Thursday, May 31

SELECTION SORT ~ CUSAT Previous Questions with Answers- C programming.( S1 S2)



Hi friends,

Here I'm posting a C program implementing Selection sort. It is specially dedicated to S1 S2 B tech students because Selection sort is one of the most frequently asking questions in "Computer  Programming" exams. Hope this will be Useful  4  you.!!! :)



#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j,temp,a[50]; 


printf("\nEnter how many elements=");
scanf("%d",&n); 


printf("\nEnter %d elements",n);//
Input the elements
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);




for(i=0;i<n-1;i++)//
Sorting the elements
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf("\nSorted Array:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}

}


Any doubt in the code? Comment it here..!!

Monday, May 28

CUSAT Previous Questions with Answers- C programming.( S1 S2)





Hi Friends,


here I'm posting one of the frequently asking question in CUSAT B tech C programming ( s 1 s2 ) and its solution:

Question:



Write a C program which prints all Armstrong numbers between 1 and 1000.


Solution:



#include<stdio.h>
main()
{
int number, temp, digit=0, dup;

printf("Printing all Armstrong numbers between 1 and 500:\n\n");

number = 1;

while (number <= 500)
{
temp=0;
dup=number;
while(dup>0)
{
digit=dup%10;
temp+=(digit*digit*digit);
dup=dup/10;
}
if (temp==number)
{
printf("\nAmstrong Number:%d", temp);
}
number++;
}
getch();
}
 

Monday, May 21

Generate Interesting Patterns Using C

Hi friends,

Look at the "+"  pattern generated by asterisks. Quite interesting na..!! :)
Lets have a look in a C program which generates exactly the same pattern.!!!!!

 main()
{
int i,j;
for ( i= 0; i < 11; ++i)
{
for ( j = 0; j < 11; ++j)
      if (j != 5 && i !=5 )
     printf(" "); 
      else printf ("* ");
printf("\n");
}
}



Saturday, March 17

Concatenate strings without using library functions


#include<stdio.h>


main()
{
      char first[20], second[20],result[40];
      int i,j;
      printf("Enter first string:");
      gets(first);
      printf("Enter second string:");
      gets(second);
   for(i=0;first[i]!='\0';i++)result[i]=first[i];
   for(j=0;second[j]!='\0';i++,j++)result[i]=second[j];
      
      printf("%s",result);


      }

Concatenate strings using library functions



#include<stdio.h>
#include<string.h>
main()
{
      char first[20], second[20],result[40];
      int i,j;
      printf("Enter first string:");
      gets(first);
      printf("Enter second string:");
      gets(second);
    
      for(i=0;i<strlen(first);i++)result[i]=first[i];
      for(j=0;j<strlen(second);i++,j++)result[i]=second[j];
      result[i]='\0';
    
      printf("%s",result);
  
      }

Fibonacci series using Recursion ~ C program


Here is a C program which prints a Fibonacci series up to a limit using recursion.





#include<stdio.h>


void fibonacci(int a, int b,int count)


{
     printf("%d\t",a+b);
     if(--count) fibonacci(b,a+b,count);
}


void main()
{
     printf("Enter limit:");
     int count;
     scanf("%d", &count);
      printf("0\t 1\t");
     fibonacci(0,1,count-2);
   
     }
    

Saturday, January 21

Convert Decimal Number to Binary Equivalent Using Linked list - C




Hi friends,


          In this post I'll explain "how  to convert a decimal number to binary equivalent using Linked list". Usually, we use array for this purpose, but arrays are created statically and memory wastage  may occur. Linked list is a  perfect solution for this as it is created dynamically and no there is no wastage of memory also.!!!!






Algorithm




  1. Initialize PREV_NODE and NODE as NULL.
  2. Input a number 'num'.
  3. if num<=0 go to step 9
  4. PREV_NODE=NODE
  5. add (num%2) to NODE->number
  6. NODE->next=PREV_NODE
  7. num=num/2
  8. go to step 3
  9. if NODE is NULL go to 13
  10. print NODE->number
  11. NODE=NODE->next 
  12. go to step 9
  13. End






The complete code in C is given here:




#include<stdio.h>


struct binary {
       int num;
       struct binary *next;
       };


struct binary* add( struct binary **curr, int n)
 {
  
   struct binary *temp;
   temp=(struct binary *)malloc(sizeof(struct binary));
   temp->num=n;
   temp->next=*curr;
   return temp;
      
 }
       


int main()
{
     int number, binary_num;
     struct binary *head= NULL;
     printf("ENTER A DECIMAL NUMBER  :   ");
     scanf("%d",&number);
     
     while(number>0)
     {
     binary_num=number%2;
     head =add(&head,binary_num);
     number/=2;
     }
  printf("EQUIVALENT BINRY NUMBER IS   :   ");  
  for(;head!=NULL;head=head->next)
     printf("%d",head->num);


}










Friends, I hope this post will be useful to you..!!!
Your valuable suggestions are always welcomed..!!!!Comment it here.!!


Monday, November 21

Read a file and copy content to other file~ C code




This C program will read an input file and copy the content to another file with Line numbers..!!


#include<stdio.h>
main()
{
      FILE *f1,*f2;
      int t=0;
      char line[20];
      f1=fopen("in.c","r");
      f2=fopen("out.c","w");
   
      while(!feof(f1)){
      fgets(line,20,f1);
      t++;
      fprintf(f2,"%d  %s",t,line);
      }
     getch();
      }

Sunday, November 20

Quadruple ~ C code

#include<stdio.h>
#include<string.h>


main()
{

 char line[20];
int s[20];
int t=1;
     
int i=0;     
printf("Enter string..  :");
gets(line);
for(i=0;i<20;i++)s[i]=0;

printf("op\ta1\ta2\tres\n");
for(i=2;line[i]!='\0';i++)
{
if(line[i]=='/' || line[i]=='*')
{
                printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;

}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);

s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}


for(i=2;line[i]!='\0';i++)
{
if(line[i]=='+' || line[i]=='-')
{
                printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;

}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);

s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}
printf("\n:=\tt%d\t\t%c",t-1,line[0]);




getch();
}





Output




Enter string..  :a=b*c-b*c


op      a1      a2      res
:=      c                t1
*       b       t1       t2
:=      c                t3
*       b       t3       t4
-       t2      t4       t5
:=      t5              a

Tuesday, November 1

FOLLOW OF GIVEN GRAMMAR USING C PROGRAM

Hello reader,
In this post I am posting implementation of FOLLOW in C.
Note:
If your production statements contain multiple terms connected by '|'  then give these terms as separate productions
 


#include<stdio.h>
#include<string.h>
int n,m=0,p,i=0,j=0;
char a[10][10],followResult[10];
void follow(char c);
void first(char c);
void addToResult(char);
int main()
{
 int i;
 int choice;
 char c,ch;
 printf("Enter the no.of productions: ");

scanf("%d", &n);

 printf(" Enter %d productions\nProduction with multiple terms should be give as separate productions \n", n);
 for(i=0;i<n;i++)
  scanf("%s%c",a[i],&ch);
    // gets(a[i]);

 do
 {
  m=0;
  printf("Find FOLLOW of -->");

  scanf(" %c",&c);
  follow(c);
  printf("FOLLOW(%c) = { ",c);
  for(i=0;i<m;i++)
   printf("%c ",followResult[i]);
  printf(" }\n");
  printf("Do you want to continue(Press 1 to continue....)?");
 scanf("%d%c",&choice,&ch);
 }
 while(choice==1);
}
void follow(char c)
{

    if(a[0][0]==c)addToResult('$');
 for(i=0;i<n;i++)
 {
  for(j=2;j<strlen(a[i]);j++)
  {
   if(a[i][j]==c)
   {
    if(a[i][j+1]!='\0')first(a[i][j+1]);

    if(a[i][j+1]=='\0'&&c!=a[i][0])
     follow(a[i][0]);

   }
  }
 }
}
void first(char c)
{
      int k;
                 if(!(isupper(c)))
                     //f[m++]=c;
                     addToResult(c);
                 for(k=0;k<n;k++)
                 {
                 if(a[k][0]==c)
                 {
                 if(a[k][2]=='$') follow(a[i][0]);
                 else if(islower(a[k][2]))
                     //f[m++]=a[k][2];
                     addToResult(a[k][2]);
                 else first(a[k][2]);
                 }
                 }

}

void  addToResult(char c)
{
    int i;
    for( i=0;i<=m;i++)
        if(followResult[i]==c)
            return;
   followResult[m++]=c;
}




Sample output: