# Introduction to programming in C Week 5

Course Name: Introduction to programming in C

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:INPUTThis is easyOUTPUTysae si sihT

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.

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.

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]
|M|=∣∣∣[3215]∣∣∣∗∣∣∣[0−256]∣∣∣−∣∣∣[2−255]∣∣∣∗∣∣∣[1075]∣∣∣=13∗10−20∗5=30

Code:-

``````#include <stdio.h>
int product(int *arr, int n)
{
if(n==4)
{
return(arr[0]*arr[3]-arr[1]*arr[2]);
}
int *a=arr,*b=arr+(n/4),*c=arr+(n/2),*d=arr+(3*n/4);
int M1=product(a,n/4);
int M2=product(b,n/4);
int M3=product(c,n/4);
int M4=product(c,n/4);
return (M1*product(d,n/4)-M2*product(c,n/4));
}
int main()
{
int n,i,res;
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
res=product(arr,n);
printf("%d",res);
return 0;
}``````

These are Introduction to programming in C Assignment 5 Answers