Tuesday 24 December 2013

Sharing User Defined C Header Files in Linux


  In C/C++ header files are used to store common code so that they can be reused at a later stage. Header files are having an extension .h in C/C++. In this post I am discussing the different ways to share a user defined C header file in Linux.

1.  The first technique is to store the header file in the current directory along with the C program accessing the header file. In this case the header file name is included by using Double Quotes instead of angle brackets.

For example the name of the header file be myheader.h. This header file can be included in a C program as shown below,

#include "myheader.h"

2. Another technique is to store the header file in a separate directory and add the whole path while including the header file. Here also the include statement uses Double Quotes instead of angle brackets. 

For example the header file myheader.h is stored in the directory /home/myheaders. Then this header file can be included in a C program as shown below,

#include "/home/myheaders/myheader.h"

3. Finally if you are satisfied with the quality of your header file you can add it to the standard header directories in Linux. The two standard header directories in Linux are,
  • /user/local/include - This directory is used to store header files for third-party libraries.
  • /usr/include - This directory is used to store Operating System Header files.  
For example if the header file myheader.h is copied into one of the two standard header directories mentioned above, then the header file can be included in a C program as shown below,

#include <myheader.h>      

Here Angle Brackets are used in the include statement instead of double quotes. Hence the compiler searches for the header file only in the standard header libraries. 

Saturday 30 November 2013

C Program to Perform Matrix addition Using Dynamic Memory Allocation


  This post provides the C Program to add two matrices. The difference with this program is that memory for the matrices is allocated dynamically. This elegant program was written by a friend and that too by taking just a few minutes. 

Figure Below Shows a Sample Output

  Let int * a; be a pointer, r1 be the number of rows of the matrix and c1 be the number of columns of the matrix, then memory allocation is done as follows.

a=(int *)malloc(r1*c1*sizeof(int));


Friday 29 November 2013

LaTeX: Presentation on Miscellaneous Topics


  This post provides a presentation on miscellaneous LaTeX topics. The presentation discusses a few useful topics in LaTeX. Download the file and view it in full screen mode or presentation mode.

Click here to download the Presentation.

Click here to download watermarking.tex.

Click here to download href.tex.

Click here to download newcommand.tex.

LaTeX: Presentation on Beamer


  This post provides a presentation on Beamer. Beamer is a LaTeX document class used to create presentations. The presentation might be sufficient for Beginners in Beamer. Download the presentation and view it in full screen mode or presentation mode.

LaTeX: Presentation on Math Mode


  This post provides a presentation on the Math mode of LaTeX. The contents discussed in the presentation gives an introduction to the basics of Math mode. For LaTeX Beginners this might be a good introduction to Math mode. Download the presentation and view it in full screen mode or presentation mode.

Click here to download the Presentation.

C Program to Add Two Matrices Using Pointers


  The C Program given with this post finds the sum of two matrices using pointers.
The prototype of function addmat is shown below.

 int (*addmat(int (*p)[MAX], int (*q)[MAX]))[MAX];

The function addmat accepts two pointers to one dimensional arrays with size MAX as arguments and returns a pointer to a one dimensional array with size MAX.

Figure Below Shows a Sample Output


C Program to Print the Upper and Lower Triangles of a Matrix


  The C Program provided with this post prints the upper and lower triangles of a matrix.

Figure Below Shows a Sample Output


Wednesday 23 October 2013

C Program to Evaluate Functions Using Simpson's Rule


  The Program provided in this post evaluates the definite integrals of sin(x), cos(x) and ex. This program is of no serious use. It simply illustrates the working of Simpson's Rule. Simpson's Rule is a method for numerical integration which helps us to find the numerical approximation of definite integrals like the one shown below.
Click here to view the Wikipedia Article on Simpson's Rule.

Figure Below Shows a Sample Output


C Program to Evaluate Functions Using Trapezoidal Rule


  The following Program evaluates the definite integrals of sin(x), cos(x) and ex. I have to confess that the program does not serve any practical purpose other than illustrating the working of Trapezoidal Rule. Trapezoidal Rule is a technique for approximating the definite integral shown below.
Click here to view the Wikipedia Article on Trapezoidal Rule.

Figure Below Shows a Sample Output

Saturday 19 October 2013

NS2 : Simulating Link State Routing


  The post provides the NS2 Script (TCL Script) to simulate a wired network using Link State Routing Protocol.

Execution of the Script

Network Topology in NAM

Data Transfer & Link Failure

Data Transfer through Alternate Path

Second Link Fails & Data Transfer through Alternate Path



NS2 : Simulating Distance Vector Routing


  The post provides the NS2 Script (Tcl Script) to simulate a wired network using Distance Vector Routing Protocol.

Execution of the Script

NAM Showing the Network Topology

Data Transfer in NAM

First Link Fails

Data Transfer through Alternate Path

Second Link Fails & Data Transfer through Alternate Path



Sunday 15 September 2013

NS2 : Simulating TCP in Wireless Networks


   This post provides the NS2 Script (Tcl Script) to simulate a wireless network using TCP.

Execution of the Script

Topology in NAM

Data Transfer in NAM


NS2 : Simulating UDP in Wireless Networks


   This post provides the NS2 Script (Tcl Script) to simulate a Wireless Network using UDP.

Execution of the Script

Topology in NAM


Data Transfer in NAM


OTcl Class Program


   OTcl refers to the object oriented extension of TclIt is used in Network Simulator (NS2). This post provides an example program involving OTcl Classes.

Program Execution


Tcl Program to Find the Square of a Number


      This post discusses the Tcl program to find the Square of a Number. 

Program Execution




NS2 : Simulating a Network using SFQ Queue Management Algorithm


   In this post the NS2 Script (TCL Script) to simulate a wired network using SFQ (Stochastic Fairness Queuing) queue management algorithm is provided.

Executing the TCL Script

Topology in NAM

Data Transfer in NAM

   
Click here to download the NS2 Script.

NS2 : Simulating Multicast in Wired Networks


      In this post I have added the NS2 Script (TCL Script) to simulate multicast in wired networks.   

Executing the TCL Script

Topology Shown in NAM

Data Transfer in NAM

Data Transfer in NAM


Thursday 5 September 2013

NS2 : Simulating a Network using RED Queue Management Algorithm


   In this post the NS2 Script (TCL Script) to simulate a wired network using RED (Random Early Discard) queue management algorithm is provided.

Executing the TCL Script

Topology in NAM Animator

NAM Showing Packet Drop



NS2 : Simulating a Network using Drop Tail Queue Management Algorithm


   This post contains NS2 Script (TCL Script) to simulate a wired network using Drop Tail queue management algorithm.

Executing the TCL Script

NAM Animator Showing the Topology

NAM Showing Data Transfer 



Tuesday 20 August 2013

NS2 : Wireless Simulation 2


   This post contains NS2 Script (TCL Script) to simulate a simple Wireless Network. The simulation involves wireless nodes which move from one position to another occasionally. 

Executing the TCL Script

Initial Node Position

Node Position after Movement



Monday 12 August 2013

NS2 : Wireless Simulation 1


   In this post I have included the TCL Script for wireless simulation using NS2. Xgraph utility is used to plot the graph of Congestion Window VS Time.

Executing the TCL Script

NAM Window Showing the Nodes

NAM Window Showing the Traffic

Graph Plotted Using Xgraph


Wednesday 7 August 2013

NS2 : Xgraph Utility Example


   In this post I have included an NS2 Script (TCL Script) calling the Xgraph utility to plot the graph.

The Xgraph utility is called by the following line of code in the TCL Script,

exec xgraph out0.tr out1.tr out2.tr -geometry 800x400 &


The three files out0.tr, out1.tr and out2.tr contains the input data for Xgraph. To learn about the data format of Xgraph input files read my post Xgraph for plotting in NS-2.

Executing The TCL Script

NAM Window Showing the Simulated Network

NAM Showing Traffic Flow

Graph Plotted Using Xgraph