Skip to content

Commit ddcd45e

Browse files
committed
lab programs
1 parent 4753887 commit ddcd45e

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

NQueens.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<math.h>
4+
5+
int a[30], count = 0;
6+
7+
// Function prototypes
8+
int place(int pos);
9+
void print_sol(int n);
10+
void queen(int n);
11+
12+
// Function to check if a queen can be placed in a particular position
13+
int place(int pos) {
14+
int i;
15+
for (i = 1; i < pos; i++) {
16+
if ((a[i] == a[pos]) || (abs(a[i] - a[pos]) == abs(i - pos)))
17+
return 0;
18+
}
19+
return 1;
20+
}
21+
22+
// Function to print the board configuration for a solution
23+
void print_sol(int n) {
24+
int i, j;
25+
count++;
26+
printf("\n\nSolution #%d:\n", count);
27+
for (i = 1; i <= n; i++) {
28+
for (j = 1; j <= n; j++) {
29+
if (a[i] == j)
30+
printf("Q\t");
31+
else
32+
printf("*\t");
33+
}
34+
printf("\n");
35+
}
36+
}
37+
38+
// Function to find all solutions to the N-Queens problem
39+
void queen(int n) {
40+
int k = 1;
41+
a[k] = 0;
42+
while (k != 0) {
43+
do {
44+
a[k]++;
45+
} while ((a[k] <= n) && !place(k));
46+
47+
if (a[k] <= n) {
48+
if (k == n)
49+
print_sol(n);
50+
else {
51+
k++;
52+
a[k] = 0;
53+
}
54+
}
55+
else
56+
k--;
57+
}
58+
}
59+
60+
int main() {
61+
int n;
62+
printf("Enter the number of Queens\n");
63+
scanf("%d", &n);
64+
queen(n);
65+
printf("\nTotal solutions = %d\n", count);
66+
return 0;
67+
}

NQueens.exe

41.2 KB
Binary file not shown.

SumOfSubset.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <stdio.h>
2+
3+
// Function prototypes
4+
void printSubset(int subset[], int size);
5+
void findSubsets(int set[], int n, int target);
6+
7+
// Function to print the current subset
8+
void printSubset(int subset[], int size) {
9+
printf("{ ");
10+
for (int i = 0; i < size; i++) {
11+
printf("%d ", subset[i]);
12+
}
13+
printf("}\n");
14+
}
15+
16+
// Function to find subsets that sum to the given target
17+
void findSubsets(int set[], int n, int target) {
18+
int subset[n];
19+
int totalSubsets = 1 << n; // Total number of subsets (2^n)
20+
21+
// Iterate over all possible subsets
22+
for (int i = 0; i < totalSubsets; i++) {
23+
int sum = 0;
24+
int subsetSize = 0;
25+
26+
// Check which elements are included in the current subset
27+
for (int j = 0; j < n; j++) {
28+
if (i & (1 << j)) {
29+
subset[subsetSize++] = set[j];
30+
sum += set[j];
31+
}
32+
}
33+
34+
// If the sum of the current subset is equal to the target, print the subset
35+
if (sum == target) {
36+
printSubset(subset, subsetSize);
37+
}
38+
}
39+
}
40+
41+
int main() {
42+
int set[10], n, target;
43+
44+
// Input number of elements in the set
45+
printf("Enter the number of elements in the set: ");
46+
scanf("%d", &n);
47+
48+
// Input elements of the set
49+
printf("Enter the elements of the set: ");
50+
for (int i = 0; i < n; i++) {
51+
scanf("%d", &set[i]);
52+
}
53+
54+
// Input the target sum
55+
printf("Enter the target sum: ");
56+
scanf("%d", &target);
57+
58+
// Find and print subsets that sum to the target
59+
findSubsets(set, n, target);
60+
61+
return 0;
62+
}

SumOfSubset.exe

40.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)