0% found this document useful (0 votes)
24 views12 pages

Pds Lab Questions 2025S - SA

The document outlines the instructions and rules for a PDS Lab course, including registration on CSE Moodle, submission guidelines, and programming norms for assignments. It provides a detailed schedule of weekly tasks, including programming exercises and their requirements, as well as important Linux commands to be used. Additionally, it emphasizes academic integrity, ethics, and grading policies.

Uploaded by

poribaf830
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views12 pages

Pds Lab Questions 2025S - SA

The document outlines the instructions and rules for a PDS Lab course, including registration on CSE Moodle, submission guidelines, and programming norms for assignments. It provides a detailed schedule of weekly tasks, including programming exercises and their requirements, as well as important Linux commands to be used. Additionally, it emphasizes academic integrity, ethics, and grading policies.

Uploaded by

poribaf830
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

PDS Lab Question Paper

2024-25 Spring, Section 3 (Teachers: SA, PB, SM)

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.

Important Linux commands


In this lab, the operating system of all computers is Linux. In any computer, files are organized in directories.
A directory is a container for organizing files and other directories, called subdirectories. For example,
/home/user is a subdirectory under the directory /home.
A command is an instruction given to the computer to perform a specific task, such as listing files, creating
directories, or compiling code. For example, the command ls lists all files in a directory. The important
Linux commands to be used in this lab are given below.

Command Meaning and Examples


clear Clears the screen.
pwd Prints the current working directory.
• If you are in the directory /home/user, running pwd will output: /home/user/

cd Changes the current directory.


• If you are in the directory /home/user and want to go to its subdirectory pds, use:
cd pds
• To go back to the parent directory, use: cd ..
ls Lists the files and directories in the current directory.
• To display the names of files and directories, use: ls
• To show detailed information (permissions, size, etc.), use: ls -l
mkdir Makes new directories.
• To create a directory named new_folder under the current directory, use:
mkdir new_folder
cp Copies files or directories.
• To copy a file named file1.txt to file2.txt, use: cp file1.txt file2.txt

gcc Compiles C programs.


• To compile a C program a01.c into an executable a.out, use: gcc a01.c
• To compile a C program a01.c into an executable a.out, linking with math library
use: gcc a01.c -lm

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:

gcc a0x-y.c -lm

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

a00-1 [Text file]


Create a text file named a00-1.txt using emacs or gedit, write your name and roll number in this file, and
submit it to moodle. The file should have exactly two lines—the 1st one containing your name (as recorded
in ERP) and the 2nd one containing your iitkgp roll number in uppercase.

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

a01-1 [Arithmetic expressions]


Write a C program that takes as input two non-zero numbers x and y and prints the values of the following
expressions on the terminal:
ˆ ˙2 ˆ ˙4
x x x
1` , 1` , 1` .
y y y
You should not use math.h.
You can use at most two multiplications and at most one division in total to compute all three expressions.
A part of the code is given below. 2 ` 4 ` 4 “ 10

/*****************************************
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);

// Now use two multiplications to get the rest

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

a01-2 [Number of prime digits]


Read in a positive integer n. Assume that it has exactly two digits, namely a at the unit place and b at
the tenth place; that is, n “ a ` 10b. Without using any conditional such as if-else or switch-case, print

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: ???

Enter the value of n: 12


Number of prime digits in n = 1
Expression used: ???

Enter the value of n: 75


Number of prime digits in n = 2
Expression used: ???

Enter the value of n: 46


Number of prime digits in n = 0
Expression used: ???

a01-3 [Repeated fraction]


Given as input a positive real number x, find the value of
1 1 1
x` ` ` .
x 1 1 1
x` x` `
x x 1
x`
x

You should not use any loops or conditionals.


You should not use any variable other than x; if used, 30% to 50% marks will be deducted depending on the
number of additional variables. (Deduction: 30% for one extra variable, 40% for two extra variables, 50%
for three or more extra variables.) 10

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.

Week 2: 24-Jan-2025 Conditionals

a02-1 [One is sum of two]


Read in three integers a, b, c, and then display a message indicating whether none or any one of them equals
the sum of the other two. 10

Examples
Enter three integers: 19 47 15
None of the integers is equal to the sum of the other two.

Enter three integers: 19 47 28


b is equal to the sum of a and c.

Enter three integers: 28 19 47


c is equal to the sum of a and b.

a02-2 [Quadratic equation]


Write a C program that takes as input three real numbers a, b, c, and computes and prints the roots of the
quadratic equation ax2 ` bx ` c “ 0, correct up to the 3rd decimal place (using "%0.3f" format).
You should include math.h and compile by gcc a02-1.c -lm. 10

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

Note: Let gpyq “ ay 2 ` by ` c “ 0, where y “ x2 .


Let p ˘ iq be the two complex roots of gpyq “ 0. (If gpyq “ 0 has only real roots, then it is simpler.)
Then the four roots of f pxq “ 0 are ˘r ˘ is, where,
c ´ c ´
1 a 2 ¯ 1 a 2 ¯
r“ p ` q2 ` p , s “ p ` q2 ´ p .
2 2
In case you are interested to know why, see the following proof.
q
Proof. Let z “ r ` i ¨ signpqq ¨ s, where signpqq “ .
|q|
Then,
z 2 “ r2 ´ s2 ` i ¨ signpqq
c´ ¨ 2rs
a ¯ ´a ¯
“ p ` i ¨ signpqq ¨ p2 ` q 2 ` p p2 ` q 2 ´ p
a
“ p ` i ¨ signpqq ¨ q 2
“ p ` iq.
Thus, the square roots of p ` iq are ˘z “ ˘pr ` i ¨ signpqq ¨ sq.

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

a03-1 [Digit count]


Given any positive integer in decimal number system, compute its number of digits.

Examples
Enter a positive integer: 4 Enter a positive integer: 9
#digits = 1 #digits = 1

Enter a positive integer: 10 Enter a positive integer: 321


#digits = 2 #digits = 3

Enter a positive integer: 194708 Enter a positive integer: 103000


#digits = 6 #digits = 6

a03-2 [Prime divisors]


Find all the prime divisors of a given integer n pě 2q. You can use sqrt but no other function of math.h.

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.

Enter n: 11437 Enter n: 1234567906


Prime divisors: 11437. Prime divisors: 2, 11, 13, 631, 6841.

a03-3 [Cubic equation]

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

a04-1 [Array of Braces]


Ask the user to input a set of braces and you will store it as an array. Calculate the length of the array,
say L(2 ď L ď 100). You can ask the user to stop using a special character ‘#’ to indicate the stop of the
character array. Do not store ‘#’ in your array. For braces, we will consider “(”, “)”, “{”, “}”, “[”, “]”.
1. Assume the array length is L. We will call the array balanced if an opening brace is at m-th (0-
indexed) position, then the corresponding closing brace has to be at (L ´ m ´ 1)-th position. If the
array is unbalanced, print the positions and which braces are missing. Otherwise, print that the array
is balanced.
2. If the array is unbalanced, then fix the array in-place (do not allocate new arrays). Insert the missing
braces in the correct position and print the final array of braces.
5 ` 5 “ 10

Examples
Initial Array: [ { ( ] #
Length: 4
Array in unbalanced.
Position 3: Missing )
Position 4: Missing }
Balanced Array: [, {, (, ), }, ]

a04-2 [Reverse and Shuffle]


Ask the user to input an array of characters (char). Again, the user will input a characters ending with a
‘#’ (do not store ‘#’).
1. Write a main function that does the following to the input array: 1) reverse the array, 2) ask an user to
input how many times to shuffle (maximum 10) and 3) shuffles the array.
2. After each shuffle you have to efficiently check whether you have generated any of the previous sequences.
You can not use a 2D array for this.
5 ` 5 “ 10
Hints:
• For shuffling, you may use the following snippet. For each character, you can generate a new random
position using this. Code Snippet to use for Random Number Generation:

#include <stdlib.h> #include <time.h>


srand(time(NULL)); // randomize seed
int newPos = rand()%L; // generate a number between 0 and L-1

• 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy