Professional Documents
Culture Documents
Pds Lab Questions 2024S
Pds Lab Questions 2024S
2024-25 Spring
Section 7
Examples
Abdul Rao Joseph
23XY10091
Revise the above file to write something about your hobby, and resubmit it. The revised file should have
three or more lines.
Examples
Abdul Rao Joseph
23XY10091
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 23XY1009.
You can compile the C file to get the binary executable file as follows:
gcc a00-2.c
a00-3 [Division]
Write a C program that takes as input two integers a and b from the keyboard and prints the value of a{b
on the terminal. The value should be correct up to 6th decimal place. The name of your C file should be
a00-3.c
Examples
Enter a and b: 2 3
a/b = 0.666667
Note
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. You should submit the C file only. The name of your C file should be <assignment number>.c; for
example, for a01-1 it will be a01-1.c.
4. No library other than stdio.h can be used unless mentioned in the question.
5. You should compile use gcc.
For example, if your file is a01-1.c, then the compilation command is:
gcc a01-1.c
6. 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
7. The input-output format must be as in the examples. Otherwise there will be some penalty.
8. Marks will depend on the efficiency of your code. Try to use as few operations (addition, subtraction,
multiplication, and division) as possible.
Examples
Enter a and b: 2 1
Enter x: .5
Expression values = 1.000000, 1.000000, 1.000000, 4.000000
Enter a and b: 2 3
Enter x: .5
Expression values = 0.000000, 0.500000, 0.333333, 1.777778
Enter a and b: 17 5
Enter x: .123
Expression values = 0.369000, 0.492000, 0.418200, 2.011291
Week 1 15-Jan-2024 Data types, expressions, operators, if-else
Examples
Enter a, b, c: 1 -2 2
Enter p, q, r: 1 -2 0
Lines are parallel.
Enter a, b, c: 1 -2 2
Enter p, q, r: 3 -2 -2
Point of intersection = (2.000, 2.000)
Enter a, b, c: 11 -15 7
Enter p, q, r: -5 123 1
Point of intersection = (-0.685, -0.036)
Examples
Enter x: 1
Value of the expression = 2.900000.
Enter x: 2
Value of the expression = 3.244828.
Enter x: .125
Value of the expression = 8.369318.
Page 4
Week 2 22-Jan-2024 switch-case, loops
Examples
Enter a and b: 7 3 Enter a and b: 19 4
Enter the operator: - Enter the operator: /
Answer = 4 Answer = 4.750
Examples
Enter n: 3 Enter n: 4
Enter the numbers: 7 3 6 Enter the numbers: 7 30 916 1023
Answer = 16, 16. Answer = 20, 16.
Examples
Enter n: 3 Enter n: 1324
#digits = 1 #digits = 4
3 4132
2413
3241
1324
Page 5
Week 3 29-Jan-2024 1D arrays
Examples
Enter 7 integers: 6 3 5 4 2 7 1 Enter 7 integers: 5 3 9 3 5 6 -15
Elements in a[]: 6 3 5 4 2 7 1 Elements in a[]: 5 3 6 0 0 0 0
Modified a[]: 0 0 0 0 0 7 0 Modified a[]: 0 0 6 0 0 0 0
Note A fraction is said to be simple if the GCD of its numerator and denominator is 1. So, for any fraction
a{g
b , its simple form is b{g , where g “ GCDpa, bq. You should use a loop to compute the GCD for every fraction
a
of f to obtain its simple form. It can be computed as follows (try to understand why it’s correct).
int c;
while(b!=0){
c = b;
b = a%b;
a = c;
}
// After the loop ends, a is the GCD of the original a and the original b
Examples
Enter 10 integers: 6 3 5 15 30 12 3 8 30 42
Fractions in f[]: 6/3, 5/15, 30/12, 3/8, 30/42.
Simple fractions: 2/1, 1/3, 5/2, 3/8, 5/7.
Page 6
Week 3 29-Jan-2024 1D arrays
Examples
Enter 10 integers: 8 7 3 2 10 9 5 4 1 6
Elements of a: 8 7 3 2 10 9 5 4 1 6
min = 1, max = 10
#comparisons = ?
Enter 10 integers: 8 7 3 2 10 9 5 7 2 6
Elements of a: 8 7 3 2 10 9 5 7 2 6
min = 2, max = 10
#comparisons = ?
Enter 10 integers: 1 1 2 2 1 2 1 1 1 1
Elements of a: 1 1 2 2 1 2 1 1 1 1
min = 1, max = 2
#comparisons = ?
Page 7
Week 4 5-Feb-2024: 2024S Functions
Write a function findQuadRoots to compute the roots of ax2 ` bx ` c “ 0, where a ‰ 0. The parameters
a, b, c should be real and taken as arguments of that function. The function will print the roots but will not
return any value. The values of a, b, c should be scanned from main() and the function should be called from
main(). You can use the function sqrt of math.h to compute the roots. Then for compilation, you have to
use the command gcc a04-1.c -lm. Your code should be organized as follows. 10
#include <stdio.h>
#include <math.h>
void findQuadRoots(...){
...
}
int main() {
float a, b, c;
printf("Enter a, b, c: ");
scanf(...);
findQuadRoots(a, b, c);
return 0;
}
Examples
Enter a, b, c: 1 1 0 Enter a, b, c: 1 2 3
0.000, -1.000 -1.000 + 1.414i, -1.000 - 1.414i
Examples
Enter n: 1 Enter n: 2 Enter n: 3
#ways = 1 #ways = 2 #ways = 3
Page 8
Week 4 5-Feb-2024: 2024S Functions
int main(){
int a[999], n;
n = readInput(a);
printArray(a, n);
printf("Average of middle one-third = %0.3f\n", findMidAvg(a, n));
return 0;
}
Examples
Enter n: 3
Enter the elements:
4
7
5
Array elements = 4 7 5
Average of middle one-third = 5.000
Enter n: 3
Enter the elements:
3
9
3
3 already exists! Enter a new element: 5
Array elements = 3 9 5
Average of middle one-third = 5.000
Enter n: 6
Enter the elements:
4
9
-1
9
9 already exists! Enter a new element: 4
4 already exists! Enter a new element: -1
-1 already exists! Enter a new element: 7
4
4 already exists! Enter a new element: 3
3
3 already exists! Enter a new element: 13
Array elements = 4 9 -1 7 3 13
Average of middle one-third = 5.500
Page 9
Week 6 26-Feb-2024 Lab Test 1
Enter n: 15
Enter the elements:
21
23
-9
16
23
23 already exists! Enter a new element: 5
-2
-3
11
13
17
16
16 already exists! Enter a new element: 14
-5
11
11 already exists! Enter a new element: 18
31
32
Array elements = 21 23 -9 16 5 -2 -3 11 13 17 14 -5 18 31 32
Average of middle one-third = 14.200
Page 10
Week 7 04-March-2024 Strings
Examples
Enter an alphanumeric string: a1
Reverse = 1a
Palindrome = a11a
Suggested to use: gets() (may signal a warning during compilation) or scanf("%[^\n]s", <string array>);
Assume that the string contains at most 1000 characters including the null character.
(b) Assuming that the string is a list of names of persons separated by commas (with exactly one space after
every comma) and ending with a full-stop, print all names without any punctuation, in uppercase, and in
successive lines. Use of string library is prohibited. 3`7
Examples
Type in a proper string and press <Enter>:
Rudyard Kipling, Rabindranath Tagore, Thomas Stearns Eliot.
Scanned:
Rudyard Kipling, Rabindranath Tagore, Thomas Stearns Eliot.
Names:
RUDYARD KIPLING
RABINDRANATH TAGORE
THOMAS STEARNS ELIOT
Page 11
Week 7 04-March-2024 Strings
Examples
Enter a binary string: 0 Enter a binary string: 1
Yes. No.
Page 12
Week 8 11-March-2024 2D arrays
Examples
gcc a07-0.c
./a.out < m1a.pgm > m1a_0.pgm
convert m1a_0.pgm m1a_0.png
Figure 1: Image fading. Left: m1a.pgm (77 rows, 59 columns). Right: m1.pgm (549 rows, 549 columns).
Page 13
Week 8 11-March-2024 2D arrays
Page 14
Week 8 11-March-2024 2D arrays
Page 15
Week 9 18-March-2024 Pointers, dynamic memory allocation, sorting, searching
int main() {
int m;
printf("Enter m: "); scanf("%d", &m);
int *a = (________)malloc(m * _______________________);
if (a == _________) {
printf("Memory allocation failed.\n");
return 1;
}
populateArray(a, m);
specialSort(a, m);
return 1;
}
Page 16
Week 9 18-March-2024 Pointers, dynamic memory allocation, sorting, searching
Examples
Enter 5 distinct elements: 2 4 6 7 9
Array after Special Sort: 2 4 6 7 9
#swaps: 0
Enter m: 5
Enter 5 distinct elements: 6 4 2 9 7
Array after Special Sort: 2 4 6 7 9
#swaps: 2
Enter m: 10
Enter 10 distinct elements: 8 6 5 12 10 9 14 20 16 15
Array after Special Sort: 5 6 8 9 10 12 14 15 16 20
#swaps: 3
Enter m: 10
Enter 10 distinct elements: 6 5 9 8 12 14 10 16 20 15
Array after Special Sort: 5 6 8 9 10 12 14 15 16 20
#swaps: 6
Enter m: 10
Enter 10 distinct elements: 6 9 5 12 8 15 10 20 14 16
Array after Special Sort: 5 6 8 9 10 12 14 15 16 20
#swaps: 9
Examples
Enter the number of integers: 5
Enter 5 integers in sorted order: 2 3 5 7 9
Enter the range [u, v]: 6 6
Number of elements in the range [6, 6]: 0
Page 17
Week 9 18-March-2024 Pointers, dynamic memory allocation, sorting, searching
Page 18
Week 10 01-April-2024 Structures & Linked List
typedef struct{
int numerator, denominator;
}rational;
Given the value of n as input, dynamically allocate an array of n rational numbers, fill it up taking input
from the user, sort it in non-decreasing order using Bubble Sort, and print it after sorting. For sorting, you
should write the function of the following prototype and call it from main():
void bubbleSort(rational a[], int n) // n = number of elements in a[]
Use scanf("%d/%d",...) to take in rational numbers as input.
All variables in your code should be integers only. So, you cannot use division. For example, to determine
whether a rational p/q is larger than another rational r/s, check whether p*s > q*r.
Examples
Enter the number of elements: 1
Enter the elements: 1/2
Sorted elements: 1/2
Page 19
Week 10 01-April-2024 Structures & Linked List
Examples
Enter the 1st complex number: 5/6 14/8
Enter the 2nd complex number: 2/3 1/2
Addition result = 3/2 + i(9/4)
Page 20
Week 10 01-April-2024 Structures & Linked List
#include <stdio.h>
#include <stdlib.h>
int main(){
int element;
node *head = NULL;
Examples
Enter nonzero elements (0 to stop): 4 5 2 0
Elements of linked list in increasing order:
2 (address = 0x55f2d2036b00)
4 (address = 0x55f2d2036ac0)
5 (address = 0x55f2d2036ae0)
[end]
Page 21