0% found this document useful (0 votes)
47 views5 pages

Pds Lab 2022A Question Paper Upto A03

The document contains instructions for 6 programming assignments covering topics like data types, operations, conditionals, logical expressions, and random number generation. Students are asked to write C programs that take inputs, perform calculations, and output results based on the given examples and rules. Programs must be compiled and submitted with file names and output formats as specified. Correctness, efficiency of code, and minimizing operations are emphasized.

Uploaded by

Hitesh Dabur
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)
47 views5 pages

Pds Lab 2022A Question Paper Upto A03

The document contains instructions for 6 programming assignments covering topics like data types, operations, conditionals, logical expressions, and random number generation. Students are asked to write C programs that take inputs, perform calculations, and output results based on the given examples and rules. Programs must be compiled and submitted with file names and output formats as specified. Correctness, efficiency of code, and minimizing operations are emphasized.

Uploaded by

Hitesh Dabur
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/ 5

PDS Lab 2022-23 Autumn Section 17

Week 1: 14-Nov-2022 (Introduction)


a01-1. Create a text file named a01-1-<Your Roll Number>.txt, write your name and roll number in
this file, and submit it to moodle. For example, if your roll number is 22XY10091, then the filename
should be a01-1-22XY10091.txt
The above 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.
Revise the above text file to write something about your hobby, and resubmit it. The revised file will
have three or more lines.
Example:
Name: Abdul Rao
Roll number: 22XY10091
Hobby: Gardening

Note: Always read the instructions in the question very very carefully and get your doubts clarified.
a01-2. 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 a01-2.c.
Example:
Hello!
I am Abdul Rao, my roll number is 22XY1009.

Write a C program that takes as inpu t two integers and from the keyboard, computes and
prints its value on the terminal.
 The value of should be correct up to 6th decimal place.
 The name of your C file should be w01-2.c.

Example
Enter a and b: 2 3
a/b = 0.666667
Note:
i. Any real number should be in floating-point precision, unless stated otherwise.
ii. Every printed real value should be correct up to the 6th decimal place, unless stated otherwise.
iii. You should submit the C file only. The name of your C file should be <assignment number>.c;
for example, for a02-1 it will be a02-1.c
iv. You can name the binary executable file during compilation; for example, for a02-1.c you can
name it a02-1.out instead of the default name a.out as follows:
gcc a02-1.c -o a02-1.out
v. The output format must be as in the examples for all the assignments. Otherwise there will be some
penalty.
vi. Marks will depend on the efficiency of your code. Try to use as few operations (addition, subtraction,
multiplication and division) as possible.

Week 2: 21-Nov-2022 (Data Types, Operations, Assignments)


a02-1. Write a C program that takes as input three nonzero integers from the keyboard and computes
and prints the values of the following expressions on the terminal.
i.
ii.

iii.

Example
Enter a, b, c: 3 2 4
a+b-c=1
-a-b+c=-1
a/b + b/c + c/a=3.333333

a02-2. Recall that the area of a triangle ABC is given by If the coordinates of its three vertices
are then it turns out to be
Given as input the coordinates of the three vertices of a triangle as integers, your program has to
compute and print its perimeter and area, correct up to the 3 rd decimal place. For perimeter
computation, you have to use the sqrt function of math.h library as follows.

#include <math.h>

double sqrt(double x);

Compilation command: gcc a02-2.c -lm


The argument -lm is to link the math library with your code. Note that it’s a hyphen followed by the
English letters and .  
Examples

Enter x and y coordinates of vertex 1: -1 0


Enter x and y coordinates of vertex 2: 1 0
Enter x and y coordinates of vertex 3: 0 1
perimeter: 4.828
Area: 1.000

Enter x and y coordinates of vertex 1: -2 -1


Enter x and y coordinates of vertex 2: 3 2
Enter x and y coordinates of vertex 3: 9 -7
perimeter: 29.178
Area: 31.500

a02-3. Write a program to accomplish the following tasks.  


i. Scan as input a double-precision number and print its value correct up to the 9 th decimal
place.
ii. Without using any division and with as few multiplications as possible, compute the values of
and
and print their values correct up to the 9 th decimal place. Also print the total number of
multiplications used in your code.

Examples
Enter x: 0.8765677821
0.876567782
1+x(1+x(1+x(1+x(1+x(1+x)))))=4.880024516
1-x(1-x(1-x(1-x(1-x(1-x)))))=0.744789378
#multiplications=?

Enter x: 25.000123456789
25.000123457
1+x(1+x(1+x(1+x(1+x(1+x)))))=254320633.968345135
1-x(1-x(1-x(1-x(1-x(1-x)))))=234757601.247287929
#multiplications=?
Week 3: 28-Nov-2022
Logical expression, conditionals (if-else, switch-case)
a03-1. [Biquadratic equation] Write a program to compute and print the roots of
Assume that are real numbers taken as input in floating-point precision. [10 marks]

Idea: Substitute in to get and use its roots to get the roots of
If are the two complex roots of then the four roots of will be
where,

Proof: Let where Then,

Thus, the square roots of are

Sample inputs and outputs:


Enter a, b, c: 1 0 1
Roots = 0.707107+0.707107i, -0.707107+0.707107i, 0.707107-0.707107i,
-0.707107-0.707107i
Enter a, b, c: 1 0 2
Roots = 0.840896+0.840896i, -0.840896+0.840896i, 0.840896-0.840896i,
-0.840896-0.840896i
Enter a, b, c: 1 4 2
Roots = 0.765367i, -0.765367i, 1.847759i, -1.847759i
Enter a, b, c: 1 4 4
Roots = 1.414214i, -1.414214i, 1.414214i, -1.414214i
a03-2. [Median] Five distinct integers are given as input. Using as few comparisons as possible, find and
print their median. The median is larger than exactly two out of the other four elements. Your code
should make as few comparisons as possible, and will be given full credit only if it uses at  most
8 comparisons. [10 marks]
Sample inputs and outputs:
Enter a, b, c, d, e: 4 7 2 6 8
Median = 6
Enter a, b, c, d, e: 2 4 7 8 6
Median = 6
Enter a, b, c, d, e: 2 4 5 7 8
Median = 5
Enter a, b, c, d, e: 8 7 5 4 2
Median = 5
a03-3. [OTP] Write a program to generate and print a 4-digit OTP against a 10-digit phone number taken as
input in long long int format, using the rules stated below. You must use switch-case
statements. Let the phone number be and the OTP be . Let be a
pseudo-random number in the interval Your program should generate and print its value.
(1) If 7 or 9, then

(2) If 6 or 8, then

(3) If then
Note: For two positive integers and is the remainder obtained when is divided by In C
language, is written as a%b. [10 marks]

Sample inputs and outputs:

Enter a phone number: 7290092920


Enter a seed: 1
Random number m = 4
OTP = 3314

Enter a phone number: 7290092920


Enter a seed: 2
Random number m = 3
OTP = 4502

Enter a phone number: 6353002244


Enter a seed: 1
Random number m = 4
OTP = 4543

Enter a phone number: 6353002244


Enter a seed: 2
Random number m = 3
OTP = 1242

Enter a phone number: 4255676762


Enter a seed: 1
Random number m = 4
OTP = 5115

Enter a phone number: 4255676762


Enter a seed: 2
Random number m = 3
OTP = 7117

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