Feb 16, 2012

C Program to implement address calculation sort.

Write a C Program to implement address calculation sort.
Address calculation sort is the sorting method which sorts the given array by using insertion method.
In this algorithm, a hash function is used and applied to the each key. Result of hash function is placed in the linked lists. The hash function must be a order preserving function which places the elements in linked lists are called as sub files. An item is placed into a sub-file in correct sequence by using any sorting method. After all the elements are placed into subfiles, the list is concatenated to produce the sorted list.
 Address calculation sort is used to efficiently store non-contiguous keys (account numbers, part numbers, etc.) that may have wide gaps in their alphabetic and numeric sequences.
Read more about C Programming Language .
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
 
#include<malloc.h>
 
#define MAX 20
 
struct  node
 
{
 
  int info ;
 
  struct node *link;
 
};
 
struct node *head[10];
 
int n,i,arr[MAX];
 
main()
 
{
 
  int i;
 
  printf("Enter the number of elements in the list : ");
 
  scanf("%d", &n);
 
  for(i=0;i<n;i++)
 
  {
 
    printf("Enter element %d : ",i+1);
 
    scanf("%d",&arr[i]);
 
  }/*End of for */
 
  printf("Unsorted list is :\n");
 
  for(i=0;i<n;i++)
 
    printf("%d  ",arr[i]);
 
  printf("\n");
 
  addr_sort();
 
  printf("Sorted list is :\n");
 
  for(i=0;i<n;i++)
 
    printf("%d  ",arr[i]);
 
  printf("\n");
 
}/*End of main()*/
 
addr_sort()
 
{
 
  int i,k,dig;
 
  struct node *p;
 
  int addr;
 
  k=large_dig();
 
  for(i=0;i<=9;i++)
 
    head[i]=NULL;
 
  for(i=0;i<n;i++)
 
  {
 
    addr=hash_fn( arr[i],k );
 
    insert(arr[i],addr);
 
  }
 
  for(i=0; i<=9 ; i++)
 
  {
 
    printf("head(%d) -> ",i);
 
    p=head[i];
 
    while(p!=NULL)
 
    {
 
      printf("%d ",p->info);
 
      p=p->link;
 
    }
 
    printf("\n");
 
  }
 
 
  i=0;
 
  for(k=0;k<=9;k++)
 
  {
 
    p=head[k];
 
    while(p!=NULL)
 
    {
 
      arr[i++]=p->info;
 
      p=p->link;
 
    }
 
  }
 
}/*End of addr_sort()*/
 
/*Inserts the number in sorted linked list*/
 
insert(int num,int addr)
 
{
 
  struct node *q,*tmp;
 
  tmp= malloc(sizeof(struct node));
 
  tmp->info=num;
 
  /*list empty or item to be added in begining */
 
  if(head[addr] == NULL || num < head[addr]->info)
 
  {
 
    tmp->link=head[addr];
 
    head[addr]=tmp;
 
    return;
 
  }
 
  else
 
  {
 
    q=head[addr];
 
    while(q->link != NULL && q->link->info < num)
 
      q=q->link;
 
    tmp->link=q->link;
 
    q->link=tmp;
 
  }
 
}/*End of insert()*/
 
/* Finds number of digits in the largest element of the list */
 
int large_dig()
 
{
 
  int large = 0,ndig = 0 ;
 
  for(i=0;i<n;i++)
 
  {
 
    if(arr[i] > large)
 
      large = arr[i];
 
  }
 
  printf("Largest Element is %d , ",large);
 
  while(large != 0)
 
  {
 
    ndig++;
 
    large = large/10 ;
 
  }
 
  printf("Number of digits in it are %d\n",ndig);
 
  return(ndig);
 
} /*End of large_dig()*/
 
hash_fn(int number,int k)
 
{
 
  /*Find kth digit of the number*/
 
  int digit,addr,i;
 
  for(i = 1 ; i <=k ; i++)
 
  {
 
    digit = number % 10 ;
 
    number = number /10 ;
 
  }
 
  addr=digit;
 
  return(addr);
 
}/*End of hash_fn()*/
 

 

Read more Similar C Programs

Data Structures


 C Sorting
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!



To browse more C Programs visit this link

C Program to solve Dining philosophers problem.

Write a C Program to solve Dining philosophers problem.
Dining philosophers problem is a classic synchronization problem.A problem introduced by Dijkstra concerning resource allocation between processes. Five silent philosophers sit  around table with a bowl of spaghetti. A fork is placed between each pair of adjacent philosophers.

Each philosopher must alternately think and eat.
Eating is not limited by the amount of spaghetti left: assume an infinite supply.
However, a philosopher can only eat while holding both the fork to the left and the fork to the right
(an alternative problem formulation uses rice and chopsticks instead of spaghetti and forks).

Each philosopher can pick up an adjacent fork, when available, and put it down, when holding it.
These are separate actions: forks must be picked up and put down one by one.
The problem is how to design a discipline of behavior (a concurrent algorithm) such that each philosopher won't starve, i.e. can forever continue to alternate between eating and thinking.
Read more about C Programming Language.


/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>
 
#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
#define LEFT (ph_num+4)%N
#define RIGHT (ph_num+1)%N
 
sem_t mutex;
sem_t S[N];
 
void * philospher(void *num);
void take_fork(int);
void put_fork(int);
void test(int);
 
int state[N];
int phil_num[N]={0,1,2,3,4};
 
int main()
{
    int i;
    pthread_t thread_id[N];
    sem_init(&mutex,0,1);
    for(i=0;i<N;i++)
        sem_init(&S[i],0,0);
    for(i=0;i<N;i++)
    {
        pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);
        printf("Philosopher %d is thinking\n",i+1);
    }
    for(i=0;i<N;i++)
        pthread_join(thread_id[i],NULL);
}
 
void *philospher(void *num)
{
    while(1)
    {
        int *i = num;
        sleep(1);
        take_fork(*i);
        sleep(0);
        put_fork(*i);
    }
}
 
void take_fork(int ph_num)
{
    sem_wait(&mutex);
    state[ph_num] = HUNGRY;
    printf("Philosopher %d is Hungry\n",ph_num+1);
    test(ph_num);
    sem_post(&mutex);
    sem_wait(&S[ph_num]);
    sleep(1);
}
 
void test(int ph_num)
{
    if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING)
    {
        state[ph_num] = EATING;
        sleep(2);
        printf("Philosopher %d takes fork %d and %d\n",ph_num+1,LEFT+1,ph_num+1);
        printf("Philosopher %d is Eating\n",ph_num+1);
        sem_post(&S[ph_num]);
    }
}
 
void put_fork(int ph_num)
{
    sem_wait(&mutex);
    state[ph_num] = THINKING;
    printf("Philosopher %d putting fork %d and %d down\n",ph_num+1,LEFT+1,ph_num+1);
    printf("Philosopher %d is thinking\n",ph_num+1);
    test(LEFT);
    test(RIGHT);
    sem_post(&mutex);
}
Read more Similar C Programs
C Basic

Search Algorithms.
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

Feb 15, 2012

C Program to demonstrate dynamic memory allocation example.

Write a C Program to demonstrate dynamic memory allocation example.
Dynamic memory allocation means you can allocate or relocate (manipulate) the memory at the run time, using malloc, calloc, and realloc functions.
Using malloc, We can allocate block of memory for a variable
Using calloc function, We can allocate multiple blocks of memory for a variable.
We can alter, reassign the allocated memory using the realloc function.
We can releasing the allocated memory using the free function. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/



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

int main() {
 int* grades;
 int sum = 0, i, numberOfStudents;
 float average;


 printf("Enter the number of students: ");
 scanf("%d", &numberOfStudents);
 getchar();

 if((grades = (int*) malloc(numberOfStudents * sizeof(int))) == NULL) {
  printf("\nError: Not enough memory to allocate grades array\n");
  exit(1);
 }

 printf("\nEnter the grades of %d students (in separate lines):\n", numberOfStudents);

 for(i = 0; i < numberOfStudents; i++) {
  scanf("%d", &grades[i]);
  getchar();
 }

 /* calculate sum */
 for(i = 0; i < numberOfStudents; i++)
  sum += grades[i];

 /* calculate the average */
 average = (float) sum / numberOfStudents;

 printf("\nThe average of the grades of all students is %.2f", 
   average);

 getchar();
 return(0);
}

Read more Similar C Programs
Learn C Programming

Number System
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

C program to generate and print prime numbers in a given range.

Write a C program to generate and print prime numbers in a given range. Also print the number of prime numbers.
Prime number is a whole number and greater than 1, which is divisible by one or itself.
Example: 2, 3, 5, 7, 11, 13............ Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

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

void main()
{
 int M, N, i, j, flag, temp, count = 0;

 clrscr();

 printf("Enter the value of M and N\n");
 scanf("%d %d", &M,&N);

 if(N < 2)
 {
  printf("There are no primes upto %d\n", N);
  exit(0);
 }
 printf("Prime numbers are\n");
 temp = M;

 if ( M % 2 == 0)
 {
  M++;
 }
 for (i=M; i<=N; i=i+2)
 {
  flag = 0;

  for (j=2; j<=i/2; j++)
  {
   if( (i%j) == 0)
   {
    flag = 1;
    break;
   }
  }
  if(flag == 0)
  {
   printf("%d\n",i);
   count++;
  }
 }
 printf("Number of primes between %d  and %d = %d\n",temp,N,count);
}



Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link

Feb 14, 2012

C Program to copy and concatenate strings without using standard functions.

C Program to copy and concatenate strings without using standard functions.
In this program , we copy one string from another, and without using the standard library function strcpy from string.h .
Here we appends the one string to another without using the strcat function. Read more about C Programming Language .


/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/



#include <stdio.h>

void newStrCpy(char *,char *);
void newStrCat(char *,char *);

void main()
{
 char str1[80],str2[80];
 int opn;
 do
 {
   printf("Press 1- NewStrCpy \t 2- NewStrCat\t 3- Exit\t Your Option?");
   scanf("%d",&opn);
   switch(opn)
   {
    case 1: flushall();
     printf("\n Read the Source String \n");
     gets(str1);
     newStrCpy(str2,str1);
     printf(" Copied String: %s\n",str2);
     break;
    case 2: flushall();
     printf(" Read the First String \n");
     gets(str1);
     printf(" Read the Second String \n");
     gets(str2);
     newStrCat(str1,str2);
     printf("Concatenated String: \n");
     puts(str1);
     break;
    case 3: printf(" Exit!! Press a key . . .");
     getch();
     break;
    default: printf(" Invalid Option!!! Try again !!!\n");
     break;
     }
 }while(opn != 3);
}   /* End of main() */

void newStrCpy(char *d,char *s)
 {
  while( (*d++ = *s++));
 }
void newStrCat(char *s, char *t)
 {
  while(*s)
  s++;
  while(*s++ = *t++);
 }

Read more c programs 
C Basic
C Strings
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link