Skip to main content

Posts

Showing posts from November, 2010

String handling functions in C

Like numbered arrays, C handles character arrays (strings). Each string is identified as a character array and ends with a ‘\0’(null) character (the compiler automatically adds the null character). So the end of string is identified as a null character. Being an array, all the elements of the string array stored in continuous memory locations following is the declaration of character array char name[50]; or char name[]=”Pradeep Kumar”; or char name[20]=”Hello Pradeep”; Strings can be handled or manipulated through loops or library functions. There are some library functions to handle strings are available at string.h Some of them are strlen(string)   This is to find the length of the string and returns an integer For example, if “Hello” is the string, the length will be 5 and “Hello “, the length is 6 (there is a blank space after o in hello) strcat(String1, string2) to concatenate two strings One string will be appended to a

Passing entire structure to a function

A function can accept a structure as a parameter and even it returns a parameter too. Either the individual structure elements can be passed or the entire structure can be passed. Here is the example, for passing the entire structure to a function #include <stdio.h> #include <conio.h> //Structure declaration struct student { char name[10]; int no; }; //Function prototypes void read(struct student); struct student display(struct student); //function which takes the entire structure as a parameter and it returns a structure also int main() {     struct student s1,s;      printf("Enter the details");     scanf("%s %d",s1.name,&s1.no);     s=display(s1);  //function call printf("Name is %s and number is %d",s.name,s.no);     getch(); } //Function definition struct student display(struct student s) { return s; }

Structures in C Programming

Structure in C groups different data type under a common name struct is a keyword to declare a structure The size of the structure depends on the size of the different data types Structure declaration always ends with a semicolon Example: struct employee { char name[50]; int empid; float salary; }; The total size occupied by the above structure is 50 + 2 + 4 = 56 bytes (on TC compiler) and 50 + 4 + 4= 58 (on other compilers like GCC, etc). Again the size depends on the compiler. GCC compiler always increase the size of structure which is divisible by 4 for faster code generation, so the above structure takes 60 bytes of memory Declaring a structure does not consume any memory, only when the structure object is created memory occupies Variables initialization will not be done during the structure declaration The above syntax declaration does not consume memory, but the following do int main() { struct employee e1;

C Program–Multiplying two matrices

/* Program to multiply two matrices */ #include <stdio.h> #include <conio.h> int main() {     int a[2][3],b[3][2],c[2][2],k,j,i;     printf("enter a");     for(i=0;i<2;i++)   //Get array A     {     for(j=0;j<3;j++)     {     scanf("%d",&a[i][j]);      }     }     printf("enter b");     for(i=0;i<3;i++) //Get array B     {     for(j=0;j<2;j++)     {     scanf("%d",&b[i][j]);     }     }     for(i=0;i<2;i++)      {     for(j=0;j<2;j++)     {     c[i][j]=0;  //to Hold a temporary multiplication     for(k=0;k<3;k++)     {     c[i][j]=c[i][j]+a[i][k]*b[k][j];   //Multiplication algorithm     }     }     }     printf("C is ");     for(i=0;i<2;i++)     for(j=0;j<2;j++)     printf(" c[%d][%d] - %d \n",i,j,c[i][j]);    getch();       return 0; }

C Program–Adding two matrices

/* Program to add two matrices */ #include <stdio.h> #include <conio.h> int main() {     int a[10][10], b[10][10],c[10][10],i,j;     printf("Enter a");     for(i=0;i<2;i++)  //get the matrix A         for(j=0;j<2;j++)     scanf("%d",&a[i][j]);     printf("Enter b");     for(i=0;i<2;i++) //get the matrix B         for(j=0;j<2;j++)     scanf("%d",&b[i][j]);         for(i=0;i<2;i++)     {         for(j=0;j<2;j++)         {         c[i][j] = a[i][j] +b[i][j];    //adding two matrices         }     }     printf("Added Matrix is \n");     for(i=0;i<2;i++)         for(j=0;j<2;j++)     printf("%d ",c[i][j]);         getch();         return 0; }

How to interpret the NS2 tracefile (manually) for wireless simulation

Assume you created a tcl file for a wireless simulation and it generates a trace file (usually .tr as extension). If any tracing softwares are not available, how to interpret manually, here is the step ACTION: [s|r|D]: s -- sent, r -- received, D – dropped WHEN: the time when the action happened   WHERE: the node where the action happened LAYER: AGT -- application, RTR -- routing, LL -- link layer (ARP is done here) IFQ -- outgoing packet queue (between link and mac layer) MAC -- mac, PHY – physical     flags:   SEQNO: the sequence number of the packet   TYPE: the packet type cbr -- CBR data stream packet DSR -- DSR routing packet (control packet generated by routing) RTS -- RTS packet generated by MAC 802.11 ARP -- link layer ARP packet SIZE: the size of packet at current layer, when packet goes down, size increases, goes up size decreases [a b c d]: a -- the packet duration in mac layer header b -- the mac address of destination c -- the mac address of source

C Program–To search a number in a given array

//This program is to search a given number in an array #include <stdio.h> #include <conio.h> int main() {     int a[10],i,num;     printf("enter the array elements");     for(i=0;i<10;i++) //get all the numbers     scanf("%d",&a[i]);     printf("Enter the number to search");     scanf("%d",&num);     for(i=0;i<10;i++)     {                      if(a[i]==num) //given num is matched in the array                      {                      printf("The number is found in the %d position",i+1);                      getch();                      exit(0); //to go the end of the program                      }     } printf("The number is not found"); //if num not found, this will be displayed getch(); return 0; }

C Program–To sort a Given set of numbers in ascending order (Bubble Sort)

/* Program to sort the given set of numbers in ascending order, this sorting is called as bubble sort algorithm */ #include <stdio.h> #include <conio.h> int main() {     int a[10],i,j,temp=0;     printf("Enter all the 10 numbers");     for(i=0;i<10;i++)     scanf("%d",&a[i]);     for(i=0;i<10;i++)  //This loop is for total array elements (n)     {     for(j=0;j<9;j++) //this loop is for total combinations (n-1)     {                     if(a[j]>a[j+1]) //if the first number is bigger then swap the two numbers                     {                     temp=a[j];                     a[j]=a[j+1];                     a[j+1]=temp;                     }     }     } printf("The ordered array is"); for(j=0;j<10;j++) //Finally print the ordered array printf("%d \t",a[j]); getch(); return 0; }