0% found this document useful (0 votes)
98 views

Pds Lab Practice Questions

The document contains practice questions on programming concepts like expressions, conditionals, loops, arrays, strings, functions, structures. It has a total of 15 questions on loops ranging from simple tasks like printing ASCII values to more complex tasks like finding prime numbers or GCD of two numbers. The questions are meant to help practice and reinforce key programming concepts. Sample code solutions are provided for some questions to help with coding other similar problems.

Uploaded by

Manju Nath
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Pds Lab Practice Questions

The document contains practice questions on programming concepts like expressions, conditionals, loops, arrays, strings, functions, structures. It has a total of 15 questions on loops ranging from simple tasks like printing ASCII values to more complex tasks like finding prime numbers or GCD of two numbers. The questions are meant to help practice and reinforce key programming concepts. Sample code solutions are provided for some questions to help with coding other similar problems.

Uploaded by

Manju Nath
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

PDS Lab Practice Questions

Partha Bhowmick
CSE, IIT Kharagpur
http://cse.iitkgp.ac.in/~pb

November 4, 2023

Contents
1 Expressions and Conditionals 2

2 Loops 3

3 1D arrays 6

4 Strings 7

5 Functions 8

6 2D arrays 9

7 Structures 9

General note
1. Questions at the advanced level are marked with ∗.
2. Solution codes for some problems are given as links (e.g., Problem 1 in § 3). Clicking the links will open
the C codes. You should see those and try coding the other ones.

1
1 EXPRESSIONS AND CONDITIONALS 2

1 Expressions and Conditionals


1. (Arithmetic/algebraic expressions) Compute and print the values of the following expressions in
floating point (rounded off to 3rd decimal place), where a, b, c are integers given as input. You can use
the math library.
  √1
a b c √ √ 5 5
a + 2b + 3c, + + , (1.25a + 2.75a2 + 5.625a3 )(b2 /(1.25 + c3 )), 2a + 3b2 + c3 .
2 2 3 4

As the values should be real, the computations should be in the real domain. For example, if a = 3, b =
2, c = 1, then the value of a2 + 2b + 3c would be 1.500 + 1.000 + 0.333 = 2.833.
2. (Logical expressions) Compute and print the logical values (True or False) of the following expres-
sions, where a, b, c are the integers 0 or 1 given as input.

(!a) && (!b) && c


(a && b) || c
a && (b || c)
a && ((b || c)==0)
!(a && (b || c))
(a && (b || c)) == 0
(a && (b || c)) == 1
(!a) && ((!b) || c)
((a == 1) || ((b == 0) && (c == 1))) || ((a == 0) || ((b == 1) && (c == 0)))
a b c
3. (Largest fraction) Given as input six positive integers a, b, c, d, e, f , find a/the largest among d, e, f ,
using only integer computations.
4. (Sum of series) Without using any loop, compute and print the values of the following sums, where n
is a positive integer given as input.
n
X n
X n
X n
X
i, i2 , i3 , (i + 1)(i + 2)(i + 3).
i=1 i=1 i=1 i=1

5. (Prime digits) User supplies a positive integer having value at most 9999. Find the number of its
prime digits. For example, if the integer is 4267, then it has two prime digits (2 and 7).
6. (Pricing) Write a program to perform the following for a user who wants to purchase wood:
— Ask the user for the type of wood (’n’ for natural wood, ’s’ for synthetic wood).
— Ask for the amount (in kg) he wants.
— Compute and print the base price.
— Compute and print the GST.
— Compute and print the total price.
All prices should have two digits in the decimal place (use %0.2f format for this). Assume that price of
natural wood is Rs. 1100/kg, that of synthetic one is Rs. 780/kg, and the respective GSTs are 20% and
15% on the base price. For example, for 7.25 kg of natural wood, the base price is Rs. 7975.00, GST is
Rs. 1595.00, and total price is Rs. 9570.00.
7. (Leap year) Given a year as input, your program should print whether it is a leap year.
(Note: A year is not a leap year if it’s not divisible by 4. It’s a leap year if it is divisible by 4 but not
by 100; however, if it is divisible by 400, then it’s a leap year. So, while 2000 and 2020 are leap years,
2023 and 2100 are not.)
8. (Coefficient of restitution) A ball is just dropped from a height h1 . On striking the floor, it bounces
back and rises up to a height h2 . Given as input the floating-point values of h1 and h2 in meters, compute
the following.
(a) e = coefficient of restitution of the floor.
2 LOOPS 3

(b) h3 = height that the ball will rise through after hitting the floor for the 2nd time.
Take g = 9.80 m/s2 and ignore air drag. The ratio of velocity after collision and that before collision
is known as the coefficient of restitution. Recall the conventional equations of motion: v = u + at, s =
ut + 12 at2 , v 2 = u2 + 2as.
9. (Circle and line) C is a circle centered at (0, 0), and L is a straight line with equation y = mx + c.
The radius of C, and the values of m and c are given as input in floating-point format. Compute and
print whether L intersects C or just touches C or none. Print the coordinates of the point(s) of the
intersection if any.
−→ −→
10. (Triangle perimeter and area) Recall that the area of a triangle ABC is given by 21 AB × AC . If
(xi , yi ), i = 1, 2, 3, are the coordinates of its three vertices, then it is given by 12 x1 (y2 − y3 ) + x2 (y3 −
y1 ) + x3 (y1 − y2 ) . Given as input the coordinates of the three vertices of a triangle, compute and print
its perimeter and area. For perimeter computation, you can use the sqrt function of math library.

2 Loops
You should not use any array or function for these problems.
1. (ASCII of English alphabet) Print as integers (in decimal number system) the ASCII values of ’a’
to ’z’ and those of ’A’ to ’Z’. Don’t directly use in your code the value of any character. [printASCII.c]
2. (ASCII) Print all the characters with ASCII values from 0 to 127.
3. (Sum) Given n real numbers from the keyboard, find their sum. User input is n and the num-
bers. [sumNumbers.c]
4. (Sum of squares) Given n real numbers from the keyboard, find the sum of their squares. User input
is n and the numbers.
n
k 5 , without using math.h.
P
5. (5th-power sum) Given a positive integer n, find [5thPowerSum.c]
k=1
n
k k , without using math.h.
P
6. (Power sum) Given a positive integer n, find
k=1
7. (Bit-length) Given a positive integer n, determine the integer ` such that 2`−1 ≤ n < 2` . Here, ` is
called the bit-length of n. [bitLength.c]
8. (Joint bit-length) Given two positive integers m and n, where m ≤ n, determine the integer ` such
that 2`−1 ≤ m and n < 2` . Here, ` is called the joint bit-length of m and n.
9. (Decimal to binary, 8 bits) Given an integer n ∈ [0, 255], print its 8-bit binary representation. You
can use at most 3 integer variables and no character variables. [decimal2binary8bits.c]
10. (Decimal to binary, 16 bits) Given an integer n ∈ [0, 2562 − 1] = [0, 65535], print its 16-bit binary
representation. You can use at most 3 integer variables and no character variables.
11. (Reverse number) Given a positive integer in decimal number system, compute its reverse as an
integer (not simply as a string of digits). For example, the reverse of 3481 is 1843, and the reverse of
3480 is 843 (not 0843). [reverseNumber.c]
12. (Digit count) Given any integer in decimal number system, compute its number of digits. For example,
for 3180, it is 4.
13. (Factors) Find all the nontrivial factors of a given positive integer n, i.e., those lying in the interval
[2, n − 1]. [factors-of-integer.c]
14. (Primes) Find all the prime numbers less than a given positive integer n.
15. (GCD) Given two positive integers a, b, compute their GCD. Use the fact that if a ≤ b, then

b if a = 0
gcd(a, b) = (1)
gcd(b mod a, a) otherwise.
2 LOOPS 4

Since no function other than main() is allowed in this section, you have to do it iteratively using a
loop. [gcdIterative.c]
16. (Co-primes) Two positive integers are said to be co-prime with each other if their GCD is 1. Given a
positive integer n, find the co-primes that are all less than n. For example, for 9, they are 2, 4, 5, 7, 8.
17. (Two-prime sum) Given a positive integer n, determine whether it can be written as the sum of two
primes.
18. (Alphabet pyramid) Given the height (at most 26) of a pyramid, print it with a pattern made by the
English alphabet. For height 5, the pyramid should look as follows. [alphaPyramid.c]

A
A B A
A B C B A
A B C D C B A
A B C D E D C B A

19. (Alphabet triangle) Given the height (at most 26) of a right-angled triangle, print it with a pattern
made by the English alphabet. For height 5, the pyramid should look as follows.

A
A B
A B C
A B C D
A B C D E

20. (Egyptian fraction) A unit fraction means a positive fraction whose numerator is 1. An Egyptian
fraction is a finite sum of distinct unit fractions, such as 21 + 15 + 12 1
. An interesting fact is that any
a
fraction b with 0 < a < b can be expressed as an Egyptian fraction. Given a, b, your task is to compute
the Egyptian fraction for ab and express it as the increasing sequence of its denominators. For example,
7
if the fraction is 11 , then its Egyptian form is 12 + 18 + 88
1
, and your code should print 2, 8, 88.
All the variables in your code must be integers—to be precise, all operations should be in the integer
domain.
Here is the idea: Let q be the quotient and r the remainder when b is divided by a. Then,
a 1 1 1 a 1 h r i
= b = =⇒ < ≤ ∵ 0 ≤ < 1 .
b a
q + ar q+1 b q a
1 a 1
So, if r > 0, then report the unit fraction q+1 and continue with the rest (i.e., b − q+1 ); otherwise,
report 1q and stop. [egyptianFraction.c]
2 3
21. (ex as a finite sum) We know that in the infinite sum ex = 1 + x + x2! + x3! + · · · , the trailing terms
have negligible values. Given a positive value of x, compute the sum up to its nth term and return that
value as the approximate value of ex as soon as the nth term is found to be less than ε. Also print the
value of n. Needless to say, you should not use the math library. [expFiniteSum.c]
22. (sin, cos) Using x as input in the double-precision format, compute and print the values of sin x and
cos x using the following equations.
∞ ∞
X (−1)n 2n+1 X (−1)n 2n
sin x = x , cos x = x . (2)
n=0
(2n + 1)! n=0
(2n)!

Use #define DIFF 0.00001 to terminate the summation when the sum up to nth term differs by less
than DIFF from the sum up to (n − 1)st term. Needless to say, you should not use the math library.
23. (Max, Min) The user enters an even number of elements as positive integers one by one, and stops by
entering 0 (that is, the number of elements is not known beforehand). You have to find the largest and
the smallest among those elements, using as few comparisons as possible.
2 LOOPS 5

(Note: If the user enters n elements, then you can find the largest (or the smallest) using n − 1 compar-
isons. So if you find the largest and the smallest separately, then you may need 2n − 3 comparisons in
some cases. As n goes high, this is almost 2n. Can you think of a better technique for which it will be
around 3n/2? You can see the solution code of Problem 3 in § 3.)
24. (Number of combinations) You might be knowing that the number of combinations of n items,
chosen r at a time, is denoted by n Cr or nr . Its value is given by
 
n n! n(n − 1) · · · (r + 1)
= = . (3)
r r!(n − r)! (n − r)!

Given two non-negative integers n and r (≤ n), compute the value of nr , without using the math


library. [nCr.c]
Method 1: Compute the numerator and the denominator of the rightmost expression of Eq. 3, and obtain
the quotient of dividing the former by the latter to get the result. However, even for pretty small values
of n and r (for example, (n, r) = (17, 8)), the value of the numerator or the denominator may overflow
the bytes of an integer variable, resulting in wrong result. The following method works better.
Method 2: Use the following recurrence obtained from Eq. 3:

   1
n   if n = r
= n! n n−1 (4)
r = if n ≥ r + 1.
r!(n − r)! n−r r

So, starting
 from  i = r and icontinuing through i = r + 1, r + 2, . . . , n, iteratively compute the values of
r r+1 n i−1 i
 
r , r , . . . , r . To get r for i ≥ r + 1, we need to just multiply r by i−r .
Here is a result by the code where all variables are 4-byte integers:
Enter n and r: 17 8
Method 1: Answer = 231678208/362880 = 638
Method 2: Answer = 24310
The reason is that 17! actually evaluates to 355687428096000 in decimal number system.∗ Its binary
form† is 1010000110111111011101110110011011000000000000000, which consists of 49 bits and thus
overflows 4 bytes.
25. (Longest increasing contiguous subsequence) The user gives a sequence of positive integers and
stops with 0. Compute the lengths of the increasing contiguous subsequences (consecutively entered
numbers) and print the maximum among them. Assume that the first integer is not 0.
Example:
input = 6 7 2 9 7 2 3 4 8 6 7 8 0
output = 4 (subsequences: 6 7 | 2 9 | 7 | 2 3 4 8 | 6 7 8) [longestIncContigSubSeq.c]
26. (Smallest monotonic partition) The user gives a sequence of positive integers and stops with 0.
Compute the size of its smallest monotonic partition. Assume that the first integer is not 0. (A partition
breaks a sequence into contiguous subsequences; it is monotonic if each subsequence is either increasing
or decreasing; the number of subsequences is its size; smallest monotonic partition is the one for which
the size is minimum.)
Example:
input = 6 2 2 9 7 2 3 4 6 6 7 8 0
output = 5 (partition: 6 2 | 2 9 | 7 2 | 3 4 6 | 6 7 8)
∗ 27. (Cubic equation) The input to this problem are three integer coefficients a, b, c of a cubic equation
f (x) := x3 + ax2 + bx + c = 0. Any cubic polynomial, as we know, has either one or three real roots.
You have to find an approximate real root of f (x) by the following method.
Maintain a pair of real variables x1 and x2 with the property that f (x1 ) ≤ 0 and f (x2 ) ≥ 0.‡ Initialize
∗ Obtained online at https://www.calculatorsoup.com/calculators/discretemathematics/factorials.php
† Obtained online at https://www.rapidtables.com/convert/number/decimal-to-binary.html
‡ If f (x ) ≤ 0 and f (x ) ≥ 0, then a real root of f is guaranteed to exist in the interval [x , x ].
1 2 1 2
3 1D ARRAYS 6

x2 = |a| + |b| + |c| and x1 = −x2 so that f (x1 ) ≤ 0 and f (x2 ) ≥ 0.§ In each step you will refine the
interval [x1 , x2 ] until you find a value x such that |f (x)| < 0.0001;¶ then you will return x. All that is
now left is to specify how to refine the interval [x1 , x2 ]. Find the point of intersection (x, 0) between
the x-axis and the line segment joining (x1 , f (x1 )) and (x2 , f (x2 )). If |f (x)| < 0.0001, return x as a
root. Else, update x1 to x if f (x) ≤ 0 or update x2 to x otherwise, so that the invariant f (x1 ) ≤ 0 and
x+x2 x+x2

f (x2 ) ≥ 0 is maintained. Further, for the former case, update x 2 to 2 if f 2 ≥ 0; similarly, for
the latter case, update x1 to x+x x+x1

2
1
if f 2 ≤ 0.

3 1D arrays
Note the following regarding the problems stated in this section.

1. All arrays here are one-dimensional. The number of elements that an array can store, is referred to as
the size of array. However, the full array may not be used. For example, the size of an array A[ ] may
be 10, but we store and work with only 7 elements from A[0] to A[6], whereby A[7] to A[9] are of no use.
2. Unless mentioned, for an array, assume that its size is at most 1000.
3. Unless mentioned, assume that the input elements need not be distinct.
1. (max) Given a positive integer n as the first input, take in as the next input n integer elements from
the user, store them in an array, and find the largest among them. The total number of comparisons
should be at most n − 1. [arrayMax.c]
2. (Check if sorted) Given a positive integer n as the first input, take in as the next input n integer
elements from the user, store them in an array, and check if they are sorted in non-decreasing order.
The total number of comparisons should be at most n − 1.
3. (max and min) Given a positive even integer n as input, take in as the next input n integer elements
from the user, store them in an array, and find both the largest and the smallest among them. The total
number of comparisons should be at most 3n 2 − 2. [arrayMaxMin.c]
4. (Largest-sum pair) Given a positive integer n as the first input, take in as the next input n integer
elements from the user, store them in an array, and find a pair of elements that add up to the largest
value over all pairs. For example, for [−2, 5, −1, 3, −1, 2, 3], the largest sum is 5 + 3 = 8. (Hint: It’s the
pair of max and 2nd max.)
5. (Check distinctness) Given a positive integer n as the first input, take in as the next input n inte-
ger elements from the user, store them in an array, and check whether all elements in that array are
distinct. [arrayCheckDistinctness.c]
6. (Pair sum) Given a positive integer n and an integer k, followed by n integer elements, store them in
an array and determine whether there are two elements with distinct indices in the array such that their
sum is k.
7. (Fibonacci sequence) The Fibonacci sequence {f (i) : i = 0, 1, 2, . . .} is defined as

f (0) = 0, f (1) = 1, and f (i) = f (i − 1) + f (i − 2) if i ≥ 2. (5)

Given a non-negative integer n, compute f (i) for i = 0, 1, 2, . . . , n, store them in an array of n + 1


integers, and print the elements of this array. [arrayFibonacci.c]
8. (Binomial coefficients) From the following recurrence of binomial coefficients
     
n n! n−1 n−1
= = + , (6)
k k!(n − k)! k−1 k

compute the values of nk for k = 0, 1, . . . , n, using the value of n as input. You should do it with a


single array.
§ Do you see why?
¶ In real-domain computation, this practically means f (x) = 0.
4 STRINGS 7

9. (Array union) Given two arrays A and B containing m and n integer elements respectively, find the
elements in A ∪ B, store them in another array C, and print C. Assume that all elements of A are
distinct, and so also for B, although an element of A may be present in B. [arrayUnion.c]
10. (Array intersection) Given two arrays A and B containing m and n integer elements respectively,
find the elements in A ∩ B, store them in another array C, and print C. Assume that all elements of A
are distinct, and so also for B.

4 Strings
Note the following regarding the problems stated in this section.
1. Unless mentioned, assume that a string will contain at most 1000 characters including the null character,
i.e., ’\0’.
2. Unless mentioned, assume that a string contains at least one non-null character.
3. A newline means the character ’\n’, which appears as the key Enter on the keyboard.
4. By “any character” or “an arbitrary character”, we mean a character that can be typed in from the
keyboard; for example, all lowercase and uppercase letters, digits, space, newline, punctuation marks,
and the symbols:
~ ! @ # ^ % $ & * ( ) _ + - / { } [ ] | \ < >

1. (Scan any character) Scan as input any character other than newline; print it as character and also
print its ASCII value. The user will type in that character followed by a newline. [scanAnyChar.c]
2. (Alphanumeric character) Scan as input any character other than newline, and check whether it is
alphanumeric (without using any ASCII value or the string library). The user will type in that character
followed by a newline.
3. (Scan any two characters) Scan as input two arbitrary characters, one at a time, using two scanf
calls, and finally print them by a single printf at the end. The user will type in each character followed
by a newline. [scanTwoChars.c]
4. (Scan integer and character) Scan first as input an integer and then a non-white-space character,
using two scanf calls, and finally print them by a single printf at the end. [scanNumChar.c]
5. (Scan an arbitrary string) Scan as input a string of arbitrary characters (alphanumeric, space,
symbols, etc.), and print it. For example, it should scan the following as a single string:
iit %kgp $2023, Autumn; & ‘‘www.ac.in!’’ +-*/~pb*& (@CSE) #_^{PDS}
The user will type in the characters and terminate with a newline. The newline shouldn’t be treated as
a part of the string.
6. (Bit flip) Given as input any arbitrary character other than a newline, print the new character formed
by flipping each bit excepting the leftmost one. Do it by two methods—once using loop, and once
without loop. [bitFlip.c]
7. (Byte reversal) Given as input a byte as character, print the new character formed by reversal of the
7-bit string formed by all but the leftmost one.
8. (String length) Without using the string library, print the length of an alphanumeric string. The
length of a string is defined as the number of its alphanumeric characters. Assume that the string has
at least one alphanumeric character. [stringLength.c]
9. (Odd characters) Without using the string library, print the characters of an alphanumeric string oc-
curring in odd positions. Assume that the string has at least one alphanumeric character. [stringOddChars.
c]
10. (Prefix or/and Suffix) A string x is said to be a prefix of a string y if y starts with x, and a suffix
of y if y ends with x. Given as input two strings from the English alphabet and each of length at most
100, find out if one of them is a prefix or/and a suffix of the other. Your code should not distinguish
5 FUNCTIONS 8

between lowercase and uppercase. For example, RAM (or Ram or ram) is a prefix of Raman, while MAN is a
suffix. Suggested library functions: strlen and strncmp.

5 Functions
You should not use any library other than stdio.h, unless mentioned otherwise.

1. (Value of a quadratic function) Write a function to compute the value of ax2 + bx + c with a, b, c, x
as real values and taken as arguments in that order. Scan their respective values from main(), call that
function from main(), and print its returned value from main(). [quadraticFunction.c]
2. (Values of a quadratic sequence) Write a function to compute the value of x2 + bx + c with b and
c as positive integers and x as real, taken as arguments in that order. Scan their respective values from
main(), call that function from main() for every integer value of s ranging from 0 to b, and print every
returned value from main(). That is, the function will be called b + 1 times from main().
3. (Arithmetic and geometric means) Write a function to compute and print the arithmetic mean and
the geometric mean of n real numbers, with n and an n-element array as arguments. The value of n and
the array elements will be supplied as input from main(). The math library can be used, but only for
the pow() function. [functionAMGM.c]
4. (Median) Write a function to compute and return the median of n integers, with n and an n-element
array as arguments. Scanning the value of n along with the array elements, and printing the median,
should all be done from main(). Assume that n is odd and all elements are distinct. Then, exactly n−1
2
elements of the array will be smaller that the median—a fact that can be used to write the function.
5. (String reversal) Write a function that takes an alphanumeric string as argument and reverses it. For
example, if it is IIT, then the reverse string created by the function will be TII. Scanning the input
string and printing the reverse one must be done from main(). [stringReversal.c]
6. (Palindrome) Write a function that takes an alphanumeric string as argument and returns 1 if it is a
palindrome, and 0 otherwise. For example, IIT is not a palindrome but ITI is.. After a string is taken
in from main(), the function should be called from main() with that string as input, and the value
returned by the function should be printed from main().
7. (String of two strings) Write a function that takes three alphanumeric strings as arguments and
creates its third string by placing the 2nd one after the 1st, with a space in between. For example, if
they are IIT and Kharagpur, then the third string created by the function will be IIT Kharagpur (not
IITKharagpur). The two input strings have to be taken in from main() using %s format in scanf(),
and the third string should be printed from main(). [stringOf2strings.c]
8. (Interleaved strings) Write a function that takes three alphanumeric strings as arguments and creates
its third string by interleaving the first two, assuming that the first string is not longer than the second.
For example, if they are IIT and Kharagpur, then the interleaved string created by the function will be
IKIhTaragpur. The two input strings have to be taken in from main() using %s format in scanf(), and
the third string should be printed from main().
9. (GCD function, recursive) Write a recursive function of the prototype int gcd(int, int) to com-
pute the GCD of two numbers, using Eq. 1. Take in two positive integers m, n from main(), call
gcd(m,n) from main(), and print its returned value from main(). [functionGCDrecursive.c]
10. (GCD function, iterative) Write an iterative version of the GCD function using Eq. 1, keeping the
main() same as the recursive version. [functionGCDiterative.c]
11. (Fibonacci function, recursive) Write a recursive function of the prototype int f(int) to compute
the nth Fibonacci term, f (n), using Eq. 5. Take in a positive integer n from main(), call f(n) from
main(), and print its returned value from main().
12. (Fibonacci function, iterative) Write an iterative version of the Fibonacci function using Eq. 5,
keeping the main() same as the recursive version.
7 STRUCTURES 9

6 2D arrays
1. (Matrix addition) Compute the addition of two matrices with compatible dimensions (between 1 to
10) and elements (integers) as input, store it in a new matrix, and print its elements on the terminal.
2. (Matrix multiplication) Compute the multiplication of two matrices with compatible dimensions
(between 1 to 10) and elements (integers) as input, store it in a new matrix, and print its elements on
the terminal. [matrixMult.c]
3. (Saddle point) An element x is said to be a saddle point in a 2D array if it is smallest in its row and
largest in its column. Given a 2D array with m rows and n columns, find whether it has any saddle
point.
4. (Max-sum 3 × 3 sub-matrix) Given a matrix with m ≥ 3 rows and n ≥ 3 columns, find a 3 × 3
sub-matrix such that the sum of its elements is maximum.
5. (4-adjacent local max) An element x in a 2D array is said to be a local max if x is larger than all its
four adjacent elements — left, right, above, and below. If x lies on the array boundary, then it it may
be disregarded due to insufficient adjacency. Given such an array with m ≥ 3 rows and n ≥ 3 columns,
find all its local maxima. [adj4Max2D.c]

7 Structures
1. (Complex number addition) Define a datatype for complex number as a structure named complex
that contains two floating-point numbers—first for the real and the second for the imaginary. Declare
two complex numbers of the above structure, scan their values during execution, add them, and print
the result. [complexAdd.c]
2. (Complex number multiplication) Define a datatype for complex number as a structure named
complex that contains two floating-point numbers—first for the real and the second for the imaginary.
Declare two complex numbers of the above structure, scan their values during execution, multiply them,
and print the result.
3. (Shopping) Write a program to do the following.
(a) Read a database of items of the structure Item defined shortly.
(b) Print the items and their unit price after adding 20% to their unit prices in the database. The
unit price is actually the cost price. The sale price is set with 20% profit.
(c) Take in a customer’s options (ID and quantity). The customer is the user.
(d) Print the total price to be paid by the customer.
You can copy-paste the input from shopping-input1.txt or shopping-input2.txt. The code should
be written in the following line. [shopping.c]

#include<stdio.h>

typedef struct{
char name[10], unit[10];
float price_per_unit, quantity;
}Item;

// Write here the functions readStock and customer

int main(){
Item list[100];
int n;
readStock(list, &n);
7 STRUCTURES 10

customer(list, n);
return 0;
}

4. (Stock management) Write a program to do the following.


(a) Read a database of items of the same structure as in the last problem.
(b) Take in the shopkeeper’s option as input: minimum stock of every item. The shopkeeper is the
user.
(c) Print each item and its unit price if its stock is less than the minimum.
(d) Print the total price of the above items if the shopkeeper wants to purchase all the above items in
order to raise their stocks to the minimum.
You can copy-paste the input from shopping-input1.txt or shopping-input2.txt. The code should
be written in the following line.

#include<stdio.h>

typedef struct{
char name[10], unit[10];
float price_per_unit, quantity;
}Item;

// Write here the functions readStock and replenish

int main(){
Item list[100];
int n;
readStock(list, &n);
replenish(list, n);
return 0;
}

You might also like