Hard_C_Programs (1)
Hard_C_Programs (1)
#include <stdio.h>
#include <stdbool.h>
#define N 9
int main() {
int grid[N][N] = {
{5,3,0,0,7,0,0,0,0},{6,0,0,1,9,5,0,0,0},{0,9,8,0,0,0,0,6,0},
{8,0,0,0,6,0,0,0,3},{4,0,0,8,0,3,0,0,1},{7,0,0,0,2,0,0,0,6},
{0,6,0,0,0,0,2,8,0},{0,0,0,4,1,9,0,0,5},{0,0,0,0,8,0,0,7,9}
};
if (solveSudoku(grid, 0, 0))
printGrid(grid);
else
printf("No solution exists");
return 0;
}
2. Knight's Tour Problem
#include <stdio.h>
#define N 8
int solveKTUtil(int x, int y, int movei, int sol[N][N], int xMove[], int yMove[]) {
if (movei == N*N)
return 1;
for (int k = 0; k < 8; k++) {
int next_x = x + xMove[k], next_y = y + yMove[k];
if (isSafe(next_x, next_y, sol)) {
sol[next_x][next_y] = movei;
if (solveKTUtil(next_x, next_y, movei+1, sol, xMove, yMove))
return 1;
else
sol[next_x][next_y] = -1;
}
}
return 0;
}
void solveKT() {
int sol[N][N], xMove[8] = {2,1,-1,-2,-2,-1,1,2}, yMove[8] = {1,2,2,1,-1,-2,-2,-1};
for (int x = 0; x < N; x++)
for (int y = 0; y < N; y++)
sol[x][y] = -1;
sol[0][0] = 0;
if (!solveKTUtil(0, 0, 1, sol, xMove, yMove))
printf("Solution does not exist");
else
printSolution(sol);
}
int main() {
solveKT();
return 0;
}
3. Huffman Coding for Data Compression
#include <stdio.h>
#include <stdlib.h>
struct MinHeapNode {
char data;
unsigned freq;
struct MinHeapNode *left, *right;
};
struct MinHeap {
unsigned size, capacity;
struct MinHeapNode** array;
};
int main() {
char arr[] = {'a','b','c','d','e','f'};
int freq[] = {5,9,12,13,16,45};
HuffmanCodes(arr, freq, 6);
return 0;
}
4. LRU Cache Implementation (Linked List)
#include <stdio.h>
#include <stdlib.h>
void display() {
Node* temp = front;
while (temp) {
printf("%d ", temp->key);
temp = temp->next;
}
printf("\n");
}
int main() {
enqueue(1); enqueue(2); enqueue(3);
display();
enqueue(4); // LRU: 1 removed
display();
return 0;
}
5. Multithreaded Matrix Multiplication
#include <pthread.h>
#include <stdio.h>
#define SIZE 2
int A[SIZE][SIZE] = {{1,2},{3,4}}, B[SIZE][SIZE] = {{5,6},{7,8}}, C[SIZE][SIZE];
void* multiply(void* arg) {
int i = *(int*)arg;
for (int j = 0; j < SIZE; j++) {
C[i][j] = 0;
for (int k = 0; k < SIZE; k++)
C[i][j] += A[i][k] * B[k][j];
}
return NULL;
}
int main() {
pthread_t threads[SIZE];
int thread_ids[SIZE];
for (int i = 0; i < SIZE; i++) {
thread_ids[i] = i;
pthread_create(&threads[i], NULL, multiply, &thread_ids[i]);
}
for (int i = 0; i < SIZE; i++) pthread_join(threads[i], NULL);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++)
printf("%d ", C[i][j]);
printf("\n");
}
return 0;
}
6. Expression Tree Evaluation
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
Node* root = newNode('*');
root->left = newNode('+');
root->right = newNode('+');
root->left->left = newNode('3');
root->left->right = newNode('2');
root->right->left = newNode('4');
root->right->right = newNode('5');
printf("Result = %d\n", eval(root));
return 0;
}
7. File System Simulation
#include <stdio.h>
#include <string.h>
struct Dir {
char name[10];
struct Dir* sub[5];
int sub_count;
};
int main() {
struct Dir root;
strcpy(root.name, "root");
root.sub_count = 0;
create(&root, "home");
create(&root, "var");
list(&root);
return 0;
}
8. Longest Common Subsequence (DP)
#include <stdio.h>
#include <string.h>
int main() {
char X[] = "AGGTAB", Y[] = "GXTXAYB";
printf("LCS length is %d", lcs(X, Y, strlen(X), strlen(Y)));
return 0;
}
9. Count Set Bits (Kernighan's Algorithm)
#include <stdio.h>
int countSetBits(int n) {
int count = 0;
while (n) {
n &= (n - 1);
count++;
}
return count;
}
int main() {
int n = 29;
printf("Set bits in %d is %d\n", n, countSetBits(n));
return 0;
}
10. Simple Shell (Command Interpreter)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main() {
char input[MAX];
char *args[10];
while (1) {
printf("shell> ");
fgets(input, MAX, stdin);
input[strcspn(input, "\n")] = 0;
if (strcmp(input, "exit") == 0) break;
int i = 0;
args[i] = strtok(input, " ");
while (args[i] != NULL) args[++i] = strtok(NULL, " ");
if (fork() == 0) execvp(args[0], args);
else wait(NULL);
}
return 0;
}