CS19002 Programming and Data Structures Laboratory, Section 5 |
Spring 2007 |
Exercise 1 | Exercise 2 | Exercise 3 | Exercise 4 | Exercise 5 | |
Solution 1 | Solution 2 | Solution 3 | Solution 4 | Solution 5 | |
Show all | Hide all |
Click on the links above
Exercise 1
Write a C program that reads a positive integer n and prints the decimal representation of the integer in reverse order. For example, upon input 72945 should print 54927. Do not write a function (well, other than the main() function).
Solution 1
#include <stdio.h> int main () { unsigned long int n, s; printf("n = "); scanf("%lu", &n); s = 0; while (n != 0) { s = s * 10 + (n % 10); n /= 10; } printf("Reversed integer is %lu\n", s); }Exercise 2
Write a C program that finds an approximate value of a definite integral. Let l be the left and r the right boundary for the integral. Also let h be the step-size. The idea is to break the interval [l,r] into sub-intervals [l,l+h], [l+h,l+2h], [l+2h,l+3h], ..., [r-h,r]. Assume that r-l is an integral multiple of h. Your program evaluates the function to be integrated at the center of each interval, multiplies these values by the width h, and computes the sum of these products as the approximate integral. As a concrete realization, integrate the function as x2. The quantities l,r,h should be supplied by the user.
Solution 2
#include <stdio.h> int main () { double l, r, h, x, s; printf("Enter left boundary : "); scanf("%lf", &l); printf("Enter right boundary : "); scanf("%lf", &r); printf("Enter step size : "); scanf("%lf", &h); s = 0; for (x = l; x < r; x += h) s += h * (x+h/2) * (x+h/2); printf("Integral = %lf\n", s); }Exercise 3
A positive integer is called a perfect number if it equals the sum of its proper divisors. For example, 6=1+2+3 is a perfect number.
Write a function that, upon input a positive integer n, returns the sum of the proper divisors of n.
Write a main() function that reads a bound B from the user and prints all perfect numbers between 1 and B.
Solution 3
#include <stdio.h> unsigned long int sumofdiv ( unsigned long int n ) { unsigned long int s, d; s = 0; for (d = 1; d < n; ++d) if (n % d == 0) s += d; return s; } int main () { unsigned long int B, n; printf("Enter a bound : "); scanf("%lu", &B); for (n = 1; n <= B; ++n) { if (sumofdiv(n) == n) { printf("%lu is a perfect number.\n", n); } } }Exercise 4
Write a function that takes two positive integer arguments a,b and returns the floating-point value f(a,b) = (a2+b2)/(ab-1). Write a main() function that reads a bound B from the user and computes f(a,b) for 1 <= a < b <= B. If f(a,b) happens to be an integer, print the value of that integer along with the corresponding values of a,b.
Solution 4
#include <stdio.h> double f ( unsigned long int a , unsigned long int b ) { if (a*b <= 1) return 0; return ((double)(a*a + b*b) / (double)(a*b - 1)); } int main () { unsigned long int a, b, B, t; double fab; printf("Enter a bound: "); scanf("%lu", &B); for (a = 1; a < B; ++a) { for (b = a+1; b < B; ++b) { fab = f(a,b); t = (unsigned long int)fab; if (fab == t) printf("f(%lu,%lu) = %lu\n", a, b, t); } } }Exercise 5
Catalan numbers Cn are defined recursively as follows:
C0 = 1
Cn = C0Cn-1 + C1Cn-2 + C2Cn-3 + ... + Cn-1C0 for n >= 1.Write a recursive function that, upon input n, returns Cn. Also write a main() function that invokes the recursive function on an argument supplied by the user.
Solution 5
#include <stdio.h> unsigned long int Catalan ( int n ) { unsigned long int s, i; if (n == 0) return 1; for (s = i = 0; i < n; ++i) s += Catalan(i) * Catalan(n-i-1); return s; } int main () { unsigned long int n; printf("Enter n: "); scanf("%lu", &n); printf("C(%lu) = %lu\n", n, Catalan(n)); }