C Program to solve Knapsack problem

C Program to solve Knapsack problem. Knapsack problem is also called as rucksack problem. In Knapsack problem, given a set items with values and weights and a limited weight bag . We have to find the optimum solution so that, in minimum cost(value) fill the bag with the maximum weight. 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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
int w[10],p[10],v[10][10],n,i,j,cap,x[10]={0};
int max(int i,int j)
{
return ((i>j)?i:j);
}
int knap(int i,int j)
{
int value;
if(v[i][j]<0)
{
if(j<w[i])
value=knap(i-1,j);
else
value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i]));
v[i][j]=value;
}
return(v[i][j]);
}
void main()
{
int profit,count=0;
clrscr();
printf("nEnter the number of elementsn");
scanf("%d",&n);
printf("Enter the profit and weights of the elementsn");
for(i=1;i<=n;i++)
{
printf("For item no %dn",i);
scanf("%d%d",&p[i],&w[i]);
}
printf("nEnter the capacity n");
scanf("%d",&cap);
for(i=0;i<=n;i++)
for(j=0;j<=cap;j++)
if((i==0)||(j==0))
v[i][j]=0;
else
v[i][j]=-1;
profit=knap(n,cap);
i=n;
j=cap;
while(j!=0&&i!=0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
i--;
}
else
i--;
}
printf("Items included aren");
printf("Sl.notweighttprofitn");
for(i=1;i<=n;i++)
if(x[i])
printf("%dt%dt%dn",++count,w[i],p[i]);
printf("Total profit = %dn",profit);
getch();
}
Read more Similar C Programs
Data Structures

Learn C Programming

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

To get regular updates on new C programs, you can Follow @c_program

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!

(c) www.c-program-example.com

C Program to count number of characters in the file

C Program to count number of characters in the file. In this program you can learn c file operations. Here we counting the characters by reading the characters in the file one by one and if read character was not an ‘n’ ,’t’ or EOF, it increments the counter by one. 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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

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

void main()
{
char ch;
int count=0;
FILE *fptr;
clrscr();
fptr=fopen("text.txt","w");
if(fptr==NULL)
{
printf("File can't be createda");
getch();
exit(0);
}
printf("Enter some text and press enter key:n");
while((ch=getche())!='r')
{
fputc(ch,fptr);
}
fclose(fptr);
fptr=fopen("text.txt","r");
printf("nContents of the File is:");
while((ch=fgetc(fptr))!=EOF)
{
count++;
printf("%c",ch);
}
fclose(fptr);
printf("nThe number of characters present in file is: %d",count);
getch();
}

Read more Similar 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 Follow @c_program

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!

(c) www.c-program-example.com

C Program to implement HEAP sort

Data structures using C, Heap sort algorithm starts by building a heap from the given elements,and then heap removes its largest element from the end of partially sorted array. After removing the largest element, it reconstructs the heap, removes the largest remaining item, and places it in the next open position from the end of the partially sorted array. This is repeated until there are no items left in the heap and the sorted array is full. Elementary implementations require two arrays – one to hold the heap and the other to hold the sorted elements. 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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>


void heapsort(int[],int);
void heapify(int[],int);
void adjust(int[],int);




main()
{
int n,i,a[50];
system("clear");

printf("nEnter the limit:");
scanf("%d",&n);

printf("nEnter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

heapsort(a,n);

printf("nThe Sorted Elements Are:n");
for(i=0;i<n;i++)
printf("t%d",a[i]);
printf("n");
}

void heapsort(int a[],int n)
{
int i,t;

heapify(a,n);

for(i=n-1;i>0;i--)
{
t = a[0];
a[0] = a[i];
a[i] = t;
adjust(a,i);
}
}


void heapify(int a[],int n)
{
int k,i,j,item;

for(k=1;k<n;k++)
{
item = a[k];
i = k;
j = (i-1)/2;

while((i>0)&&(item>a[j]))
{
a[i] = a[j];
i = j;
j = (i-1)/2;
}
a[i] = item;
}
}

void adjust(int a[],int n)
{
int i,j,item;

j = 0;
item = a[j];
i = 2*j+1;

while(i<=n-1)
{
if(i+1 <= n-1)
if(a[i] <a[i+1])
i++;
if(item<a[i])
{
a[j] = a[i];
j = i;
i = 2*j+1;
}
else
break;
}
a[j] = item;
}

Read more Similar C Programs
Data Structures
C Sorting Techniques
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

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!

(c) www.c-program-example.com

C Program to implement quick sort

C Program to implement quick sort. Quick sort algorithm is based on divide and conquer strategy. In a quick sort we take the one element called as pivot,then we list all the smaller elements than pivot, and greater than pivot. after partitioning we have pivot in the final position. After recursively sorting the partition array, we get the sorted elements. 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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include<stdio.h>

void swap (int a[], int left, int right)
{
int temp;
temp=a[left];
a[left]=a[right];
a[right]=temp;
}//end swap

void quicksort( int a[], int low, int high )
{
int pivot;
// Termination condition!
if ( high > low )
{
pivot = partition( a, low, high );
quicksort( a, low, pivot-1 );
quicksort( a, pivot+1, high );
}
} //end quicksort

int partition( int a[], int low, int high )
{
int left, right;
int pivot_item;
int pivot = left = low;
pivot_item = a[low];
right = high;
while ( left < right )
{
// Move left while item < pivot
while( a[left] <= pivot_item )
left++;
// Move right while item > pivot
while( a[right] > pivot_item )
right--;
if ( left < right )
swap(a,left,right);
}
// right is final position for the pivot
a[low] = a[right];
a[right] = pivot_item;
return right;
}//end partition

// void quicksort(int a[], int, int);
void printarray(int a[], int);

int main()
{
int a[50], i, n;
printf("nEnter no. of elements: ");
scanf("%d", &n);
printf("nEnter the elements: n");
for (i=0; i<n; i++)
scanf ("%d", &a[i]);
printf("nUnsorted elements: n");
printarray(a,n);
quicksort(a,0,n-1);
printf("nSorted elements: n");
printarray(a,n);

}//end main


void printarray(int a[], int n)
{
int i;
for (i=0; i<n; i++)
printf(" %d ", a[i]);
printf("n");
}//end printarray

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 Follow @c_program

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!

(c) www.c-program-example.com

C Program to Implement the multiple priority queue.

Data structures using C,Priority QUEUE is a abstract data type in which the objects are inserted with respect to certain priority. In this program, we created the simple multiple priority queue. 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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<stdlib.h>
#define MAX 3
struct myqueue {
int f, r;
int a[MAX];
};
typedef struct myqueue pqueue;
void insert(pqueue *);
void delete(pqueue *);
void display(pqueue *, pqueue *, pqueue *);
int isempty(pqueue *);
int isfull(pqueue *);

main() {
pqueue q[3];
int ch, i, n;
for (i = 0; i < 3; i++) {
q[i].f = 0;
q[i].r = -1;
}
while (1) {
printf("n******* MENU ********n");
printf("1.Insertn2.Deleten3.Displayn4.Exit");
printf("nEnter your choice:n");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("nEnter priority:n");
scanf("%d", &n);
insert(&q[n - 1]);
break;

case 2:
if (isempty(&q[0])) {
if (isempty(&q[1])) {
if (isempty(&q[2])) {
printf("nAll queues are empty..n");
break;
} else
n = 2;
} else
n = 1;
} else
n = 0;
delete(&q[n]);
break;

case 3:
display(&q[0], &q[1], &q[2]);
break;
case 4:
exit(0);
default:
printf("nInvalid choice..");
break;
}
}
}

int isempty(pqueue *q) {
if (q->f > q->r)
return 1;
else
return 0;
}

int isfull(pqueue *q) {
if (q->r == MAX - 1)
return 1;
else
return 0;
}

void insert(pqueue *q) {
int x;
if (isfull(q)) {
printf("nQueue overflow..");
return;
} else {
printf("nEnter the element to be inserted:n");
scanf("%d", &x);
(q->r)++;
q->a[q->r] = x;
}
}

void delete(pqueue *q) {
int n;
n = q->a[q->f];
(q->f)++;
printf("The deleted element is = %d", n);
}

void display(pqueue *q1, pqueue *q2, pqueue *q3) {
int i;
if (isempty(q1))
printf("nQueue1 is empty..");
else
printf("nThe contents of queue1 are:n");
for (i = q1->f; i <= (q1->r); i++) {
printf("%dt", q1->a[i]);
}
if (isempty(q2))
printf("nQueue2 is empty..");
else
printf("nThe contents of queue2 are:n");
for (i = q2->f; i <= (q2->r); i++) {
printf("%dt", q2->a[i]);
}
if (isempty(q3))
printf("nQueue3 is empty..");
else
printf("nThe contents of queue3 are:n");
for (i = q3->f; i <= (q3->r); i++) {
printf("%dt", q3->a[i]);
}
}

/
Read more Similar C Programs
Data Structures

Learn C Programming

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

To get regular updates on new C programs, you can Follow @c_program

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!

(c) www.c-program-example.com