2014

Utopian Tree



Problem Statement
The Utopian Tree goes through 2 cycles of growth every year. The first growth cycle occurs during the spring, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter. 
Now, a new Utopian Tree sapling is planted at the onset of spring. Its height is 1 meter. Can you find the height of the tree after N growth cycles?

SOURCE CODE:

 #include<stdio.h>  
 void main()  
 {  
   int t,test,total;  
   scanf("%d",&test);  
   while(test--){  
     scanf("%d",&t);  
     total=1;  
     int flag=1;  
     while(t--)  
     {  
       if(flag){  
         if(total==1){  
           total+=1;  
         }else{  
         total*=2;  
         }  
       flag--;  
       }  
       else{  
         total+=1;  
         flag++;  
       }  
     }  
     printf("%d\n",total);  
   }  
 }   

UniPolar Encoding


Unipolar encoding has 2 voltage states with one of the states being 0 volts. Since Unipolar line encoding has one of its states being 0 Volts, it is also called Return to Zero (RTZ). A common example of Unipolar line encoding is the logic levels used in computers and digital logic. A logic High (1) is represented by +5V and a logic Low (0) is represented by 0V.

Unipolar line encoding works well for inside machines where the signal path is short but is unsuitable for long distances due to the presence of stray capacitance in the transmission medium. On long transmission paths, the constant level shift from 0 volts to 5 volts causes the stray capacitance to charge up. There will be a "stray" capacitor effect between any two conductors that are in close proximity to each other. Parallel running cables or wires are very suspectible to stray capacitance.
If there is sufficient capacitance on the line and a sufficient stream of 1s, a DC voltage component will be added to the data stream. Instead of returning to 0 volts, it would only return to 2 or 3 volts! The receiving station may not recognize a digital low at voltage of 2 volts!
Unipolar line encoding can have synchronization problems between the transmitter and receiver's clock oscillator. The receiver's clock oscillator locks on to the transmitted signal's level shifts (logic changes from 0 to 1). If there is a long series of logical 1s or 0s in a row. There is no level shift for the receive oscillator to lock to. The receive oscillator's frequency may drift and become unsynchronized. It could lose track of where the receiver is supposed to sample the transmitted data!
Receive oscillator may drift during the period of all 1s


Below is an simulation for unipolar encoding in java.

SOURCE CODE:

 import java.util.Scanner;  
 public class Lowhigh {  
   public static void main(String[] args) {  
     String input = "";  
     String line1 = "";  
     String line2 = "";  
     String line3 = "";  
     int flag = 0, i;  
     Scanner inputs = new Scanner(System.in);  
     System.out.print("Enter the binary number :");  
     input = inputs.next();  
     for (i = 0; i < input.length(); i++) {  
       if (input.charAt(i) == '1') {  
         if (flag == 0) {  
           line1 += " ";  
           line2 += "|";  
           line3 += "|";  
           flag = 1;  
         }  
         line1 = line1 + " _ ";  
         line2 = line2 + "  ";  
         line3 = line3 + "  ";  
       } else {  
         if (flag == 1) {  
           line1 += " ";  
           line2 += "|";  
           line3 += "|";  
           flag = 0;  
         }  
         line1 = line1 + "  ";  
         line2 = line2 + "  ";  
         line3 = line3 + " _ ";  
       }  
     }  
     System.out.println(line1);  
     System.out.println(line2);  
     System.out.println(line3);  
   }  
 }  


Operator Precedence


Simple program for operator precedence parser in C.
It takes the expression as input and calculates the precedence of the operators.

Subscribe to the blog to get instant updates via E-Mail.

PROGRAM CODE:


 #include<stdio.h>  
 #include<string.h>  
 void main()  
 {  
   int i,j,cnt=1;  
   char operators[] ="*/%+-",input[100];  
   printf("Enter the statement : ");  
   gets(input);  
   for(i=0;i<strlen(operators);i++)  
   {  
     for(j=0;j<strlen(input);j++)  
     {  
       if(input[j]==operators[i])  
       {  
         printf("%d %c=%c%c%c\n",cnt++,input[j-1],input[j-1],input[j],input[j+1]);  
         input[j+1]=input[j-1];  
       }  
     }  
   }  
 }  

OUTPUT:



Subscribe to the blog to get instant updates via E-Mail.

Notify the Suggestions about the program through comments ... :)

Lexical Analyzer


Simple Lexical Analyzer program. It takes the filename in the same directory as the input and analyzes the program for variables operators and special characters. and prints the output for each values separately.

 #include<stdio.h>  
 #include<conio.h>  
 #include<string.h>  
 void main()  
 {  
      FILE *fp;  
      int i=0;  
      char input[100],filename[20];  
      clrscr();  
      printf("Enter Filename : ");  
      scanf("%s",filename);  
      fp=fopen(filename,"r");  
      while(!feof(fp))  
      {  
           input[i]=fgetc(fp);  
           i++;  
      }  
      input[--i]='\0';  
      for(i=0;i<strlen(input);i++)  
      {  
           if(isalpha(input[i]))  
           {  
                printf("%c is a Character.\n",input[i]);  
           }  
           else if(input[i]=='='||input[i]=='+'||input[i]=='-'||input[i]=='*'||input[i]=='/'||input[i]=='%')  
           {  
                printf("%c is an Operator.\n",input[i]);  
           }  
           else if(isspace(input[i])||iscntrl(input[i]))  
           {  
           }  
           else if(isdigit(input[i]))  
           {  
                printf("%c is a number.\n",input[i]);  
           }  
           else  
           {  
                printf("%c is a Special character.\n",input[i]);  
           }  
      }  
      getch();  
 }  
Notify the Suggestions about the program through comments ... :)

C++ Hourglass pattern


  C++ program to print the hourglass pattern using '#' symbols. In this program two for loops are used to print the pattern. The first loop prints the upper part to the hourglass (an inverted triangle ).
The second for loop prints the lower part of the hourglass ( triangle part ). By combining both of the triangles an hourglass structure is formed.

PROGRAM CODE:

 #include<iostream.h>  
 #include<conio.h>  
 void main()  
 {  
     int i,j,k=9,s=0,l;  
     clrscr();  
     for(i=0;i<5;i++,k-=2,s+=1)  
     {  
          for(l=s;l>=0;l--)  
          cout<<' ';  
          for(j=k;j>0;j--)  
               cout<<"#";  
          cout<<endl;  
     }  
     for(i=0;i<5;i++,k-=2,s-=1)  
     {  
          for(l=s-1;l>=0;l--)  
          cout<<' ';  
          for(j=k;j<0;j++)  
              cout<<"#";  
          cout<<endl;  
    }  
    getch();  
 }  

OUTPUT:

# # # # # # # # #
   # # # # # # #
      # # # # #
        # # #
           #
        # # #
     # # # # #
   # # # # # # #
# # # # # # # # #

C++ Triangle pattern


PROGRAM CODE :

 #include<iostream.h>  
 #include<conio.h>  
 void main()  
 {  
          char a[]="program";  
          int i,j;  
          clrscr();  
          for(i=0;i<7;i++)  
          {  
                   for(j=0;j<=i;j++)  
                   {  
                           cout<<a[j]<<'\t';  
                   }  
                   cout<<endl;  
           }  
           getch();  
 }  

OUTPUT :

   p
   p    r 
   p    r    o 
   p    r    o    g 
   p    r    o    g    r 
   p    r    o    g    r    a
   p    r    o    g    r    a    m

Text analyzer


The following Text analyzer program is implemented in c program. It program analyzes the given text from the user and calculate the number of words and total number of characters in the line of text. It determines the numbers of vowels, consonants, white space characters and other characters for each line. And finally determines average number of vowels, consonants per line.

Concepts used:

Array concept is used to store the input text which is to be analysed. The input text is entered during run time. The text contains space hence gets function is used to get the input text from the user.
Local variables and global variables are used. For loops and while loops are used to calculate the number of characters and words. Function is used to check whether the given character matches with the available character.

Source code:

 #include<stdio.h>  
 #include<conio.h>  
 #include<string.h>  
 int stc(char *,char);  
 void main()  
 {  
 int ch,ln=0,space,vow,con,other,err,tch=0,twrd=0,tvow=0,tcon=0;  
 char txt[500],*l, v[]="aeiouAEIOU";  
 char c[]="bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";  
 printf("\t\t\t\tTEXT ANALYZER");  
 printf("\nEnter the text to be analyzed :\n");  
 gets(txt);  
 for(l=strtok(txt,".");l!=NULL;l=strtok(NULL,"."))  
 {  
 ch=1,err=0,vow=0,space=0,other=1,con=0;  
 ln++;  
 if(*l==' ')  
 {  
 err++;  
 }  
 while(*l!='\0')  
 {  
 if(stc(v,*l)==1)  
 vow++;  
 else if(stc(c,*l)==1)  
 con++;  
 else if(*l==' ')  
 space++;  
 else  
 other++;  
 l++,ch++;  
 }  
 if(*(l-1)==' ')  
 {  
 err++;  
 }  
 printf("\nnumber of vowels in line %d : %d" ,ln,vow);  
 printf("\nnumber of consonants in line %d : %d" ,ln,con);  
 printf("\nnumber of white spaces in line %d : %d" ,ln,space);  
 printf("\nnumber of other characters in line %d : %d" ,ln,other);  
 printf("\nnumber of words in line %d : %d" ,ln,space+1-err);  
 printf("\nnumber of characters in line %d : %d\n" ,ln,ch);  
 tvow+=vow;  
 tcon+=con;  
 tch+=ch;  
 twrd+=space+1-err;  
 }  
 printf("\nAverage number of vowels per line : %d",tvow/ln);  
 printf("\nAverage number of consonants per line : %d",tcon/ln);  
 printf("\nTotal number of words : %d",twrd);  
 printf("\nTotal number of characters : %d",tch);  
 getch();  
 }  
 int stc(char *str, char c)  
 {  
 while(*str!='\0')  
 {  
 if(*str==c)  
 return 1;  
 str++;  
 }  
 return 0;  
 }  

OUTPUT:

Text analyser sample output
Text analyzer

text analyzer sample output
Text analyzer

Library Management System ( CPP coding ) :



Library Management System has been implemented as a C++ program. Complete program code is given below. Complete project is available on user request. User comments are welcome to notify any Bugs or to improve the further posts. :)


Concepts used:

Multilevel inheritance is used in this program. The files concepts were also used to store and retrieve the required data's for the login details, membership details and book details.

Modules :  

1. Login maintenance 
2. Library maintenance 
3. Book issue maintenance 
4. Book return maintenance 

LOGIN MAINTENANCE

 Separate login for administrator, member and guest should be provided.
 Should accept user name and password from the user.
 Should compare the user name and password (case sensitive).
 If the user name and password matches then the menu for the respective login
should be displayed.
 If the user name or password is incorrect it should display an error message.
 If the user name or password is wrong return to previous menu.

LIBRARY MAINTENANCE

This module has the following functions

 Add member

 Every member should be allocated with a unique ID.
 The member ID should be generated by the system.
 Each member should be filled with their details such as name address etc.
 After adding a new member, it should return to the main menu.

 View/Delete a member

 If the member ID is entered the details of the member should be
displayed.
 If the member should be removed from the list, use delete option to
remove the member from the list.

 Add books

 Should add new books to the library.
 Read book details such as book name, author etc.
 Store the details of the book.

 View book details

 Read the book title to search the book.
 If the book is found display the details of the book.

 View all books

 If this module is executed list all the available books in the library.

BOOK ISSUE MAINTENANCE

 Only members can take the books from library.
 Search the book and click to view the book details.
 If the member wants that book and if that book is available lend that book.
 Store the member details and book details along with the issue date in issue.txt
file.
 Update the book detail as not available.

BOOK RETURN MAINTENANCE

 User should be logged in as a member.
 To return a book enter the book number.
 Display the issue date.
 Calculate the number of days from issue to return.
 If the number of days is more than 15 charge RS.1 for each day.
 Calculate the total fine amount.
 Update the book status as available.

Source Code :

 #include<iostream.h>  
 #include<iomanip.h>  
 #include<string.h>  
 #include<conio.h>  
 #include<fstream.h>  
 int line=0,entry=0,mem=1,log=0,no,id;  
 class library  
 {  
      public:  
           void header();  
           void footer();  
 }obj;  
 class books: public library  
 {  
      protected:  
           char name[20],author[20];  
      public:  
           int code,stat;  
           void update(ofstream &fout)  
           {  
                fout<<code<<' '<<name<<' '<<author<<' '<<stat<<endl;  
           }  
           friend void book_display();  
 }bks[10];  
 class users : public books  
 {  
      protected:  
           char name[20];  
           int rollno,member_id;  
      public:  
           int take,tcode;  
           void welcome()  
           {  
                cout<<setw(40)<<"WELCOME "<<name;  
           }  
           void read(ifstream &fin)  
           {  
                fin>>member_id>>rollno>>name>>take>>tcode>>mem;  
           }  
           void update(ofstream &fout)  
           {  
           fout<<member_id<<' '<<rollno<<' '<<name<<' '<<take<<' '<<tcode<<' '<<mem<<endl;  
           }  
           void login()  
           {  
                if(no==rollno&&id==member_id)  
                     entry=1;  
           }  
           void add()  
           {  
                obj.header();  
                cout<<endl<<setw(48)<<"ADD MEMBER\n\n";  
                cout<<setw(50)<<"Enter Member ID :";  
                cin>>member_id;  
                cout<<setw(50)<<"Enter Roll number:";  
                cin>>rollno;  
                cout<<setw(50)<<"Enter Name    :";  
                cin>>name;  
                obj.footer();  
                take=0;  
                tcode=0;  
                mem++;  
           }  
           void display()  
           {                         cout<<member_id<<'\t'<<rollno<<'\t'<<name<<'\t';//<<"\t\t"<<take<<'\t'<<tcode<<endl;  
                if(tcode==0)  
                {     cout.setf(ios::right);  
                     cout.width(15);  
                     cout<<"No"<<endl;  
                }  
                else  
                {  
                     cout.setf(ios::right);  
                     cout.width(15);  
                     cout<<tcode<<endl;  
                }  
           }  
 };  
 class admins: public users  
 {  
      protected:  
           char user[20],passwd[30];  
      public:  
           admins()  
           {  
                strcpy(user,"gokul");  
                strcpy(passwd,"gokul");  
           }  
           void login();  
 };  
 void library::header()  
 {  
      int i;  
      clrscr();  
      for(i=0;i<5;i++)cout<<endl;  
      for(i=0;i<80;i++)cout<<"*";  
      cout<<"*"<<setw(48)<<"Digital Library"<<setw(31)<<"*";  
      cout<<"*"<<setw(44)<<"KCE, CBE"<<setw(35)<<"*";  
      for(i=0;i<80;i++)cout<<"*";  
      line=0;  
 }  
 void library::footer()  
 {  
      int i;  
      for(i=line;i<10;i++)cout<<endl;  
      for(i=0;i<80;i++)cout<<"*";  
 }  
 void admins::login()  
 {  
      int i=0;  
      char c,name[20],passwrd[20];  
      cout<<"\n\n"<<setw(51)<<"ADMINISTRATOR LOGIN";  
      cout<<"\n\n"<<setw(40)<<"username:";  
      cin>>name;  
      cout<<setw(40)<<"Password:";  
      while((c=getch())!=13)  
      {  
           passwrd[i++]=c;  
           cout<<'*';  
      }  
      passwrd[i]='\0';  
      if((strcmp(user,name)==0)&&(strcmp(passwd,passwrd)==0))  
      {  
           entry=1;  
      }  
      else  
      {  
           entry=0;  
           cout<<endl<<setw(55)<<"Incorrect username/password";  
           line=line+6;  
           obj.footer();  
           getch();  
      }  
 }  
 void book_display();  
 int main()  
 {  
      int i,login;  
      users student[10];  
      ifstream fin("members.txt");  
      for(i=0;i<mem;i++)  
           student[i].read(fin);  
      fin.close();  
      obj.header();  
      cout<<endl<<endl<<setw(45)<<"LOGIN AS\n"<<endl<<setw(50)<<"1. Administrator\n"<<setw(44)<<"2. Student\n"<<setw(40)<<"3. Exit";  
      line=line+6;  
      obj.footer();  
      cout<<endl<<setw(47)<<"Enter choice:";  
      cin>>login;  
      switch(login)  
      {  
           case 1:  
           {  
                admins admin;  
                obj.header();  
                admin.login();  
                if(entry)  
                {  
                     int choice;  
                     do  
                     {  
                          obj.header();  
                          cout<<"Welcome Gokul,"<<setw(66)<<"0-Logout";  
                          cout<<"\n"<<setw(50)<<"Adminstrator Menu";  
                          cout<<"\n\n"<<setw(49)<<"1.View members";  
                          cout<<"\n"<<setw(48)<<"2.Add members";  
                          cout<<"\n"<<setw(51)<<"3.Delete members";  
                          cout<<"\n"<<setw(47)<<"4.View books";  
                          line=line+7;  
                          obj.footer();  
                          cout<<endl<<setw(50)<<"Enter your choice:";  
                          cin>>choice;  
                          switch(choice)  
                          {  
                               case 0:  
                                    break;  
                               case 1:  
                               {  
                                    int i;  
                                    obj.header();  
                          cout<<setw(47)<<"Members List"<<endl;  
                          cout<<"\t\tID\tRoll\tName"<<setw(30)<<"Book taken"<<endl;  
                                    for(i=0;i<mem;i++)  
                                    {  
                                         cout<<"\t\t";  
                                         student[i].display();  
                                    }  
                                    line=line+mem+2;  
                                    obj.footer();  
                                    getch();  
                                    break;  
                               }  
                               case 2:  
                               {  
                                    student[mem].add();  
                                    break;  
                               }  
                               case 3:  
                               {  
                                    int i,del;  
                                    obj.header();  
                               cout<<setw(47)<<"Members List"<<endl;  
                               cout<<"\tS.No\tID\tRoll\tName"<<setw(30);  
 cout<<"Book taken"<<endl;  
                                    for(i=0;i<mem;i++)  
                                    {  
                                         cout<<"\t"<<i+1<<"\t";  
                                         student[i].display();  
                                    }  
                                    line=line+mem+2;  
                                    obj.footer();  
                                    cout<<setw(47)<<"Enter your choice:";  
                                    cin>>del;  
                                    ofstream out("members.txt");  
                                    int j=mem;  
                                    mem--;  
                                    for(i=0;i<j;i++)  
                                    {  
                                         if(i+1!=del)  
                                              student[i].update(out);  
                                    }  
                                    out.close();  
                                    ifstream fin("members.txt");  
                                    for(i=0;i<mem;i++)  
                                         student[i].read(fin);  
                                    fin.close();  
                                    break;  
                               }  
                               case 4:  
                               {  
                                    obj.header();  
                                    book_display();  
                                    obj.footer();  
                                    getch();  
                                    break;  
                               }  
                               default:  
                               {       
 cout<<endl<<setw(55)<<"Enter a valid choice...";  
                               getch();  
                               }  
                          }  
                          ofstream out("members.txt");  
                          for(i=0;i<mem;i++)  
                               student[i].update(out);  
                          out.close();  
                     }while(choice!=0);  
                     entry=0;  
                }  
                main();  
                break;  
           }  
           case 2:  
           {  
                obj.header();  
                int i;  
                cout<<"\n\n"<<setw(48)<<"STUDENT LOGIN";  
                cout<<"\n\n"<<setw(43)<<"Member ID:";  
                cin>>id;  
                cout<<setw(43)<<"Roll no:";  
                cin>>no;  
                for(i=0;i<mem;i++)  
                {  
                     log=i;  
                     student[log].login();  
                     if(entry==1)  
                          break;  
                     else if(entry==0&&i+1==mem)  
                     {  
                          entry=0;  
                          cout<<endl<<setw(52)<<"Incorrect ID/Roll no";  
                          line=line+7;  
                          obj.footer();  
                          getch();  
                     }  
                }  
                if(entry)  
                {  
                     int choice;  
                     do  
                     {  
                          obj.header();  
                          cout<<"Take/Return"<<setw(69)<<"0-Logout";  
                          student[log].welcome();  
                          book_display();  
                          line=line+1;  
                          obj.footer();  
                          cout<<"Taken books/book code:"<<student[log].tcode;  
 cout<<setw(25)<<"Enter your choice:";  
                          cin>>choice;  
                          if(bks[choice-1].stat==1&&student[log].take==0)  
                          {  
                               bks[choice-1].stat=0;  
                               student[log].take=1;  
                               student[log].tcode=bks[choice-1].code;  
                               obj.header();  
                          cout<<"\n\n"<<setw(55)<<"Book taken successfully...\n";  
                          cout<<endl<<setw(60)<<"Return the book before 15 days.";  
                          cout<<endl<<setw(60)<<"A fine of Rs.1 will be collected\n";  
 cout<<setw(60)<<"for each day after the due date.";  
                               line=line+6;  
                               obj.footer();  
                               getch();  
                          }  
 else if(bks[choice-1].stat==0&&student[log].take==1&&student[log].tcode==bks[choice-1].code)  
                          {  
                               int days,fine=0;  
                               bks[choice-1].stat=1;  
                               student[log].take=0;  
                               student[log].tcode=0;  
                               obj.header();  
                               cout<<"\n\n"<<setw(48)<<"Book Return\n\n";  
                               cout<<setw(50)<<"Enter number of days:";  
                               cin>>days;  
                               if(days>15)  
                               {  
                               fine=days-15;  
                               cout<<"\n\n"<<setw(47)<<"Fine amount:"<<fine;  
                               }  
                               else  
                               cout<<"\n\n"<<setw(55)<<"Book returned successfully";  
                               line=line+7;  
                               obj.footer();  
                               getch();  
                          }  
                          else  
                          {  
                             if(choice)  
                             {  
                               obj.header();  
                               cout<<"\n\n"<<setw(53)<<"unable to take/return";  
                               cout<<endl<<setw(53)<<"Possible reasons:\n";  
                               cout<<endl<<setw(56)<<"- Return the taken book";  
                               cout<<endl<<setw(60)<<"- Book may not be available";  
                               line=line+6;  
                               obj.footer();  
                               getch();  
                             }  
                          }  
                          ofstream fout("books.txt");  
                          for(i=0;i<10;i++)  
                          {  
                               bks[i].update(fout);  
                          }  
                          fout.close();  
                          ofstream out("members.txt");  
                          for(i=0;i<mem;i++)  
                               student[i].update(out);  
                          out.close();  
                     }while(choice!=0);  
                }  
                entry=0;  
                main();  
                break;  
           }  
           case 3:  
           {  
                return 0;  
           }  
           default:  
           {  
                cout<<setw(52)<<"Enter a valid option!";  
                getch();  
                main();  
                break;  
           }  
      }  
      return 0;  
 }  
 void book_display()  
 {  
      int i;  
      ifstream fin("books.txt");  
      for(i=0;i<10;i++)  
           fin>>bks[i].code>>bks[i].name>>bks[i].author>>bks[i].stat;  
      cout<<endl<<"S.No\tCode\tName\t\t\tAuthor"<<setw(34)<<"Available";  
      for(i=0;i<10;i++)  
      {  
           cout<<" "<<i+1<<'\t'<<bks[i].code<<"\t"<<bks[i].name<<"\t\t"<<bks[i].author;  
           if(bks[i].stat==1)  
           {  
                cout.setf(ios::right);  
                cout.width(10);  
                cout<<"\t\tYes\n";  
           }  
           else  
           {  
                cout.setf(ios::right);  
                cout.width(10);  
                cout<<"\t\tNo\n";  
           }  
      }  
      line=10;  
 }  

 Output:

Login menu
Login Menu

Login welcome screen
Administrator Menu

Availability of books
List of Available books

username and password
Membership details

books list
Available books

incorrect password
Login failure






















Complete SRS of Library Management system ( LMS ) and algorithms are available in pdf format.
Comment your E-mail ID to get it as a mail.
Subscribe via e-mail to get instant updates... Happy coding... :) ;)