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 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.

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.

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();
}


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:

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

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 analyzer

 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 :

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

 Should accept user name and password from the user.
 Should compare the user name and password (case sensitive).
should be displayed.
 If the user name or password is incorrect it should display an error message.

LIBRARY MAINTENANCE

This module has the following functions

 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.

 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.

 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.

Output:

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... :) ;)