C PROGRAMMING • WEEK 6
NPTEL - Introduction to Programming in C - Week 6:Assignment 6 Answers- 2025
NPTEL • Introduction to Programming in C
Week 6 : Assignments - NPTEL >> Introduction to Programming In C - 2025
Week 6 : Assignment 6 - Question 1
Complete the function isSymmetric(int A[][n], int n) that checks if A is a symmetric matrix and returns 1 if symmetric, 0 otherwise.
Solution :
#include<stdio.h>
// Complete this Function to check if a matrix is symmetric.
// A is an n*n Matrix. Return 1 is A is symmetric and 0 otherwise.
int isSymmetric(int A[10][10], int n) {
int flag=1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if(A[i][j]!=A[j][i]){
flag=0;
break;
}
}
}
return flag;
}
int main() {
int n;
scanf("%d", &n);
int A[10][10];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &A[i][j]);
}
}
printf("%d",isSymmetric(A,n));
return 0;
}
Week 6 : Assignment 6 - Question 2
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.
Solution :
#include<stdio.h>
int main()
{
int n,i,j;
int A[10][10];
scanf("%d", &n);
for (i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
{
scanf("%d", &A[i][j]);
}
}
i=0;
j=0;
while (i != n-1 || j !=n-1)
{
if(j+1 < n && A[i][j+1] == 1)
{
printf("R");
j++;
}
else if(i+1 < n && A[i+1][j] == 1)
{
printf("D");
i++;
}
}
return 0;
}
Week 6 : Assignment 6 - Question 3
Write a C program to count the number of islands in the matrix.
Solution :
#include<stdio.h>
int main() {
int n;
scanf("%d", &n);
int grid[20][20];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
scanf("%d", &grid[i][j]);
int count = 0;
// Complete the code here.
// Compute the number of islands, and store them in count.
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
if ((i == 0 || grid[i-1][j] == 0) && (j == 0 || grid[i][j-1] == 0)) {
count++;
}
}
}
}
printf("%d", count);
return 0;
}