Tuesday, 12 December 2017

Binary Tree Implementation and Operations in C

C program to implement binary tree and its three operations:

 1. Preorder traversal
 2. Inorder traversal
 3. Postorder traversal

Source Code

binarytree.c

#include <stdio.h>
#include <stdlib.h> 

// Defines a tree node as a structure
struct node { 
     struct node * left;
     char data; 
     struct node * right;
 };

// Function declaration
struct node * buildTree ( int ); 
void preorder(struct node *); 
void inorder(struct node *); 
void postorder( struct node *root ); 

// Tree node data
char array[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H' }; 

// Indices of left children
int leftcount[ ] = { 1, 3, 5, -1, 9, -1, -1, -1, -1, -1 }; 

// Indices of right children
int rightcount[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 }; 


int main() { 
      struct node *root; 
      int ch;
      root = buildTree ( 0 );
   
      do{
           printf("\n1: Preorder Traversal\n");
           printf("2: Inorder Traversal\n");
           printf("3: Postorder Traversal\n");
           printf("\tEnter your choice: ");
           scanf("%d",&ch);

           switch(ch) {
                case 1:
                     printf("\nPre-order Traversal: \n");
                     preorder(root);
                     break;
                case 2:
                     printf("\nIn-order Traversal: \n");
                     inorder(root);
                     break;
                case 3:
                     printf("\nPost-order Traversal: \n");
                     postorder(root); 
                     break;
                default:
                     printf("\nExiting.....\n");
           }
      }while(ch>0 && ch<4);
 } 

// Function to build tree using above data
struct node * buildTree ( int index ) { 
      struct node *temp = NULL;
      if (index != -1) {
           temp = (struct node *) malloc( sizeof ( struct node ) );
           temp->left = buildTree ( leftcount[index] );
           temp->data = array[index];
           temp->right = buildTree ( rightcount[index] );
      }
      return temp;
}

// Function to print tree data using inoder traversal 
void inorder( struct node *root ) {
      if (root != NULL) {
           inorder(root->left);
           printf("%c\t", root->data);
           inorder(root->right);
      }
}

// Function to print tree data using preorder traversal 
 void preorder( struct node *root ) {
      if (root != NULL) {
           printf("%c\t", root->data);
           preorder(root->left);
           preorder(root->right);
      }
}

// Function to print tree data using postorder traversal 
void postorder( struct node *root ) {
      if (root != NULL) {
           postorder(root->left);
           postorder(root->right);
           printf("%c\t", root->data);
      }
}






Thursday, 17 September 2015

How to add User Defined Function in an Excel Work Book?

MS Excel is very powerful for processing worksheets and any data that can be organized in a tabular form. However  built-in function are not available  for some sort of problems. For example I can share a practical scenario for which one of my friends asked me a solution.

He has a file which contains multiple data records exported from some accounting software. Each record contains an amount as the last word of the line. Like follows

Electricity Charge 3000
News Papers and Periodicals 2000
Food Expenses 1000
Miscellaneous 2500

etc.


His requirement is quite simple. He want to calculate the total of these expenses. He copied this text to a Excel wok sheet. In Excel work sheet one line of text will be considered as the content of one cell. Like this






Now the amounts should be displayed in column B so that it can be processed as numeric values.

In excel function to extract last part of a text  is not available.


 So I have added a new function in macro like below:


 Function LASTWORD(strSting As String) As String
    Dim strLastWord As String
    Dim arrWords() As String
  
  
    strSting = Trim(strSting)
    arrWords = Split(strSting, " ")
    strLastWord = arrWords(UBound(arrWords))
  
 
   
LASTWORD = strLastWord
End Function



 Now you call this function from any cell as a normal excel function like below

=LASTWORD(C10)






Tuesday, 6 January 2015

Multiplication of two matrices / Matrix multiplication

Let us see how to multiply two matrices. Here is the rule applicable for the multiplication of two matrices. Multiplication of two matrices is possible only if the number of columns of the first matrix is equal to the number of rows of the second matrix.

For example it is possible to multiply two matrices with size  2 x 2 and 2 x 3. Here number of columns of first matrix ie. 2 is equal to the number of rows of the second matrix that is again  2.

Example:



















C program for matrix multiplication

matmult.c



#include <stdio.h>
int main() {
int m, n, p, q, i, j, k, sum = 0;
int a[10][10], b[10][10], c[10][10];

clrscr();
   
        // Input order or size of first matrix
printf("Enter the number of rows and columns of first matrix:");
scanf("%d%d", &m, &n);

        // Input first matrix
for (i=0;i<m;i++)
for (j=0;j<n;j++) {
printf("Enter the element %d, %d of matrix a:", i,j);
scanf("%d", &a[i][j]);
}

         // Input order or size of second matrix
printf("\nEnter the number of rows and columns of second matrix:");
scanf("%d%d", &p, &q);

         // Check whether the matrices are compatible for multiplication
if (n!=p)
printf("\nMatrices with entered orders can't be multiplied with each other.");
else {
                // Input second matrix
for (i=0;i<p;i++)
for (j=0;j<q;j++) {
printf("Enter the %d, %d element of  matrix b:", i, j);
scanf("%d", &b[i][j]);
}
             
               // Logic for multiplication
for (i=0;i<m;i++) { // Loops row times of first matrix
for (j=0;j<q;j++) { // Loops column times of second  matrix
for (k=0;k<p;k++) { // Loops row times of  second matrix
sum=sum+a[i][k]*b[k][j];
}

c[i][j] = sum;
sum = 0;
}
}

                // Displays first matrix
printf("Matrix A:\n");

for (i=0;i<m;i++) {
for (j=0;j<n;j++)
printf("%d\t", a[i][j]);
printf("\n");
}

                // Displays second matrix
printf("Matrix B:\n");

for (i=0;i<p;i++) {
for (j=0;j<q;j++)
printf("%d\t", b[i][j]);
printf("\n");
}

                // Displays third matrix
printf("Product of entered matrices:\n");

for (i=0;i<m;i++) {
for (j=0;j<q;j++)
printf("%d\t", c[i][j]);
printf("\n");
}
}

return 0;
}

Monday, 15 December 2014

Google translator introduces Malayalam

Google has added translation service for Malayalam language also. Using this facility you can translate text from all other language supported by google translator to Malayalam and vice versa. . Google provides this facility in their site https://translate.google.co.in/ . I t will be an easiest tool to learn languages

 https://translate.google.co.in/ .








It help you to type either in Malayalam or in another language and translate it. It allows the user to contribute their own translations to improve the output.

TCExam - an Open Source Computer-Based Assessment system

TCExam is an Open Source system for electronic exams (also know as CBA - Computer-Based Assessment, CBT - Computer-Based Testing or e-exam) that enables educators and trainers to author, schedule, deliver, and report on quizzes, tests and exams. It is developed and supported by Tecnick.com LTD

Following are the features of TCExam
  •     Open Source Software
  •     Community support
  •     Platform Independent
  •     No expensive hardware requirements
  •     Internationalization (I18N)
  •     Accessibility and Usability
  •     Data Import and Export
  •     Rich Content
  •     Unique test
  •     Paper Testing with Optical Character Recognition (OMR)
It is developed using PHP for scripting and MySQL / PostgreSQL as back-end database server. Its simple and interactive installation script will provide a wizard for easy installation. It can be setup on  XAMPP, WAMP or MAMP,  or you can configure after setting up either Apache web server  or Microsoft's IIS, and MySQL or PostgreSQL server on your system.

 TCExam and  required software can be downloaded by following the links



Wednesday, 19 November 2014

How to pass variable number of arguments to a function in PHP?

In certain cases, you may require to pass different number of arguments to a function in PHP scripts. Let us consider a case where you want to calculate average of a set of values using a function. Number of values in the set may vary in each call to the function. PHP provides a mechanism to pass different number of arguments to a function and process the arguments from the function by using the following functions.

func_num_args() - Returns the number of arguments passes to the function

func_get_args() - Returns the arguments passed to the function as an array

func_get_arg($arg_num) - Returns the argument at the $arg_num th position or index . Starting index is 0.



You can see the PHP implementation of the above problem here.

Tuesday, 18 November 2014

Which are the top 10 websites in the world?



As per alexa (www.alexa.com) ranking www.google.com, worlds No. 1 search engine, is the top ranked website in the world. Second place goes to www.facebook.com, the new generation social media website.

Here is the list of top 10 websites of the world as per the records of www.alexa.com.
 

How to create your own Forms with custom Paper Sizes for Printers in Windows?

Windows provides an option to create your own Forms / Paper Size like A4, A3, etc.  with custom size and name for printing. You can add new forms to the list of Forms that will be listed in the Paper Tab of Print Dialog Box. Here is the step by step procedure for creating your own Forms.

Monday, 17 November 2014

How to apply alternate background color for rows of an HTML table using CSS

In some cases you may want to apply alternate background color for each row of an HTML table.Using CSS3 it is easy to apply alternate color for rows in an HTML table.

Create styles using CSS3 and specify background color by using nth-child(even) and nth-child(odd) selectors.


You can see the CSS3 implementation of the above problem here.

How to apply alternate background color for rows of an HTML table using PHP Script

In some cases you may want to apply alternate background color for each row of an HTML table. Using a simple script you can solve the problem. You can just apply one color for every odd row and apply another color for every even row. Check whether current row is even or odd by using a rowcount variable and check the result of rowcount mod 2  (ie. rowcount % 2) is 0 or not and change the value of background color attribute accordingly.

If rowcount % 2 is 0 then it is a even row and apply one color for that, other wise it is odd row and apply another color.

  $bgcolor = "#ffffff";
   if ($i % 2 == 0)
              $bgcolor = "#ECD23F";




You can see the PHP implementation of the above problem here.

Monday, 10 November 2014

Page wise or paginated listing of data from a database table using PHP

This example demonstrates how data from a database table  is displayed as different pages  in a webpage.

In this example records per page is stored in a variable $pagesize and it is set to 15 (you can change it as per your requirements). For each page the query is selecting only the required 15 records and displays it. 
Starting record of the given  page ($pageno) is calculated by the following expression.

 $start_rec = (($pageno-1) * $pagesize) ;

Query to select required number of records starting from start_rec is given below. To specify the starting record and number of records in SQL query the LIMIT clause is used.

Wednesday, 5 November 2014

How to read, write, update and list data from a database using Java - JDBC

JDBC is a Java database connectivity technology to access databases. This technology is an API for the Java programming language that defines how a client may access a database. It provides methods for querying and updating data in a database A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source

The example given below demonstrate how to access a database using JDBC. In this example we uses three classes.

Sunday, 26 October 2014

Reading data from an HTML table using JavaScript

This example demonstrates how data in an HTML table can be read using JavaScript which can further be used for submitting to a server side script.

In this example, we first get the reference of the table using  the function call
var table = document.getElementById('productlist'); 
Then loop through each row of the table.
Each time data read from a cell is added to a string separated by a delimiter ( here we used "," ).
Finally the data read is displayed by using alert() function.

Entire code of this example is given below:

Friday, 17 October 2014

Use of final keyword in Java

In Java, the keyword final can be used in various contexts.  In all contexts, it prevents the modification or redefinition of the existing entity whether it is a variable, function or a class. Generally final keyword is used for the following purposes.

1.    To define constants
2.    To prevent overriding of methods
3.    To prevent inheritance

final keyword to define constants

If you want to define a constant, you can use the final keyword while declaring a variable and make it a constant.

PI is defined as a constant in the following example.

Thursday, 16 October 2014

Interface in Java

An interface can be treated as a class which contains only abstract methods (methods without body) and constants.  Interface is defined using the Java keyword interface. A class implements an interface by providing code for each method declared by the interface. By this mechanism an implementation rule or protocol is imposed on all classes they are implementing a particular interface or interfaces.

Sunday, 5 October 2014

Goodbye to Orkut!


 Google has  downed its social networking website Orkut  on September 30, 2014 and made the end of a 10-year run.

 Google says Goodbye to Orkut!

Recursive Function in C Language

Recursion is a method used to solve some problems where the solution depends on solutions to smaller instances of the same problem. The approach can be applied to types of problems for which solution can be represented recursively.

Friday, 3 October 2014

New Windows is Windows 10

Microsoft has disclosed that Windows 10 will be the next version of Windows. Windows 10 offers new experiences for customers to work, play and connect.

Windows 10 will run on broad set of devices like Mobile Gadgets, PCs and even Servers. And it is designed to use touch/pen, mouse/keyboard and controller/gesture.

And for all these devices, Microsoft provides uniform platform for application developers..
Microsoft includes the nostalgic start menu again in Windows 10 which was lost in Windows 8.

Thursday, 2 October 2014

What is an ER Diagram and how to draw it?


ER Diagram or Entity Relationship Diagram is a tool used to graphically represent the relational model of a database schema. It is a very important and useful tool that can be used in the Database Design phase of software development process to represent entities, relations, attributes, multiplicity, participation and constraints identified.  Various icons and notations are used to represent entities, relationships, attributes, multiplicity, participation and constraints are discussed below.

Tuesday, 30 September 2014

How to create a user defined exception in Java?

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program. When an error occurs within a method (member function), the method creates an object of the error/exception and hands it off to the runtime system.

Java provides better exception handling mechanism for its programmers. It simplifies the exception handling process and gives a try .... catch ... finally stucture to handle the exception.