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 does the following: Read a positive integer a from the user. Report whether this integer is even or odd. If a is even, assign to b the value a/2, else assign to b the value 3a+1. Print b and also whether b is even or odd.
Sample runs
Enter a positive intger: 122 122 is even 61 is odd Enter a positive intger: 123 123 is odd 370 is even Enter a positive intger: 124 124 is even 62 is evenSolution 1
#include <stdio.h> int main () { unsigned int a, b; printf("Enter a positive intger: "); scanf("%u", &a); if (a % 2 == 0) { printf("%u is even\n", a); b = a / 2; } else { printf("%u is odd\n", a); b = 3 * a + 1; } printf("%u is %s\n", b, ((b & 1) == 0) ? "even" : "odd"); }Exercise 2
Write a C program that reads a positive real number x, computes the fractional part of x, and also the rounded value of x (to the nearest integer). Your program may not make any math library calls.
Sample runs
Enter a positive real number: 1.2345 Fractional part of 1.234500 is 0.234500 Rounded value of 1.234500 is 1 Enter a positive real number: 235.5 Fractional part of 235.500000 is 0.500000 Rounded value of 235.500000 is 236 Enter a positive real number: 235.49 Fractional part of 235.490000 is 0.490000 Rounded value of 235.490000 is 235 Enter a positive real number: 235.51 Fractional part of 235.510000 is 0.510000 Rounded value of 235.510000 is 236Solution 2
#include <stdio.h> int main () { double x, y; long int a; printf("Enter a positive real number: "); scanf("%lf", &x); y = x - (int)x; printf("Fractional part of %lf is %lf\n", x, y); a = (int)x; if (y >= 0.5) ++a; printf("Rounded value of %lf is %ld\n", x, a); }Exercise 3
Write a C program that computes the two roots of the quadratic equation ax2+bx+c=0, where a,b,c are real numbers with a>0. Your program should handle complex roots also.
Sample runs
Enter the coefficients (a,b,c): 2 3 1 The roots are real: -0.500000, -1.000000 Enter the coefficients (a,b,c): 2 2 0.5 The roots are real: -0.500000, -0.500000 Enter the coefficients (a,b,c): 2 3 2 The roots are complex: (-0.750000)+i(0.661438), (-0.750000)-i(0.661438)Solution 3
#include <stdio.h> #include <math.h> int main () { double a, b, c, d, x, y; printf("Enter the coefficients (a,b,c): "); scanf("%lf%lf%lf", &a, &b, &c); if (a == 0) { printf("I want a > 0\n"); } else { d = b*b - 4*a*c; if (d >= 0) { x = (-b+sqrt(d))/(2*a); y = (-b-sqrt(d))/(2*a); printf("The roots are real: %lf, %lf\n", x, y); } else { d = -d; x = -b/(2*a); y = sqrt(d)/(2*a); printf("The roots are complex: (%lf)+i(%lf), (%lf)-i(%lf)\n", x,y,x,y); } } }Exercise 4
Write a C program that reads the coordinates of three points in the plane and reports which two of the three points are closest. Do not use math library calls.
Sample runs
Point 1: 1,1 Point 2: 2,2 Point 3: 4,4 Points 1,2 are closest Point 1: 1,2 Point 2: 4,8 Point 3: 3,6 Points 2,3 are closest Point 1: 6,2 Point 2: 1,-1 Point 3: 4,1 Points 1,3 are closestSolution 4
#include <stdio.h> int main () { double x1, y1, x2, y2, x3, y3; double d12, d13, d23; printf("Point 1: "); scanf("%lf,%lf", &x1, &y1); printf("Point 2: "); scanf("%lf,%lf", &x2, &y2); printf("Point 3: "); scanf("%lf,%lf", &x3, &y3); d12 = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2); d13 = (x1-x3)*(x1-x3) + (y1-y3)*(y1-y3); d23 = (x2-x3)*(x2-x3) + (y2-y3)*(y2-y3); if (d12 < d13) { if (d12 < d23) printf("Points 1,2 are closest\n"); else printf("Points 2,3 are closest\n"); } else { if (d13 < d23) printf("Points 1,3 are closest\n"); else printf("Points 2,3 are closest\n"); } }Exercise 5
Write a C program that reads the user's birth-place and determines whether this birth-place appears earlier or later in the dictionary than the string "kharagpur". Use the library call strcmp(). Work with strings in lower case only.
Sample runs
Enter your birth place: calcutta "calcutta" < "kharagpur" in dictionary Enter your birth place: kolkata "kolkata" > "kharagpur" in dictionary Enter your birth place: kharagpur Your birth place is "kharagpur"Solution 5
#include <stdio.h> #include <string.h> int main () { char bplace[1000], compres; printf("Enter your birth place: "); scanf("%s",bplace); compres = strcmp(bplace,"kharagpur"); if (compres == 0) printf("Your birth place is \"kharagpur\"\n"); else if (compres < 0) printf("\"%s\" < \"kharagpur\" in dictionary\n", bplace); else printf("\"%s\" > \"kharagpur\" in dictionary\n", bplace); }