Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

PDS Lab Question Paper

2024-25 Spring
Section 7

You must submit all files to: https://moodlecse.iitkgp.ac.in/moodle/course/view.php?id=560


by 5 PM. Try submitting a few minutes before time. No late submission will be allowed.

Week 0: 8-Jan-2024 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
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.

Week 1: 15-Jan-2024 Data types, expressions, operators, if-else

a01-1 [Mixed data types]


Given as input two positive integers a and b and a real number x, compute and print the values of the
following expressions without using the math library:
Ya] QaU ´a¯ ´´ a ¯ ¯2 ´a¯
x, x, x, x `2 x ` 1.
b b b b b
Note: For any positive real number r, tru denotes the largest integer in r0, rs, and rrs denotes the smallest
integer in rr, 8q. For example, t3.14u “ 3 and r3.14s “ 4; t3u “ r3s “ 3. 2 21 ˆ 4 “ 10

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

a01-2 [Line intersection]


There are two straight lines ax ` by ` c “ 0 and px ` qy ` r “ 0, where a, b, c, p, q, r are all integers and given
as input. (a) Check whether they are parallel, and if not, (b) find their point of intersection. Its coordinates
should be printed up to the 3rd decimal place (using %0.3f instead of %f in printf). 5 ` 5 “ 10

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)

a01-3 [Repeated fraction]


1 1 1
Given as input a positive real number x, find the value of x ` ` ` .
x 1 1 1
x` x` `
x x 1
x`
x
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. 10

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

Week 2: 22-Jan-2024 switch-case, loops

a02-1 [Operator symbol]


Write a program that takes as input two integers a and b, and a character from t+, -, *, /u. With a and b as
the 1st and the 2nd operands respectively, and with the given character as the operator, it has to compute
and print the value of the corresponding expression. To read the operator, use scanf("\n%c", &operator);
or scanf(" %c", &operator); because otherwise the <Enter> or white-space will be mistakenly read as
the operator. You must use switch-case. 10

Examples
Enter a and b: 7 3 Enter a and b: 19 4
Enter the operator: - Enter the operator: /
Answer = 4 Answer = 4.750

a02-2 [First- and last-digit sums of positive integers]


Write a program that takes n positive integers as input and computes the sum of their first digits and the
sum of their last digits. You should not use array or any user-defined function or math.h. 10

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.

a02-3 [Cyclic shift of digits]


User supplies a positive integer n. Your program should (a) first find its number of digits and (b) then shift
its digits cyclically towards right—one digit at a time—which should continue until you get back the original
value of n. You should not use math.h. 5`5

Examples
Enter n: 3 Enter n: 1324
#digits = 1 #digits = 4
3 4132
2413
3241
1324

Enter n: 250 Enter n: 841397


#digits = 3 #digits = 6
25 784139
502 978413
250 397841
139784
413978
841397

Page 5
Week 3 29-Jan-2024 1D arrays

Week 3: 29-Jan-2024 1D arrays

a03-1 [Keep max]


Declare an array a of 7 integers. Using a loop, take exactly 7 integers as input from the user, and insert
each of them in a if it lies in the interval r1, 7s. However, if the user enters an element more than once, then
insert it exactly once in a. If some rear part of a remains unfilled after this, then fill it with 0’s. Don’t use
any extra array.
1. Print the elements of a.
2. Change all but the largest element of a to 0 and print the elements of a. 3`7

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

Enter 7 integers: 6 3 5 8 7 2 9 Enter 7 integers: -1 -5 -1 9 8 -2 23


Elements in a[]: 6 3 5 7 2 0 0 Elements in a[]: 0 0 0 0 0 0 0
Modified a[]: 0 0 0 7 0 0 0 Modified elements: 0 0 0 0 0 0 0

a03-2 [Simple fractions]


f r2is
Declare an array f of 10 positive integers. Note that f r2i`1s is a fraction for i “ 0, 1, . . . , 4. So, the array
f will effectively contain 5 fractions (not necessarily distinct). Fill it up with user input. Assume that the
user gives only positive integers.
1. Print the fractions stored in f [separated by commas and terminated by a full-stop].
2. Convert every fraction into its simple form and store its numerator and denominator back into f . Print
the fractions in f [separated by commas and terminated by a full-stop]. 3`7

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.

Enter 10 integers: 18 120 120 18 42 6825 2178 7293 7293 2178


Fractions in f[]: 18/120, 120/18, 42/6825, 2178/7293, 7293/2178.
Simple fractions: 3/20, 20/3, 2/325, 66/221, 221/66.

Page 6
Week 3 29-Jan-2024 1D arrays

a03-3 [Max and Min]


Declare an array a of 10 integers. Using a loop, take exactly 10 integers (need not be distinct) as input and
insert them in a.
1. Print the elements of a.
2. Find and print the largest and the smallest elements of a.
Print also the number of comparisons used by your code.
Don’t use any extra array and try to use as few comparisons as possible. 3`7

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

Week 4: 5-Feb-2024: 2024S Functions

a04-1 [Roots of a quadratic equation]

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

Enter a, b, c: 1 3 2 Enter a, b, c: 0.01 2 0.2


-1.000, -2.000 -0.100, -199.900

a04-2 [Two-summand combination]


Given a positive integer n, find in how many ways it can be written as a sum of 1 and 2 as the summands.
For example, if n “ 4, then there are five ways only: 1 ` 1 ` 1 ` 1, 1 ` 1 ` 2, 1 ` 2 ` 1, 2 ` 1 ` 1, 2 ` 2.
You should use a recursive function and should not use any variable other than n. 10

Examples
Enter n: 1 Enter n: 2 Enter n: 3
#ways = 1 #ways = 2 #ways = 3

Enter n: 4 Enter n: 10 Enter n: 25


#ways = 5 #ways = 89 #ways = 121393

Page 8
Week 4 5-Feb-2024: 2024S Functions

a04-3 [Average of middle one-third]


You are given an array a[] of n distinct integers. Assume that n is a multiple of 3 and can be at most 999.
The middle one-third of the array means those of its elements none of which is among the smallest one-third
or among the largest one-third. For example, if a[] = {4, 9, -1, 7, 3, 13}, then its smallest one-third
is {-1,3}, the largest one-third is {9,13}, and so the middle one-third is {4,7}. Your code has to (a) read
the input data first and print the whole array, and (b) then compute the average of the middle one-third
elements. The function main() should be exactly as follows. 4 ` 6 “ 10

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

Week 5: 12-Feb-2024 Recap

Lab Test 1 is postponed to Feb 26.


Today’s problems (posted on moodle):
1. Compute and print Pascal’s Triangle.
2. Chapter 4 of Exercises: Problem 4 (Binomial coefficients).

Week 6: 26-Feb-2024 Lab Test 1

1st Lab Test.


You must reach the lab by 1:55 PM.

Page 10
Week 7 04-March-2024 Strings

Week 7: 04-March-2024 Strings

a06-1 [Make a palindrome]


(a) Given as input an alphanumeric string x, construct a string y by reversing x, and print y.
(b) Make a palindrome z “ xy and print it.
You should use two new arrays for y and z, and should not use the string library. Assume that x contains
at most 1000 characters including the null character. 5`5

Examples
Enter an alphanumeric string: a1
Reverse = 1a
Palindrome = a11a

Enter an alphanumeric string: iitkgp721302


Reverse = 203127pgktii
Palindrome = iitkgp721302203127pgktii

a06-2 [String of names]


(a) Scan as input a single string containing English characters with spaces, comma, and full-stop. Print the
string as it is. For example, it should scan the following as a single string:

Rudyard Kipling, Rabindranath Tagore, Hermann Hesse, Thomas Stearns Eliot.

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

Check also with this:


Rudyard Kipling, Rabindranath Tagore, Hermann Hesse, Thomas Stearns Eliot, Boris Pasternak,
Salvatore Quasimodo, Pablo Neruda, Czeslaw Milosz.

Page 11
Week 7 04-March-2024 Strings

a06-3 [0-dominated prefix]


A string w is said to be a prefix of a string x if x starts with w. For example, 0101 has 4 prefixes: 0, 01,
010, and 0101. Given a binary string x (i.e., an array of the characters 0 and 1), check whether in every
prefix of x the number of 0’s is at least the number of 1’s. For example, 01010011 is a yes-instance (i.e.,
0-dominated prefix), while 01010110 is not.

Examples
Enter a binary string: 0 Enter a binary string: 1
Yes. No.

Enter a binary string: 01 Enter a binary string: 10


Yes. No.

Enter a binary string: 0101 Enter a binary string: 0110


Yes. No.

Enter a binary string: 00101100 Enter a binary string: 00111000


Yes. No.

Page 12
Week 8 11-March-2024 2D arrays

Week 8: 11-March-2024 2D arrays

a07-0 [Tutorial: Image fading]


An image is a 2-dimensional in which every element represents the color of a pixel. Thus, an image with r
rows and c columns contains the colors of exactly r ˆ c pixels.
An image may be colored or in gray scale. A gray-scale image is one in which the color is in gray shade,
expressed as an integer ranging from 0 (absolute black) to 255 (absolute white). PGM (portable gray map)
is used to represent gray-scale images. In ASCII format, a PGM file contains the following:
1. Line 1: P2
2. Line 2: values of c and r (in this order)
3. Line 3: 255
4. Line 4 to Line p3 ` r ˆ cq: values of the pixel colors in row-major order
Optionally, it may contain one or more comment lines (e.g. # created by ...) just after Line 1. You may
assume that in your input PGM files, there is no comment line.
Given such an image as input, our task is to fade the image and to save it as PGM and PNG files (Figure 1).
To convert from PGM to PNG, you can use the convert command in Linux. When you run the code by
./a.out < m1a.pgm > m1a_0.pgm, the sign < instructs to take input from the file m1a.pgm, and the sign >
instructs to write the output to the file m1a_0.pgm.

Examples
gcc a07-0.c
./a.out < m1a.pgm > m1a_0.pgm
convert m1a_0.pgm m1a_0.png

./a.out < m1.pgm > m1_0.pgm


convert m1_0.pgm m1_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

a07-1 [Image inversion]


Given a PGM image as input, invert it and save it as PGM and PNG files (Figure 2).

Figure 2: Image inversion.

a07-2 [Object boundaries]


Given a PGM image as input, highlight the boundaries of its object in black color, and save the result as
PGM and PNG files (Figure 3).
You should define k as a parameter using #define to fix the thickness of the boundaries. An object pixel
px, yq should be considered as a boundary pixel only if the axis-parallel square centered at px, yq and of length
2k ` 1 intersects the background. Note that this square will contain p2k ` 1q ˆ p2k ` 1q pixels. As shown in
Figure 3, you should color the boundaries in black, and the interiors in light gray (value 230), keeping the
background white.

Figure 3: Object boundaries in two images for k “ 1, 2, 5.

Page 14
Week 8 11-March-2024 2D arrays

a07-3 [Image on chequerboard]


Given a PGM image as input, overlay it on a chequerboard. You should define ` as the length of a square of
the chequerboard, using #define. As shown in Figure 3, you should use appropriate colors to get the effect.

Figure 4: Overlaid images on chequerboards ` “ 5, 10, 55.

Page 15
Week 9 18-March-2024 Pointers, dynamic memory allocation, sorting, searching

Week 9: 18-March-2024 Pointers, dynamic memory allocation, sorting, searching

a08-1 [Intersection of arrays]


• Take in as input two numbers m and n, and dynamically allocate two integer arrays a[] and b[] to store
m and n elements, respectively. Assume that m ď n.
• Populate the arrays a[] and b[] using two successive function calls from main():
populateArray(a, m);
populateArray(b, n);
The prototype of the function should be void populateArray(int *a, int n). The user may supply
the same element more than once; you should insert all without checking anything.
• Write a function of the prototype void findInter(int *a, int m, int *b, int n) and call it from
main() to find (and print) the intersection of a[] and b[], so that the intersection has distinct elements
only. You can dynamically allocate and use at most one additional array of size m in findInter() to
store the elements of intersection. 2`3`5

a08-2 [Sorting a nearly-sorted array]


An array a[] with distinct elements is said to be nearly sorted if and only if the index of every element in
a[] differs by at most 2 from its index when a[] is sorted in increasing order.
(a) Take in as input a number m and dynamically allocate an array a[] for m integers. Populate a[] by
calling populateArray(a, m) from main(). Assume that the user supplies a nearly-sorted sequence of
distinct elements.
(b) Write a function void specialSort(int *a, int m) that takes an array as input, sorts it with fewest
swaps, and prints its elements along with the number of swaps at the end. Call this function from main()
to get the output. 3`7
The code should be as follows.
#include <stdio.h>
#include <stdlib.h>

void populateArray(int *a, int m) {


...
}

void specialSort(int *a, int m) {


...
}

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

a08-3 [Interval search]


(a) Take in as input a number m and dynamically allocate an array a[] for m integers. Populate a[] by
calling populateArray(a, m) from main(). Assume that the user supplies a sorted sequence of distinct
elements. 3
(b) Write a function int intervalSearch(int *a, int m, int u, int v) that takes an array as input
and returns the number of elements in the array lying in the interval [u,v]. You must use binary search
in intervalSearch to get the answer. For this you should write a function for binary search and call it
twice from intervalSearch. Call this function from main() to get the output. 7

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

Enter the number of integers: 5


Enter 5 integers in sorted order: 2 3 5 7 9
Enter the range [u, v]: 10 15
Number of elements in the range [10, 15]: 0

Enter the number of integers: 5


Enter 5 integers in sorted order: 2 3 5 7 9
Enter the range [u, v]: 3 8

Page 17
Week 9 18-March-2024 Pointers, dynamic memory allocation, sorting, searching

Number of elements in the range [3, 8]: 3

Enter the number of integers: 10


Enter 10 integers in sorted order: 3 7 9 11 21 31 32 33 37 40
Enter the range [u, v]: -2 15
Number of elements in the range [-2, 15]: 4

Enter the number of integers: 10


Enter 10 integers in sorted order: 3 7 9 11 21 31 32 33 37 40
Enter the range [u, v]: 11 31
Number of elements in the range [11, 31]: 3

Enter the number of integers: 10


Enter 10 integers in sorted order: 3 7 9 11 21 31 32 33 37 40
Enter the range [u, v]: 10 35
Number of elements in the range [10, 35]: 5

Page 18
Week 10 01-April-2024 Structures & Linked List

Week 10: 01-April-2024 Structures & Linked List

a09-1 [Rational number sorting]


Define a datatype as a structure named rational to represent rational numbers. It will contain two integers
named numerator and denominator.

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

Enter the number of elements: 2


Enter the elements: 3/8 1/7
Sorted elements: 1/7 3/8

Enter the number of elements: 3


Enter the elements: 1/2 3/4 1/7
Sorted elements: 1/7 1/2 3/4

Enter the number of elements: 4


Enter the elements: 1/2 1/3 1/2 1/4
Sorted elements: 1/4 1/3 1/2 1/2

Enter the number of elements: 5


Enter the elements: 3/4 -5/16 2/4 7/8 -1/4
Sorted elements: -5/16 -1/4 2/4 3/4 7/8

Enter the number of elements: 12345678900


Insufficient memory during malloc, program terminated...

Page 19
Week 10 01-April-2024 Structures & Linked List

a09-2 [Rational complex number addition]


Use the structure rational of a09-1. Define another datatype for rational complex number as a structure
named rat_complex that contains two numbers of rational datatype—first for the real and the second
for the imaginary. Declare two rational complex numbers of the above structure, scan their values during
execution, add them, and print the result as a rational complex number in which the real and the imaginary
parts are in the reduced form (i.e., GCD of the numerator and the denominator is 1). You should write
a function for GCD to use it for converting the result of addition to its reduced form (e.g., 5{6 ` 2{3 “
p15 ` 12q{18 “ 27{18 reduces to 3{2).

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)

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)

Enter the 1st complex number: 112/213 -145/21


Enter the 2nd complex number: 210/456 23/56
Addition result = 15967/16188 + i(-1091/168)

Page 20
Week 10 01-April-2024 Structures & Linked List

a09-3 [Linked list]


Insert integer elements in a linked list (initially empty) in increasing order, discarding duplicate elements,
stopping by 0. After insertion is over, print the list elements and the addresses of their nodes using %p.

#include <stdio.h>
#include <stdlib.h>

typedef struct mynode{


int x;
struct mynode *next;
} node;

node *insert_linked_list(node *head, int x){


...
}

void print_linked_list(node *p){


...
}

int main(){
int element;
node *head = NULL;

printf("Enter nonzero elements (0 to stop): " );


...

printf("Elements of linked list in increasing order: ");


print_linked_list(head);
return 1;
}

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]

Enter nonzero elements (0 to stop): 3 -2 4 -5 1 0


Elements of linked list in increasing order:
-5 (address = 0x55c635fafb20)
-2 (address = 0x55c635fafae0)
1 (address = 0x55c635fafb40)
3 (address = 0x55c635fafac0)
4 (address = 0x55c635fafb00)
[end]

Page 21

You might also like