Introduction To Programming in C | NPTEL | Week 5 Assignment Solutions

This post will help you with the solutions of Introduction To Programming in C NPTEL 2022 Week 5 Assignment.

Course layout

Answers COMING SOON! Kindly Wait!

Week 0: Assignment answers
Week 1: Assignment answers
Week 2: Assignment answers
Week 3: Assignment answers
Week 4: Assignment answers
Week 5: Assignment answers
Week 6: Assignment answers
Week 7: Assignment answers
Week 8: Assignment answers

Introduction To Programming in C NPTEL 2022 Week 5 Assignment Solutions

Week 5: Question 1

The Collatz function is defined for a positive integer n as follows.
f(n) = 3n+1 if n is odd
      n/2     if n is even
We consider the repeated application of the Collatz function starting with a given integer n, as follows:
f(n), f(f(n)), f(f(f(n))), …

#include<stdio.h>

int f(int n) {
  if(n==1)
    return 0;
  else if(n==2)
    return 1;
  else if((n>2) && (n%2==0))
    return f(n/2)+1;
  else
    return f((3*n)+1)+1;
}

int main() {
  int n;
  scanf("%d", &n);
  printf("%d", f(n));
  return 0;
}

Week 5: Question 2

Consider a matrix M of integers. Divide M into 4 sub-matrices. These sub-matrices are called as Quadrants. Report the Quadrant number which has the smallest minimum-element. If two or more quadrants have same smallest minimum, report the smallest quadrant index.
The matrix M is divided into four quadrants by halving the rows and columns. If row/column is an odd number, divide them in such a way that the first half of the row/column should be one smaller than the second half.

The four quadrants are numbered from 1 to 4 in the structure shown below: Q1 | Q2
—+—
 Q3 | Q4

#include<stdio.h>

int main() {
  int m, n, min, q;
  int a[15][15];
  scanf("%d%d", &m, &n);
  
  for(int i=0; i<m; i++){
    for(int j=0; j<n; j++)
      scanf("%d", &a[i][j]);
  }
  min = a[0][0];
  q = 1;
  for(int i=0; i<m; i++){
    for(int j=0; j<n; j++){
      if(min > a[i][j]){
        min = a[i][j];
        if(i < m/2) {
          if(j < n/2)
            q=1;
          else
            q=2;
        }
        else {
          if(j < n/2)
            q = 3;
          else
            q = 4;
        }
      }
    }
  }
  printf("%d", q);
  return 0;
}

Week 5: Question 3

Write a recursive program that inputs a line of characters from the user. The line may contain blanks. It outputs the line with the characters reversed. The input ends with EOF (end of file). 
NOTE: You have to use recursion to solve this, and are NOT allowed to use array to store the input!! 

Example: 
INPUT 
This is easy 

OUTPUT 
ysae si sihT

#include<stdio.h>

void reverse() {
  char ch;
  ch = getchar();
  
  if(ch == EOF)
    return;
  reverse();
  putchar(ch);
}

int main(){
  reverse();
  printf("\n");
  return 0;
}

<< Prev- Introduction To Programming in C Week 2 Assignment Solutions

>> Next- Introduction To Programming in C Week 6 Assignment Solutions


DISCLAIMER: Use these answers only for the reference purpose. Quizermania doesn't claim these answers to be 100% correct. So, make sure you submit your assignments on the basis of your knowledge.

For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.

Leave a Comment

Your email address will not be published. Required fields are marked *