0% found this document useful (0 votes)
22 views

Assignment 6

The document provides instructions for submitting assignments in Moodle and formatting output files. It includes three programming assignments: (1) write a recursive function to compute a sum of terms and count recursive calls, (2) write a function to find the maximum sum of digits in an array of numbers, and (3) write a recursive function to reverse digits and determine if a number is a palindrome. Students are to run sample test cases and provide results in separate output files for each part.

Uploaded by

jitbitan.kgpian
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Assignment 6

The document provides instructions for submitting assignments in Moodle and formatting output files. It includes three programming assignments: (1) write a recursive function to compute a sum of terms and count recursive calls, (2) write a function to find the maximum sum of digits in an array of numbers, and (3) write a recursive function to reverse digits and determine if a number is a palindrome. Students are to run sample test cases and provide results in separate output files for each part.

Uploaded by

jitbitan.kgpian
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Assignment VI

Submit all the programs separately against each assignment in the Moodle System. Provide
the result in a separate output file (named, result_<assgn><no>.txt). Use standard output
redirection feature to generate the output file.

Hints. If you run the program with the following command

./a.out >result.txt

Output of your program (generated by printf(.) function) will be written in the file result.txt.
You need to provide input from your keyboard, by remembering the sequence of inputs to be
given or writing them in a text file in the same sequence.

Otherwise you may use the redirection for the standard input file, such as,

./a.out <input.txt

For the above all your printing by printf(.) function would be displayed on your monitor.

For both reading from a file and writing to a file use the following.

./a.out <input.txt >result.txt

If you execute the program multiple times, you may concatenate the outputs in a single file by
using the following redirection command:

./a.out >>result.txt

or

./a.out <input.txt >> result.txt

(a) Write a program, which has a recursive function f(n) as defined below:
f(n) = 0, for n<0

= 1, for n=0

= f(n-2) -2* f(n-1) + f(n-3), otherwise

Note that n is an integer argument and f(n)returns an integer value.

The program computes the following expression using f(n):


f(0)+f(1)+f(2)+ ….. for M terms (M to be read)
The program prints the value of the expression and also the number of times
recursive calls invoked while computing the expression.

Hints: Use a global variable to keep count of the number of recursive calls
within the function f(n).

Run your program for the following input data set and provide the results
with input datasets in a separate output file.

(i) M=5, and (ii) M=10.

(b) Write a recursive function named sum_digits(.) for computing sum of digits
of a positive integer (up to six digits long). Read a list of positive integers in an
array and print the number(s) which has (have) the maximal sum of digits.

Run your program for the following input data set and provide the results with
input datasets in a separate output file.

(i) 35, 467, 1234, 89, 145, 912, 0, 1000, 10000

(ii) 856, 1000, 94, 19, 205, 489, 75, 57

(c) Write a recursive C function digit_reverse(.) that takes a positive integer


(up to six digits long) as argument, and returns another integer where the digits
are reversed. For example, if the number passed as argument is 2371, the value
returned will be 1732. Write a main function that uses "digit_reverse" to
determine, if a given number is a palindrome. A palindrome is one which reads
the same forward or backward (like 120021, 234, 5, etc.).

Read a list of positive numbers in an array and print them after reversing their
digits. Further report the palindromes and the number of palindromes.

Run your program for the following input data set and provide the results with
input datasets in a separate output file.

(i) 1456, 100, 1001, 23432, 156, 789, 4502

(ii) 8000, 80008, 23657, 78986, 23032, 10010

You might also like