Skip to main content

Posts

Showing posts from October, 2010

Installing Omnet in Ubuntu 10.10

To install Omnet on Ubuntu 10.10, the following are necessary Java (jdk)Gcc Compiler / G++ Compilerzlibraries and other componentsHere are the instructionsDownload Omnet from http://www.omnetpp.org/  (the latest edition is 4.1)Download the omnet-xxx.tgz file (its for linux)Open the terminal, untar it to any folder in linux (in my case it is /home/pradeepkumar/) using the following command tar zxvf omnet-xxx.tgzgo to the folder by this command cd omnet-xx/ (the above command creates a folder and untar all the contents into it)execute the command ./configure you may get some errors like unavailability of packages and dependencies, execute the following command to download and install all the packagesfor running the following command, your machine has to be connected to the internet….sudo apt-get install bison flex byacc zlib1g-dev tcl tk tcl8.4 tk8.4 tcl8.4-dev tk8.4-dev build-essentials libxmu-devonce all are installed, type again ./configure (now this command will give some informatio…

checking system version (for dynamic loading)...

If you encounter the following error,  checking system version (for dynamic loading)... ./configure: 1: Syntax error: Unterminated quoted string
then do the following, Open the following files under ns-allinone-2.xx/tcl: file tcl8.x.x/unix/tcl.m4
tk: file tk8.x.x/unix/tcl.m4
otcl: file otcl-1.x/configure.in
In all the above files, you can find the following line
system=MP-RAS-`awk ' { print $3 } '/etc/.relid'`
and change the above line to (just remove the last but one single quote, remember that ‘ is different from ` you need to remove only ‘ and not `)system=MP-RAS-`awk ' { print $3 } '/etc/.relid`

C Program–Passing an entire array to a function

/* This program is to pass the entire array to a function */#include <stdio.h>
#include <conio.h>
int maximum(int[],int); //function prototype, 1st parameter is array and the second is integerint main()
{
    int a[100],i,n,max=0;
    printf("Enter the numbers"); 
    scanf("%d",&n); //get the index number
    for(i=0;i<n;i++) //get the array elements
    {
    scanf("%d",&a[i]);
    }
    max=maximum(a,n); //function call
    printf("Maximum number is %d",max);
    getch();
    return 0;
}int maximum(int b[],int a) //function implementation
{
    int i,max=b[0];
    for(i=0;i<a;i++)
    if(b[i]>max)
    max=b[i];
    return max;
}
In the function call iemax=maximum(a,n) where a is the array and n is the number of array elementsthe above line can be written as max = maximum(&a[0],n)since the array is just a memory address, it is …

C Program–Arrays with Functions (passing Array elements to function)

/* Program to demonstrate arrays with function in which the array elements are passed as function parameters*/#include <stdio.h>
#include <conio.h>
void display(int); //function prototype
int main()
{
    int a[10],i;
    for(i=0;i<10;i++) //getting the array elements
    {
    scanf("%d",&a[i]);
    }
    for(i=0;i<10;i++)
        display(a[i]); //call the function inside the loop, this function called for 10 times
    getch();
    return 0;
}void display(int a) //function implementation to print the elements, here the parameter is an integer only(means we are passing the array elements)
{
     printf("%d",a);
}

Arrays in C

Arrays in C programming starts with memory addresses. Yes the compiler handles arrays as memory addresses. Similar elements group together with a single name is called arraythe indexing always starts with 0 and ends at n-1 (if the size of the array is n)Array elements stores in consecutive memory location.The array elements usually stores in consecutive memory location. For example,int a[10];  //Array with size 10the above example is an integer array with size 10 or this array can hold 10 variables.The elements starts from a[0], a[1], a[2]…..a[9], so totally there are 10 elements. Array elementsvaluesmemory address locationa[0]32600AH  (This address is called as the base address)a[1]34600EHa[2]236012Ha[3]356016Ha[4]45601AHa[5]67601EHa[6]566022Ha[7]16026Ha[8]34602AHa[9]25602EHThe memory addresses shown above are in hexadecimal, that’s why each address suffixed with a letter H. The memory addresses in each machine will be different.The base address is the address where the first element…

Functions in C

Almost all the programming languages uses functions. Functions are the entities which are grouping a set of statements which do a specific job or set of jobs.Example: sum of integers, sum of float number, complex number addition.All the above three can be implemented as a single function or three separate functions.When someone wants to use that, a function can be simply called.So function implementation happens asFunction prototype or Function declarationFunction definition or function implementationFunction callFunction prototypeIt is necessary to specify the name of the function, the parameters and the return type to the compiler that the function is being defined in this program.The prototype ends with a semicolonsyntax:int sum(int, int); //function prototypeFunction DefinitionThis is the actual function definition which shows the function implementation.for the above syntax here is the functionint sum(int a, int b){int c;c=a+b;return c;}The above function is returning an integer,…

C Program 4–To check whether a given number is prime or not

A prime number can be divided by 1 and itself, there are no other divisors,Examples are : 2 3, 5, 7, 11, …..To find out whether a given number is prime or not, here is the logic1. Get the number2. divide the given number from 2 to n-1 (Example if 6 is the number divided by 2,3,4,5 will get the remainder respectively 0,0,2,3)3. increment a counter to 1 if the remainder is 04. if there counter variable is 0, then the given number is prime (because we didn’t get any remainder) else non prime Here is the program#include <stdio.h>
#include <conio.h>int main()
{
    int a,i,count=0;
    printf("enter a"); //Let the given number is a
    scanf("%d",&a); //get the number
    for(i=2;i<a;i++) //divide the number a from 2 to a-1
    {
    if(a%i==0) 
    count++; //increment a counter if the divisibility is 0
    }
    if(count !=0) //if the counter is not zero, then prime
    printf("a is not a prime number"…

C Program 3 – To check whether a number is Armstrong Number

The armstrong number is of the form 153= 13 + 53 + 33The input is : 153 or any other numberoutput: The number is armstrong or not.Processing: take 153 as an example, remove 3, 5 and 1 in the reverse order (using % operator) and take the power of 3 and add to the sum variable. if the total sum and the original number, both are same, then that is the arm strong number.if else, the number is not an armstrong number#include <stdio.h>
#include <conio.h>
int main()
{
    int original_num, check, temp, sum=0;
    printf("Enter the number to check for armstrong number");
    scanf("%d", &original_num);
// Get the original number
    temp=original_num;
    while(original_num>0)
//run the loop till the number becomes 0
    {
     check=original_num%10; 
//remove the last digit using modulo operator
     sum=sum+check*check*check; //the last digit is taken power to 3 and added to sum�����…