PDS LAB (Date: 13 Aug 2024) Assignment - 1 (Datatypes, Variables, Operators, Expressions and I/O)
PDS LAB (Date: 13 Aug 2024) Assignment - 1 (Datatypes, Variables, Operators, Expressions and I/O)
________________________________________________________________
p1] Write a C program to find the parimeter and squared area of a triangle. The corners of a
triangle [A(x1, y1), B(x2, y2) and C(x3, y3)] should be provided by the user through keyboard.
Use appropriate prompts to the user for passing the inputs from the keyboard and display
the output. [25 marks]
Perimeter of a triangle = AB+BC+CA, where AB, BC and CA are the lengths of sides of
triangle.
Squared area = S(S-AB)(S-BC)(S-CA), where S = Perimeter/2
___________________________________________________________________________
p2] Swap 2 numbers: [25 marks]
Write a C program to swap two given numbers. You need to assign the input numbers (from
the keyboard) to two variables “x” and “y”. You need to perform the swapping of the values
associated to the variables and print the variables before and after swapping.
a] using 1 extra variable.
b] without using any extra variable.
________________________________________________________________
p3] Write a program in C that performs the following tasks: [25 marks]
1. Read a floating-point number x and an integer n from the user.
2. Compute and print the following:
o Ceiling of x: The smallest integer greater than or equal to x.
o Floor of x: The largest integer less than or equal to x.
o Result of dividing x by n as a floating-point number.
o Result of dividing x by n and casting the result to an integer.
o Result of dividing n by x as a floating-point number.
o Result of dividing n by x and casting the result to an integer.
3. Print all floating-point results up to 2 decimal places, to do this you can use:
printf("%.2f", floating_point_value);
Include<math.h> [while compiling: gcc 3.c -o 3.out -lm]
To find ceil of x use: ceil(x)
To find floor of x use: floor(x)
To convert a floating-point number to an integer, use the int type cast:
int intValue = (int)x; // Casts float x to int.
To convert an integer to a floating-point number, use the float type cast:
float floatValue = (float)n; // Casts int n to float
Example:
Enter a floating-point number (x): 8.75 Enter an integer (n): 4
Output explanation:
1. Ceiling of x:
o The smallest integer greater than or equal to 8.75 is 9.
o Result: 9.00
2. Floor of x:
o The largest integer less than or equal to 8.75 is 8.
o Result: 8.00
3. Result of dividing x by n (as float):
o 8.75 divided by 4 equals 2.1875. When printed with 2 decimal places, it is
2.19.
o Result: 2.19
4. Result of dividing x by n (as int):
o 8.75 divided by 4 equals 2.1875. Casting this to an integer gives 2 (fractional
part is discarded).
o Result: 2
5. Result of dividing n by x (as float):
o 4 divided by 8.75 equals approximately 0.457. When printed with 2 decimal
places, it is 0.46.
o Result: 0.46
6. Result of dividing n by x (as int):
o 4 divided by 8.75 equals approximately 0.457. Casting this to an integer gives
0 (fractional part is discarded). Hence result = 0.
o Result: 0
________________________________________________________________
p4] In this problem, you need to compute the derivative of a degree-5 polynomial of one
variable x: a5 x^5 + a4 x^4 + a3 x^3 + a2 x^2 + a1 x + a0 [25 marks]
Assume that the coefficients ai of the polynomial are integers. Input the 6 coefficients from
the user. Print the polynomial, and then compute and print the derivative polynomial.
A sample input and output are given; your program should follow the format shown below.
Enter the constant term: 5
Enter coefficient of x: 2
Enter coefficient of x^2: -2
Enter coefficient of x^3: 7
Enter coefficient of x^4: 0
Enter coefficient of x^5: 3
Polynomial: 3*x^5 + 0*x^4 + 7*x^3 - 2*x^2 + 2*x + 5
Derivative: 15*x^4 + 0*x^3 + 21*x^2 - 4*x + 2
Note that some of the coefficients can be zero. Ideally, terms with zero coefficients should
not be printed while printing the polynomials. You will learn later how to print a certain term
conditionally, i.e., print a term only if the coefficient is non-zero. For the purpose of this
assignment, you can print out all terms including those with zero coefficients
__________________________________________________________________________