# Introduction to Programming in C Week 6

Session: JAN-APR 2024

Course Name: Introduction to Programming in C

#### These are Introduction to Programming in C Assignment 6 Answers

Question 1

ℓ-window smoothing.
Given an n×n integer Matrix A and an positive number ℓ such that 2ℓ+1≤n, write a C program to print the ℓ window smoothing of A.
Input
The first line contains the dimension of the matrix n. Assume n < 100.
The second line contains the smoothing parameter ℓ.
The next n lines contains the contents of the matrix A, each row per line.
Output
The smoothed matrix of A

Solution:

``````#include <stdio.h>

int max(int a, int b){
if(a>b)
return a;
return b;
}

int min(int a, int b){
if(a<b)
return a;
return b;
}

int main() {
int A[100][100];
int B[100][100];

int n,l,sum;

scanf("%d",&n);
scanf("%d",&l);

for(int i = 0; i< n;i++){
for(int j = 0; j< n;j++)
scanf("%d",&A[i][j]);
}

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

int ih,il,jh,jl;
sum =0;

/*
Code for calculating B[i][j]
*/
for(il= max(i-l,0); il <= min(i+l,n-1); il++)
for(jl= max(j-l,0); jl <= min(j+l,n-1); jl++)
sum += A[il] [jl];

B[i][j] = sum;
}
}

for(int i = 0; i< n;i++){
for(int j = 0; j< n;j++){
printf("%d ",B[i][j]);
}
printf("\n");
}
return 0;
}``````

These are Introduction to Programming in C Assignment 6 Answers

Question 2

Simple Path Finding
Given an n×n binary Matrix A , where each entry is 0 or 1.
A has a unique path of 1’s from A[0][0] to A[n-1][n-1].
The path always goes Right (R) or Down (D).
Write a C Program.to print the directions of this path.
Note: You can assume that there is exactly one correct path.
All 1’s in A are in this unique path, there are no dead ends.
Input
The first line contains the dimension of the matrix n. Assume n < 100.
The second line contains the contents of the matrix A, each row per line.
Output
The path of 1’s in the Matrix.

Solution:

``````#include <stdio.h>

void findPath(int matrix[100][100], int n, int x, int y, char* path, int pathIndex) {

if (x == n - 1 && y == n - 1) {
path[pathIndex] = '\0';
printf("%s\n", path);
return;
}

/*

Complete the code here.

*/
if (matrix[x][y + 1] == 1){
path [pathIndex] = 'R';
findPath(matrix, n, x, y+1, path, pathIndex+1);
}
else if (matrix[x+ 1][y] == 1){
path [pathIndex] = 'D';
findPath(matrix, n, x+1, y, path, pathIndex+1);
}
}

int main() {
int n;
scanf("%d", &n);

int matrix[100][100];
char path[200];

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}

findPath(matrix, n, 0, 0, path, 0);

return 0;
}``````

These are Introduction to Programming in C Assignment 6 Answers

Question 3

Recursive Path Finding
Given an n×n binary Matrix A , where each entry is 0 or 1.
A has a unique path of 1’s from A[0][0] to A[n-1][n-1].
The path can go Right (R) Left (R) Down (D) or Up (U).
Write a C Program.to print the directions of this path.
Note: You can assume that there is exactly one correct path.
All 1’s in A need not be in this unique path, there can be dead ends.
Input
The first line contains the dimension of the matrix n. Assume n < 100.
The second line contains the contents of the matrix A, each row per line.
Output
The path of 1’s in the Matrix.

Solution:

``````#include<stdio.h>

int findPath(int matrix[100][100], int n, int x, int y, char* path, int pathIndex) {

// If the destination is reached, print the path and return
if (x == n - 1 && y == n - 1) {
path[pathIndex] = '\0'; // Null terminate the string
printf("%s", path);
return 1;
}

int last = 'I';

if(pathIndex !=0)
last = path[pathIndex - 1];

// Try moving Right
if (last != 'L' && y + 1 < n && matrix[x][y + 1] == 1) {
path[pathIndex] = 'R';
if(findPath(matrix, n, x, y+1, path, pathIndex+1) == 1)
return 1;
}

// Try moving Down
if (last != 'U' && x + 1 < n && matrix[x + 1][y] == 1) {
path[pathIndex] = 'D';
if(findPath(matrix, n, x+1, y, path, pathIndex+1) == 1)
return 1;
}

// Try moving Up
if (last != 'D' && x - 1 >= 0 && matrix[x - 1][y] == 1) {
path[pathIndex] = 'U';
if(findPath(matrix, n, x-1, y, path, pathIndex+1) == 1)
return 1;
}

// Try moving Left
if (last != 'R' && y - 1 >= 0 && matrix[x][y - 1] == 1) {
path[pathIndex] = 'L';
if(findPath(matrix, n, x, y-1, path, pathIndex+1) == 1)
return 1;
}
return 0;
}

int main() {
int n;
scanf("%d", &n);

int matrix[100][100];
char path[1000];

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}

findPath(matrix, n, 0, 0, path, 0);

return 0;
}``````

These are Introduction to Programming in C Assignment 6 Answers

Course Name: Introduction to programming in C

These are Introduction to Programming in C Assignment 6 Answers

Question 1

#### We say that a string ‘s’ is an anagram of another string ‘t’ if the letters in ‘s’ can be rearranged to form ‘t’.For example, “butterfly” is an anagram of “flutterby”, since a rearrangement of the first word results in the second.We say that a position ‘i’ in ‘s’ and ‘t’ match, if ‘s’ is an anagram of ‘t’, and s[i]==t[i].In this question, you will be given two words, ‘s’ and ‘t’. You have to output the number of matching positions if s is an anagram of t, and -1 if s is not an anagram of t.

These are Introduction to Programming in C Assignment 6 Answers

Input
The input consists of two lines. The first line contains the first string, with length <= 100 characters. The second line contains the second string, with length <= 100 characters.
Output
If the first string is an anagram of the second string, then output the number of matching positions. Otherwise, print -1.
Sample Input 1
————–
butterfly
flutterby
Sample Output 1
—————
2
Sample Input 2
————–
home
come
Sample Output 2
—————
-1

These are Introduction to Programming in C Assignment 6 Answers

Solution

``````#include <stdio.h>
#include <string.h>
int main()
{
char s[101], t[101];
int freq_s[26] = {0}, freq_t[26] = {0};
int i, match = 0;
scanf("%s", s);
scanf("%s", t);
if(strlen(s) != strlen(t))
{
printf("-1");
return 0;
}
for(i=0; i<strlen(s); i++)
{
freq_s[s[i]-'a']++;
freq_t[t[i]-'a']++;
}
for(i=0; i<26; i++)
{
if(freq_s[i] != freq_t[i])
{
printf("-1");
return 0;
}
}
for(i=0; i<strlen(s); i++)
{
if(s[i] == t[i])
{
match++;
}
}
printf("%d", match);
return 0;
}``````

These are Introduction to Programming in C Assignment 6 Answers

Question 2

In a string, a “run” is a substring with consisting of consecutive occurrences of the same character. For example, the string “mississippi” contains the following runs – “ss”, “ss” and “pp”.
In this question, given a string, you have to output the length of the longest run in the string.
Input
A string, having length at most 100. The string is guaranteed to have at least one run.
Output
The length of the longest run in the string.
Sample Input
abbaaacccc
Sample Output
4

Solution

``````#include <stdio.h>
int main()
{
char str[100];
int i, count, max_count;
scanf("%s", str);
count = 1;
max_count = 1;
for(i=1; str[i]!='\0'; i++)
{
if(str[i] == str[i-1])
{
count++;
if(count > max_count)
{
max_count = count;
}
}
else
{
count = 1;
}
}
printf("%d", max_count);
return 0;
}``````

These are Introduction to Programming in C Assignment 6 Answers

Question 3

Given an n×n integer Matrix A and an positive number ℓ such that 2ℓ+1≤n, print the ℓ window smoothing of A.
To get the ℓ-window smoothing of A , we replace A[i][j] with the sum of the values of the 2ℓ+1×2ℓ+1 submatrix of A with centre at A[i][j].
More precisely, the smoothed matrix B[i,j]=∑u=ilih∑v=jljhA[u][v] where il=max(i−ℓ,0),ih=min(i+ℓ,n−1), jl=max(j−ℓ,0),jh=min(j+ℓ,n−1).
Input
The first line contains the dimension of the matrix n. Assume n < 100. The second line contains the smoothing parameter ℓ.
The next n lines contains the contents of the matrix A, each row per line.
Output
The smoothed matrix of A

Solution

``````#include <stdio.h>
int main()
{
int n, l;
scanf("%d %d", &n, &l);
int A[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d", &A[i][j]);
}
}
int B[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int il = (i - l < 0) ? 0 : i - l;
int ih = (i + l > n - 1) ? n - 1 : i + l;
int jl = (j - l < 0) ? 0 : j - l;
int jh = (j + l > n - 1) ? n - 1 : j + l;
int sum = 0;
for (int u = il; u <= ih; u++)
{
for (int v = jl; v <= jh; v++)
{
sum += A[u][v];
}
}
B[i][j] = sum;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%d ", B[i][j]);
}
printf("\n");
}
return 0;
}``````

These are Introduction to Programming in C Assignment 6 Answers