Introduction to programming in C Week 6

Course Name: Introduction to programming in C

Course Link: Click Here

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

More Weeks of Introduction to programming in C: Click Here

More Nptel courses: http://13.235.254.184/nptel



These are Introduction to programming in C Assignment 6 Answers


These are Introduction to programming in C Assignment 6 Answers