Introduction to Programming in C Week 5

Session: JAN-APR 2024

Course Name: Introduction to Programming in C

Course Link: Click Here

For answers or latest updates join our telegram channel: Click here to join

These are Introduction to Programming in C Assignment 5 Answers


Question 1

Convergence depth of Collatz function
The Collatz function is defined for a positive integer n as follows.
f(n)={3n+1,n/2,if n oddif n is even
Sample Input
7
Sample Output
16

Solution:

#include <stdio.h>

int collatzConvergenceDepth(int n) {
    int count = 0;
    while (n != 1) {
        if (n % 2 == 0) {
            n /= 2;
        } else {
            n = 3 * n + 1;
        }
        count++;
    }
    return count;
}

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

    int depth = collatzConvergenceDepth(n);

    printf("%d", depth);

    return 0;
}

For answers or latest updates join our telegram channel: Click here to join

These are Introduction to Programming in C Assignment 5 Answers


Question 2

BlockSum of an Array
Given an integer array M having size n which is power of 2, Write a recursive code to find the BlockSum of the array M.
The following is the recursive definition of BlockSum:
If size of M is 2, say M=[a,b], where a and b are integers, then BlockSum(M)=a−b.

Solution:

#include <stdio.h>

int blockSum(int arr[], int n) {
    if (n == 2) {
        return arr[0] - arr[1];
    } else {
        int mid = n / 2;
        int sumA = blockSum(arr, mid);
        int sumB = blockSum(arr + mid, mid);
        return sumA - sumB;
    }
}

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

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

    int result = blockSum(arr, n);
    printf("%d", result);

    return 0;
}

For answers or latest updates join our telegram channel: Click here to join

These are Introduction to Programming in C Assignment 5 Answers

More Solutions of Introduction to Programming in C: Click Here

More NPTEL Solutions: https://progiez.com/nptel-assignment-answers/


Course Name: Introduction to programming in C

Course Link: Click Here

These are Introduction to Programming in C Assignment 5 Answers


Question 1

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

These are Introduction to Programming in C Assignment 5 Answers

Code:-

#include <stdio.h>
void revstr(){
    int ch = getchar();
    if (ch == EOF) return;
    revstr();
    putchar(ch);
}
int main(){
    revstr();
    return 0;
}

These are Introduction to Programming in C Assignment 5 Answers


Question 2

The Collatz function is defined for a positive integer n as follows.
f(n)={3n+1,n/2,if n oddif 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))),…
It is conjectured that no matter which positive integer n you start from, this sequence eventually will have 1 in it. It has been verified to hold for numbers up to 5 × 260 [Wikipedia: Collatz Conjecture].

These are Introduction to Programming in C Assignment 5 Answers

e.g. If n=7, the sequence is
f(7) = 22
f(f(7)) = f(22) = 11
f(11) = 34
f(34) = 17
f(17) = 52
f(52) = 26
f(26) = 13
f(13) = 40
f(40) = 20
f(20) = 10
f(10) = 5
f(5) = 16
f(16) = 8
f(8) = 4
f(4) = 2
f(2) = 1
Thus if you start from n=7, you need to apply f 16 times in order to first get 1.
In this question, you will be given a positive number <= 32,000. You have to output how many
times f has to be applied repeatedly in order to first reach 1.

These are Introduction to Programming in C Assignment 5 Answers

Code:-

#include <stdio.h>
int collatz(int m);
int c=0;
int main()
{
  int n,c;
  scanf("%d",&n);
  if(n<=0)
  {
    printf("0");
  }
  else if(n==1)
  {
    printf("0");
  }
  else
  {
    c = collatz(n);
    printf("%d",c);
  }
}
int collatz(int m)
{
  int n=m;
  if(n%2==0)
  {
    n=n/2;
    c++;
    if(n!=1)
    {
      collatz(n);
    }
    else
    {
      return c;
    }
  }
  else
  {
    n=(3*n)+1;
    c++;
    if(n!=1)
    {
      collatz(n);
    }
    else
    {
      return c;
    }
  }
}

These are Introduction to Programming in C Assignment 5 Answers


Question 3

Description
Write a recursive code to find the block product of an array.
The following is the algorithm to find the block product recursively:
Given an array A partition it into four quadrants of equal size:
M=[ABCD]
The block product of the array is defined recursively as :
|M|=|A|∗|D|−|B|∗|C|
With the base case, when n = 4 , |[a,b,c,d]|=ad−bc
Note : You can assume that n is a power of 4. n>=4 and n <= 1024.

These are Introduction to Programming in C Assignment 5 Answers

Input
The first line contains the array size n
The next n lines contains the elements of the array.
Output
Block product |M|
Example 1
M=[3215]
|M|=3∗5−2∗1=13
Example 2
M=[32152−25510750−256]

Code:-

//Code

These are Introduction to Programming in C Assignment 5 Answers

More Weeks of Introduction to programming in C: Click Here

More Nptel courses: https://progiez.com/nptel


These are Introduction to Programming in C Assignment 5 Answers


These are Introduction to programming in C Assignment 5 Answers
The content uploaded on this website is for reference purposes only. Please do it yourself first.