5 to 9
PLAINTEXT
loki
Public
5. min/max
#include <stdio.h>
int A[100];
int min, max;
void minmax(int i, int j)
{
int mid;
int newmin, newmax;
if (i == j)
{
min = max = A[i];
}
else if (i == j - 1)
{
if (A[i] < A[j])
{
min = A[i];
max = A[j];
}
else
{
min = A[j];
max = A[i];
}
}
else
{
mid = (i + j) / 2;
minmax(i, mid);
newmin = min;
newmax = max;
minmax(mid + 1, j);
if (min > newmin)
min = newmin;
if (max < newmax)
max = newmax;
}
}
int main()
{
int n, i;
printf("Enter the number of elements in array: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &A[i]);
}
minmax(0, n - 1);
printf("Minimum element = %d\n", min);
printf("Maximum element = %d\n", max);
return 0;
}
6. strassen matrix multi
#include <stdio.h>
int main()
{
int Z[2][2];
int i, j;
int A, B, C, D, E, F, G, H;
int p1, p2, p3, p4, p5, p6, p7;
int c1, c2, c3, c4;
int X[2][2] = {{1, 2}, {3, 4}};
int Y[2][2] = {{5, 6}, {7, 8}};
printf("The First Matrix is:");
for (i = 0; i < 2; i++)
{
printf("\n");
for (j = 0; j < 2; j++)
printf("%d\t", X[i][j]);
}
printf("\n\nThe Second Matrix is:");
for (i = 0; i < 2; i++)
{
printf("\n");
for (j = 0; j < 2; j++)
printf("%d\t", Y[i][j]);
}
A = X[0][0];
B = X[0][1];
C = X[1][0];
D = X[1][1];
E = Y[0][0];
F = Y[0][1];
G = Y[1][0];
H = Y[1][1];
p1 = A * (F - H);
p2 = H * (A + B);
p3 = E * (C + D);
p4 = D * (G - E);
p5 = (A + D) * (E + H);
p6 = (B - D) * (G + H);
p7 = (A - C) * (E + F);
c1 = p4 + p5 + p6 - p2;
c2 = p1 + p2;
c3 = p3 + p4;
c4 = p1 - p3 + p5 - p7;
Z[0][0] = c1;
Z[0][1] = c2;
Z[1][0] = c3;
Z[1][1] = c4;
printf("\n\nProduct achieved using Strassen's algorithm:");
for (i = 0; i < 2; i++)
{
printf("\n");
for (j = 0; j < 2; j++)
printf("%d\t", Z[i][j]);
}
return 0;
}
7. fractional knapsack
#include <stdio.h>
int main()
{
float weight[20], profit[20], capacity;
int n, i, j;
float ratio[20], tp = 0, temp, fract;
printf("\nEnter the capacity of knapsack: ");
scanf("%f", &capacity);
printf("\nEnter the no. of items: ");
scanf("%d", &n);
printf("\nEnter the weights and profits of each item:\n");
for (i = 0; i < n; i++)
{
scanf("%f%f", &weight[i], &profit[i]);
}
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[i];
ratio[i] = ratio[j];
ratio[j] = temp;
temp = weight[i];
weight[i] = weight[j];
weight[j] = temp;
temp = profit[i];
profit[i] = profit[j];
profit[j] = temp;
}
}
}
for (i = 0; i < n; i++)
{
if (weight[i] > capacity)
{
break;
}
else
{
capacity = capacity - weight[i];
tp = tp + profit[i];
}
}
if (i < n)
{
fract = capacity / weight[i];
tp = tp + (fract * profit[i]);
}
printf("\nMaximum profit is: %.2f\n", tp);
return 0;
}
8. all pair shortest path
#include <stdio.h>
#define n 4
#define INF 999
int main()
{
int A[n][n] = {
{0, 5, 9, INF},
{INF, 0, 1, INF},
{INF, INF, 0, 2},
{INF, 3, INF, 0}
};
int i, j, k;
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (A[i][k] + A[k][j] < A[i][j])
{
A[i][j] = A[i][k] + A[k][j];
}
}
}
}
printf("\nThe shortest distance from every pair of vertices are:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (A[i][j] == INF)
printf("%s\t", "INF");
else
printf("%d\t", A[i][j]);
}
printf("\n");
}
return 0;
}
9. LCS
#include <stdio.h>
#include <string.h>
char x[20], y[20], b[20][20];
void print_lcs(int i, int j)
{
if (i == 0 || j == 0)
return;
if (b[i][j] == 'c')
{
print_lcs(i - 1, j - 1);
printf("%c", x[i - 1]);
}
else if (b[i][j] == 'u')
{
print_lcs(i - 1, j);
}
else
{
print_lcs(i, j - 1);
}
}
int main(void)
{
int i, j, m, n, c[20][20];
printf("\nEnter the 1st sequence: ");
scanf("%19s", x);
printf("\nEnter the 2nd sequence: ");
scanf("%19s", y);
m = strlen(x);
n = strlen(y);
for (i = 0; i <= m; i++)
c[i][0] = 0;
for (j = 0; j <= n; j++)
c[0][j] = 0;
for (i = 1; i <= m; i++)
{
for (j = 1; j <= n; j++)
{
if (x[i - 1] == y[j - 1])
{
c[i][j] = c[i - 1][j - 1] + 1;
b[i][j] = 'c';
}
else if (c[i - 1][j] >= c[i][j - 1])
{
c[i][j] = c[i - 1][j];
b[i][j] = 'u';
}
else
{
c[i][j] = c[i][j - 1];
b[i][j] = 'l';
}
}
}
printf("\nThe Longest Common Subsequence is: ");
print_lcs(m, n);
printf("\n");
return 0;
}