0% found this document useful (0 votes)
3 views3 pages

Assignment Solutions

The document contains solutions to four programming assignments in C. It includes a recursive function to print a number pattern, a function to reverse an array in-place using pointers, a function to reverse a string in-place, and a solution to the 4x4 N-Queens problem using backtracking. Each solution is accompanied by code snippets and explanations of the logic used.

Uploaded by

Kunal Dhiman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Assignment Solutions

The document contains solutions to four programming assignments in C. It includes a recursive function to print a number pattern, a function to reverse an array in-place using pointers, a function to reverse a string in-place, and a solution to the 4x4 N-Queens problem using backtracking. Each solution is accompanied by code snippets and explanations of the logic used.

Uploaded by

Kunal Dhiman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment Solutions

1. Recursive Function to Print a Number Pattern


#include <stdio.h>

void printPattern(int n) {
if (n == 0)
return;

for (int i = 1; i <= n; i++)


printf("%d ", i);
printf("\n");

printPattern(n - 1);
}

int main() {
int n = 3;
printPattern(n);
return 0;
}
2. Reverse an Array In-Place Using Only Pointers
#include <stdio.h>

void reverseArray(int *arr, int size) {


int *start = arr;
int *end = arr + size - 1;

while (start < end) {


int temp = *start;
*start = *end;
*end = temp;

start++;
end--;
}
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);

reverseArray(arr, n);

for (int i = 0; i < n; i++)


printf("%d ", arr[i]);

return 0;
}
3. Reverse a String In-Place
#include <stdio.h>
#include <string.h>
void reverseString(char *str) {
char *start = str;
char *end = str + strlen(str) - 1;

while (start < end) {


char temp = *start;
*start = *end;
*end = temp;

start++;
end--;
}
}

int main() {
char str[] = "hello";
reverseString(str);
printf("Reversed String: %s\n", str);
return 0;
}
4. Solve 4x4 N-Queens Problem Using Backtracking
#include <stdio.h>
#include <stdbool.h>

#define N 4

void printSolution(int board[N][N]) {


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
printf(board[i][j] ? "Q " : ". ");
printf("\n");
}
printf("\n");
}

bool isSafe(int board[N][N], int row, int col) {


for (int i = 0; i < col; i++)
if (board[row][i]) return false;

for (int i=row, j=col; i>=0 && j>=0; i--, j--)


if (board[i][j]) return false;

for (int i=row, j=col; i<N && j>=0; i++, j--)


if (board[i][j]) return false;

return true;
}

bool solveNQueens(int board[N][N], int col) {


if (col >= N)
return true;
for (int i = 0; i < N; i++) {
if (isSafe(board, i, col)) {
board[i][col] = 1;

if (solveNQueens(board, col + 1))


return true;

board[i][col] = 0; // backtrack
}
}

return false;
}

int main() {
int board[N][N] = {0};

if (solveNQueens(board, 0))
printSolution(board);
else
printf("No solution exists\n");

return 0;
}
N-Queens Diagram
. Q . .
. . . Q
Q . . .
. . Q .

You might also like