sábado, 18 de enero de 2014

structure programs: C Program to Calculate Size of Structure using Sizeof Operator

#include<stdio.h>
#include<conio.h>
struct stud
{
int roll;
char name[10];
int marks;
};

void main()
{
int size;
struct stud s;
clrscr();
size = sizeof(s);
printf("nSize of Structure : %d",size);
getch();
}
Explanation :
size = sizeof(s);
Formula for Calculating Size of Structure :
Size of Structure 'S' = sizeof(roll) + sizeof(name) + sizeof(mark)
                      = 2 + 10 + 2
                      = 14

algorithms: Program to implement knapsack problem using greedy method

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

void knapsack(int n, float weight[], float profit[], float capacity)
{
 float x[20], tp= 0;
 int i, j, u;
 u=capacity;

 for (i=0;i<n;i++)
     x[i]=0.0;

 for (i=0;i<n;i++)
 {
 if(weight[i]>u)
      break;
 else
     {
     x[i]=1.0;
     tp= tp+profit[i];
     u=u-weight[i];
     }
 }

 if(i<n)
       x[i]=u/weight[i];

 tp= tp + (x[i]*profit[i]);

 printf("n The result vector is:- ");
 for(i=0;i<n;i++)
        printf("%ft",x[i]);

 printf("m Maximum profit is:- %f", tp);

}

void main()
{
 float weight[20], profit[20], capacity;
 int n, i ,j;
 float ratio[20], temp;
 clrscr();

 printf ("n Enter the no. of objects:- ");
 scanf ("%d", &num);

 printf ("n Enter the wts and profits of each object:- ");
 for (i=0; i<n; i++)
 {
 scanf("%f %f", &weight[i], &profit[i]);
 }

 printf ("n enter the capacityacity of knapsack:- ");
 scanf ("%f", &capacity);

 for (i=0; i<n; i++)
 {
 ratio[i]=profit[i]/weight[i];
 }

 for(i=0; i<n; i++)
 {
    for(j=i+1;j< n; j++)
    {
      if(ratio[i]<ratio[j])
      {
      temp= ratio[j];
      ratio[j]= ratio[i];
      ratio[i]= temp;

     temp= weight[j];
     weight[j]= weight[i];
     weight[i]= temp;

     temp= profit[j];
     profit[j]= profit[i];
     profit[i]= temp;
     }
   }
 }

 knapsack(n, weight, profit, capacity);
 getch();
}
Output :
Enter the no. of objects:- 7

Enter the wts and profits of each object:-
2 10
3 5
5 15
7 7
1 6
4 18
1 3

Enter the capacity of knapsack:- 15

The result vector is:- 1.000000        1.000000        1.000000        1.000000
    1.000000        0.666667        0.000000

Maximum profit is:- 55.333332

algorithms: C Progran to Implement N Queen's Problem using Backtracking

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

char a[10][10];
int n;

void printmatrix()
{
 int i,j;
 printf("n");
 for(i=0;i < n;i++)
       {
       for(j=0;j < n;j++)
            printf("%ct",a[i][j]);
       printf("nn");
       }
}

//------------------------------------------

int getmarkedcol(int row)
{
 int i,j;
 for(i=0;i < n;i++)
     if(a[row][i]=='Q')
     {
     return(i);
     break;
     }
}

//------------------------------------------

int feasible(int row, int col)
{
 int i,tcol;
 for(i=0;i < n;i++)
     {
     tcol=getmarkedcol(i);
         if(col==tcol || abs(row-i)==abs(col-tcol))
              return 0;
     }
 return 1;
}

//------------------------------------------

void nqueen(int row)
{
 int i,j;
 if(row < n)
 {
  for(i=0;i < n;i++)
     {
     if(feasible(row,i))
         {
         a[row][i]='Q';
         nqueen(row+1);
         a[row][i]='.';
         }
     }
 }
 else
 {
 printf("nThe solution is:- ");
 printmatrix();
 }
}

//---------------------------------

void main()
{
 int i,j;
 clrscr();

 printf("n Enter the no. of queens:- ");
 scanf("%d",&n);

 for(i=0;i < n;i++)
     for(j=0;j < n;j++)
  a[i][j]='.';

 nqueen(0);
 getch();
}
Output :
Enter the no. of queens:- 4

The solution is:-
.       Q       .       .

.       .       .       Q

Q       .       .       .

.       .       Q       .


The solution is:-
.       .       Q       .

Q       .       .       .

.       .       .       Q

.       Q       .       .

algorithms: C Program to implement prims algorithm using greedy method

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

int n, cost[10][10];

void prim()
{
 int i,j,k,l,x,nr[10],temp,min_cost=0,tree[10][3];
 /* For first smallest edge */
 temp=cost[0][0];
 for(i=0;i< n;i++)
 {
     for(j=0;j< n;j++)
     {
        if(temp>cost[i][j])
        {
        temp=cost[i][j];
        k=i;
        l=j;
        }
     }
 }
 /* Now we have fist smallest edge in graph */
 tree[0][0]=k;
 tree[0][1]=l;
 tree[0][2]=temp;
 min_cost=temp;

 /* Now we have to find min dis of each 
vertex from either k or l
by initialising nr[] array 
*/

 for(i=0;i< n;i++)
    {
    if(cost[i][k]< cost[i][l])
        nr[i]=k;
    else
        nr[i]=l;
    }
 /* To indicate visited vertex initialise nr[] for them to 100 */
 nr[k]=100;
 nr[l]=100;
 /* Now find out remaining n-2 edges */
 temp=99;
 for(i=1;i< n-1;i++)
    {
    for(j=0;j< n;j++)
       {
       if(nr[j]!=100 && cost[j][nr[j]] < temp)
           {
           temp=cost[j][nr[j]];
           x=j;
           }
       }
 /* Now i have got next vertex */
 tree[i][0]=x;
 tree[i][1]=nr[x];
 tree[i][2]=cost[x][nr[x]];
 min_cost=min_cost+cost[x][nr[x]];
 nr[x]=100;

 /* Now find if x is nearest to any vertex 
than its previous near value */

    for(j=0;j< n;j++)
    {
    if(nr[j]!=100 && cost[j][nr[j]] > cost[j][x])
         nr[j]=x;
    }
    temp=99;
 }
 /* Now i have the answer, just going to print it */
 printf("n The min spanning tree is:- ");
 for(i=0;i< n-1;i++)
    {
    for(j=0;j< 3;j++)
           printf("%d", tree[i][j]);
    printf("n");
    }

 printf("n Min cost:- %d", min_cost);
}

//////////////////////////////////////////////
void main()
{
 int i,j;
 clrscr();

 printf("n Enter the no. of vertices:- ");
 scanf("%d", &n);

 printf ("n Enter the costs of edges in matrix form:- ");
 for(i=0;i< n;i++)
     for(j=0;j< n;j++)
          scanf("%d",&cost[i][j]);

 printf("n The matrix is:- ");
    for(i=0;i< n;i++)
    {
      for(j=0;j< n;j++)
           printf("%dt",cost[i][j]);
      printf("n");
    }
 prim();
 getch();
}
Output :

Enter the no. of vertices:- 3
Enter the costs of edges in matrix form:- 99 2 3 2 99 5 3 5 99 The matrix is:- 99 2 3 2 99 5 3 5 99 The min spanning tree is:- 0 1 2 2 0 3 Min cost:- 5