Pds Lab Questions 2025S - SA
Pds Lab Questions 2025S - SA
Instructions
1. For this lab, we will use CSE Moodle: https://moodlecse.iitkgp.ac.in/moodle
2. You should register to CSE Moodle as follows:
(a) To create Moodle login, use your IIT roll number.
It may be in lowercase—check it!
(b) To create Moodle password, use your email/gmail password.
(c) For name in Moodle, use your full name — exactly same as in IIT’s ERP.
(d) For moodle ID, use your IIT roll number in uppercase.
3. After you have completed registration to CSE Moodle, register to this lab:
https://moodlecse.iitkgp.ac.in/moodle/course/view.php?id=37
Password: pds2025s
4. See all instructions related to lab rules in pds-lab-Student-instructions-2025S.pdf available in
Student folder.
5. See Announcements Forum for general information.
6. Every week, the question paper will be available in Student folder.
7. You must submit all files (C files only and not a.out) to moodle by 12:50 PM.
Try submitting a few minutes before time.
No late submission will be allowed.
8. Write the following comment block in each and every code:
/*****************************************
Name:
Roll number:
Machine number:
Assignment:
******************************************/
Your code won’t be evaluated if you don’t write the header line.
9. Name the submission file as <Roll Number>_<Assignment Number>.c. For example, if your roll number
is 24XY10091 and the assignment number is a11.c, then the file name should be 24XY10091_a11.c.
Rules
1. Plagiarism: For any instance of copying or use of unfair means, the final grade will be reduced by at least
one letter grade, regardless of the roles of the students involved. In severe cases, the grade may be
reduced to an F (fail).
2. Ethics: During laboratory sessions, students must leave their personal belongings, including mobile
phones, outside the lab. If a student is found in possession of a mobile phone during the laboratory, the
final grade will be penalized in the same manner as in cases of plagiarism.
3. Books: For regular assignments, students are encouraged to use books and notebooks.
4. Absence: There won’t be any compensatory laboratory for absence on any ground. With proper medical
certificates from IIT Hospital, an absentee can get prorated marks. The medical certificates should be
shown physically at the next lab.
5. Grading: The final grading will be relative based on the distribution of the marks.
2
Programming norms
1. Any real number should be in float, unless stated otherwise.
2. Every printed real value should be correct up to the 6th decimal place, unless stated otherwise.
3. No library other than stdio.h can be used unless mentioned in the question.
4. You should compile use gcc.
For example, if your file is a01-1.c, then the compilation command is:
gcc a01-1.c
5. If you are using the math library—for example to compute the square root in a code a0x-y.c—then you
have to compile as follows:
6. The input-output format must be as in the examples. Otherwise there will be some penalty.
7. Marks will depend on the efficiency of your code. Try to use as few operations (addition, subtraction,
multiplication, and division) as possible.
3
Week 0: 03-Jan-2025 Introduction
Examples
Abdul Rao Joseph
24XY10091
Revise the above file to write something about your hobby, and resubmit it. The revised file should have
three or more lines. 10
Examples
Abdul Rao Joseph
24XY10091
I love flowers and butterflies. My hobby is gardening.
a00-2 [Hello]
Write a C program that, when compiled and run on the terminal, prints a hello in the 1st line, and then
prints your name and roll number in the next line. The name of your C file should be a00-2.c
Examples
Hello!
I am Abdul Rao Joseph, my roll number is 24XY10091.
You can compile the C file to get the binary executable file as follows:
gcc a00-2.c 10
Week 1: 10-Jan-2025 Expressions
/*****************************************
Name:
Roll number:
Machine number:
Assignment: a01-1
******************************************/
#include <stdio.h>
int main(){
float x, y, z;
printf("Enter two numbers: ");
scanf("%f%f", &x, &y);
z = 1 + x/y; // One division used
printf("1 + x/y = %f\n", z);
return 0;
}
Examples
gcc a01-1.c
./a.out
Enter two numbers: 5 2.5
1 + x/y = 3.000000
(1 + x/y)^2 = 9.000000
(1 + x/y)^4 = 81.000000
./a.out
Enter two numbers: 2.5 3.3
1 + x/y = 1.757576
(1 + x/y)^2 = 3.089072
(1 + x/y)^4 = 9.542369
5
its number of prime digits. Also, print the expression (a function of a and b) you have used to get the
answer. 5 ` 5 “ 10
Examples
Enter the value of n: 20
Number of prime digits in n = 1
Expression used: ???
Examples
Enter x: 1
Value of the expression = 2.900000.
Enter x: 0.5
Value of the expression = 3.244828.
Enter x: 3.14
Value of the expression = 4.014452.
6
17-Jan-2025
Compensatory holiday.
Examples
Enter three integers: 19 47 15
None of the integers is equal to the sum of the other two.
Examples
Enter a, b, c: 0 0 1
Invalid equation!
Enter a, b, c: 0 1 2
Linear equation, root: -2.000
Enter a, b, c: 2 0 1
Complex roots: -0.000 - i0.707, -0.000 + i0.707
Enter a, b, c: 2 0 -1
Real roots: 0.707, -0.707
Enter a, b, c: 1 9 3
Real roots: -0.347, -8.653
Enter a, b, c: 9 1 3
Complex roots: -0.056 - i0.575, -0.056 + i0.575
7
a02-3 [Biquadratic equation]
Given three real numbers ap‰ 0q, b, c, compute and print the roots of f pxq “ ax4 ` bx2 ` c “ 0, correct up to
the 3rd decimal place. If the real or the imaginary part is zero, then that shouldn’t be printed (as in the 1st
example). 10
Examples
Enter a, b, c: 1 0 -4
Roots = 1.414, -1.414, i1.414, -i1.414
Enter a, b, c: 1 0 4
Roots = 1.000+i1.000, -1.000+i1.000, 1.000-i1.000, -1.000-i1.000
Enter a, b, c: 1 2 3
Roots = 0.605+i1.169, -0.605+i1.169, 0.605-i1.169, -0.605-i1.169
Enter a, b, c: 3 2 1
Roots = 0.349+i0.675, -0.349+i0.675, 0.349-i0.675, -0.349-i0.675
8
Week 3: 31-Jan-2025 Loops
Examples
Enter a positive integer: 4 Enter a positive integer: 9
#digits = 1 #digits = 1
Examples
Enter n: 2 Enter n: 7
Prime divisors: 2. Prime divisors: 7.
Enter n: 9 Enter n: 72
Prime divisors: 3. Prime divisors: 2, 3.
The task is to solve a cubic equation f pxq “ 0, where f pxq “ x3 ` ax2 ` bx ` c, and a, b, c are all integers
in r´10, 10s. It can be solved by Cardano’s Formula, which requires a lot of maths. So, as a practical
alternative, you have to write a program to compute an approximate root α so that |f pαq| ă ε, taking ε as
a very small quantity, say 10´6 . Note that any cubic polynomial has at least one real root (in fact, it always
has either one or three real roots), and you have to find any one using the following method.
1. Maintain a pair of variables (type double) u and v with the property that f puq ď 0 and f pvq ě 0. (If
f puq ď 0 and f pvq ě 0, then a real root of f is guaranteed to exist in the interval ru, vs.)
2. Initialize v “ |a| ` |b| ` |c| and u “ ´v. (It ensures f puq ď 0 and f pvq ě 0—do you see why?)
3. Iteratively refine the interval ru, vs to find α as follows:
(a) Find the intersection pα, 0q between the X-axis and the line through pu, f puqq and pv, f pvqq.
(b) If |f pαq| ă ε, then α is the required solution.
Else, update u to x if f pxq ď 0 or update v to x otherwise (ensures f puq ď 0 and f pvq ě 0).
Note: Other than a, b, c, u, v, you can use at most 6 variables. You should not use any library other than
stdio.h. 10
9
exact root exact root
f (v) f (v) f (v)
u α u
v α v v
f (u) f (u)
Examples
Enter a, b, c: 1 1 1
Root = -1.000000 (function value = 0.000000)
Enter a, b, c: -1 -1 1
Root = 1.000000 (function value = 0.000000)
Enter a, b, c: 1 1 -1
Root = 0.543689 (function value = -0.000001)
Enter a, b, c: 7 8 9
Root = -5.903047 (function value = -0.000001)
Enter a, b, c: 9 0 1
Root = -9.012312 (function value = 0.000000)
10
Week 4: 14-Feb-2025 Arrays
Examples
Initial Array: [ { ( ] #
Length: 4
Array in unbalanced.
Position 3: Missing )
Position 4: Missing }
Balanced Array: [, {, (, ), }, ]
• To check whether two shuffled arrays are equal, you will encode the integer values of the the positions. Say,
the original characters were in position 1, 2, 3, 4. They are encoded as oldhash “ ΣpPt1,...,#digitsu dp ˚10p´1 .
When they are shuffled as 4, 3, 2, 1. They can be encoded as newhash “ ΣnewpPt1,...,#digitsu dnewp ˚
10newp´1 “ 4 ˚ 1000 ` 3 ˚ 100 ` 2 ˚ 10 ` 1. For any new shuffles, shuf f le1 and shuf f le2 , you can compare
corresponding newhash1 and newhash2 to determine whether the new shuffles are identical. Use long
int as a datatype. Handle numeric overflows by defining a MAXINT, using hash “ hash%MAXINT.
Examples
11
Enter character array: c o m p u t e #
Reversed character array: e t u p m o c
Enter How many times to shuffle: 2
Original character array hash value: 1234567
Shuffling 1: t e u p m c o, Hash Value: 6754312
Shuffling 2: t m p u o c e, Hash Value: 6345217
None of them are same.
12