Lab Quizzes
Lab Quizzes
Instructions
• You have to write two programs in this lab quiz to solve the two questions given to you.
• Please use only the concepts covered in the syllabus of this quiz to solve the questions (only
exception to this rule: you can use for loops if you wish).
• Each question carries 10 marks, which will be assigned after a demo+viva with your TA later.
• Create a sub-directory with name submission YourRollNumer on the Desktop, as men-
tioned in the submission instructions. In this directory, you must create two C files to write your
programs: q1.c for question 1, and q2.c for question 2.
• Please test your code thoroughly before you submit it. Test for several possible inputs, not just the
ones shown as examples in the question.
• Write your code neatly, with proper indentation and comments. You will be assigned marks for
code readability.
• The TAs will only assist you if you are facing any issues with the lab machines while writing code
or submitting it. Please do not ask the TAs for help in debugging your code.
• After writing your progams, follow the submission instructions provided. You must run the com-
mand check, followed by submit on the terminal. Please call your TA when running submit.
• After you submit, you may open Moodle, and upload your code under the labquiz1 assignment.
This code is only for your records, and we will not grade it.
• Please double check that you submit the correct files. We will not permit any code changes (even
minor ones) during grading.
1
Divison D1-A, Question 1: Number triangle
Write a program that takes a positive number N as input from the user, and prints a number triangle
as illustrated below. The triangle has N rows. The first row of the triangle contains the number 1, the
second row contains the numbers 1 and 2, and so on. There should also be spaces between the numbers
for easy readability.
A sample run of the program is shown below.
Enter number: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Enter number: 15
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2
Divison D1-A, Question 2: Polynomial
Write a program to compute a polynomial function a0 + a1 x1 + a2 x2 + ... + an xn of degree n ≥ 0,
where x and all coefficients ai are integers. Your program reads the values of n and x as inputs from the
user. The program also takes as input all the n + 1 coefficients a0 to an one after the other. The program
then computes the polynomial and prints its final value.
Please note the following when you write the program.
• You must evaluate all terms with addition and multiplication operations. Specifically, you cannot
use any library functions to calculate xn .
• You must reuse the value of xn−1 when calculating xn so that your code runs efficiently.
• Be mindful of integer overflow, and do not run your code with very large values.
3
Divison D1-B, Question 1: Star triangle
Write a program that takes a positive number N as input, and prints a triangle as illustrated below. The
triangle has an increasing number of star (*) symbols from 1 to N, and then a decreasing number of
symbols back to 1. There are no spaces between the symbols in a row.
A sample run of the program is shown below.
Enter number: 5
*
**
***
****
*****
****
***
**
*
Enter number: 1
4
Divison D1-B, Question 2: Power series for ex
Consider the following series to compute ex (considering only the first n + 1 terms):
x x2 xn
ex = 1 + 1! + 2! + ... + n!
Write a program that takes as inputs a positive integer x and a positive number n, and computes the
value of ex using the above equation with n + 1 terms. You must store the resulting value in a float
data type, and print it out up to 6 decimal places.
Please note the following when you write your program:
• You cannot use any library functions to calculate any of the terms like xn , and you must do it with
repeated multiplication.
• You must reuse the value of xn−1 and (n − 1)! when calculating xn and n! respectively, so that
your code runs efficiently.
• Be mindful of overflow in the data types, and stick to smaller numbers when testing your program.
Enter x: 2
Enter n: 10
e power x = 7.388995
Enter x: 4
Enter n: 8
e power x = 53.431747
Enter x: 5
Enter n: 12
e power x = 148.113556
5
Divison D2-A, Question 1: Printing ABBA
Write a program that takes as input a positive integer N, and prints out a pattern as shown below. The
pattern has N rows. The row k has k letters, which are either ‘A’ or ‘B’, separated by spaces. The odd
rows (first, third, ..) have an alternating sequence of the letters A B A B ...., starting with A. The even
rows (second, fourth, ...) have an alternating sequence of letters B A B A ..., starting with B.
A sample run of the program is shown below.
Enter N: 4
A
B A
A B A
B A B A
Enter N: 5
A
B A
A B A
B A B A
A B A B A
6
Divison D2-A, Question 2: Sum of squares of digits
Write a program that takes as input two positive numbers, start and end. For each number between start
and end (both start and end included), the program prints the sum of the squares of the digits of that
number. There is one line of output for each number between start and end in this manner.
A sample run of the program is shown below.
7
Divison D2-B, Question 1: Printing star and dash pattern
Write a program that takes as input a positive integer N, and prints a pattern as shown below. The pattern
has N rows. The first row has one star (*) symbol in the middle, the second row has 3 star symbols in the
middle and so on. The star symbols in the middle are surrounded on either side by dash (-) symbols, so
that the entire pattern appears in a rectangular shape. The last row has only stars and no dashes. There
are no spaces between the symbols in a row.
A sample run of the program is shown below.
Enter N: 4
---*---
--***--
-*****-
*******
Enter N: 5
----*----
---***---
--*****--
-*******-
*********
Enter N: 1
8
Divison D2-B, Question 2: Finding primes
Write a program that takes as input two positive numbers, start and end. The program prints all prime
numbers that exist between start and end, both start and end included. There is one line of output for
every prime found. The program does not print anything if there are no primes in this range.
A sample run of the program is shown below.
9
5/22/23, 2:07 PM https://moodle.iitb.ac.in/pluginfile.php/378116/mod_resource/content/2/labquiz1-grading-rubric.txt
- Please plan to spend no more than 3-4 min per question per student. You can do this during
regular weekly lab hours when there aren't any students waiting for your help.
- Download the student code from our submission server and have it ready on your laptop/desktop
for grading. Talk to instructor or course managers for access to Google drive containing the
submissions. Search for the student's code in the folder corresponding to the day of the student's
exam. DO NOT download the student code from Moodle. The Moodle submission is only for the
student's reference, and is not to be graded.
- [DEMO: 6 marks] For each question, first, compile the code and run the test cases given below.
It is not necessary to match the output exactly (e.g., student can change the string used to
prompt for input), but the key concepts tested in the question should be output correctly. For
example, if we ask for a pattern to be printed in the question, the output of the pattern should
match exactly, but statements asking for user input or other statements can vary slightly in
format.
- If the code passes at least 2 out of the 3 testcases, then go to the next step. Else, if more
than one test cases fail, no need to conduct viva or check the code.
- [VIVA: 3 marks] Next, open the code, skim through it at a high level, and ask the student to
explain the code as suggested below. We are expecting only simple 1-2 sentence answers for each
question. You can give marks to the student as long as you feel the student has understood the
program he or she has written. Please be forgiving towards students who cannot communicate well,
or have a language issue. If the student gets full marks in test cases, the student should almost
always get full marks in the viva, unless you think the student has not written the code on their
own, or the student has not followed specific instructions given in the question on how to do some
computation.
- [READABILITY: 1 mark] Finally, we will assign 1 mark for code that looks reasonably well-
indented. Please be lenient here also, and do not cut this mark unless you see some very ugly
code.
- Grade two questions in this manner for a total of 20 marks. Let the students know the marks and
upload them on Moodle right away. If the student wishes to dispute the marks given by the TA,
please check with the instructor immediately (in person in the lab, or via email).
--------------------------
Test case 1 [2 marks] Run for N = 4 and check if correct triangle is printed.
Test case 2 [2 marks] Run for N = 19 and check if correct triangle is printed.
Test case 3 [2 marks] Run for N = 1 and check if correct triangle is printed.
Viva question 1 [1 mark] Show how many loops you have used and where they are in the program.
Viva question 2 [1 mark] What are the starting and ending values of the outer loop?
Viva question 3 [1 mark] What are the starting and ending values of the inner loop?
--------------------------------------
https://moodle.iitb.ac.in/pluginfile.php/378116/mod_resource/content/2/labquiz1-grading-rubric.txt 1/5
5/22/23, 2:07 PM https://moodle.iitb.ac.in/pluginfile.php/378116/mod_resource/content/2/labquiz1-grading-rubric.txt
Viva question 1 [1 mark] How many times does the loop to input coefficients run?
Viva question 2 [1 mark] What is the processing inside the loop to compute each term of the
polynomial?
Viva question 3 [1 mark] How are you reusing value of x^{n-1} while computing x^n?
(For every i, we do not want to compute x^i by multiplying i times. Instead, we must multiply
x^{i-1} computed in previous iteration and multiply it by x and use it. TAs to read code to figure
this out. Do not give marks here if there is no such reuse.)
--------------------------
Test case 1 [2 marks] Run for N = 4 and check if correct triangle is printed.
Test case 2 [2 marks] Run for N = 9 and check if correct triangle is printed.
Test case 3 [2 marks] Run for N = 1 and check if correct triangle is printed.
Viva question 1 [1 mark] How many loops are used in the program?
Viva question 2 [1 mark] Explain the starting and ending values of the counter of one of the
loops.
Viva question 3 [1 mark] Explain the processing that happens inside the innermost loop.
---------------------
Allow for slight variation in the last few digits due to floating point calculation differences
across systems.
Enter x: 2
Enter n: 8
https://moodle.iitb.ac.in/pluginfile.php/378116/mod_resource/content/2/labquiz1-grading-rubric.txt 2/5
5/22/23, 2:07 PM https://moodle.iitb.ac.in/pluginfile.php/378116/mod_resource/content/2/labquiz1-grading-rubric.txt
e power x = 7.387302
Enter x: 6
Enter n: 10
e power x = 386.234253
Enter x: 4
Enter n: 10
e power x = 54.443104
Viva question 1 [1 mark] How many times does the loop run and what is the processing in each
iteration?
Viva question 2 [1 mark] How are you reusing value of x^{n-1} computed in the previous iteration
while computing x^n?
Viva question 3 [1 mark] How are you reusing value of (n-1)! computed in the previous iteration
while computing n!
(For every i, we do not want to compute x^i by multiplying i times. Instead, we must multiply
x^{i-1} computed in previous iteration and multiply it by x and use it. Similarly, we must reuse
previous factorial value and multiply by i to get factorial in an iteration. TAs to read code to
figure this out. Do not give marks here if there is no such reuse.)
----------------------------
Division D2-A, Question 1: Printing ABBA
Viva question 1 [1 mark] How many times does the outer loop run?
Viva question 2 [1 mark] How many times does the inner loop run?
Viva question 3 [1 mark] What is the logic to decide if we should print A or B in the inner loop?
--------------------
https://moodle.iitb.ac.in/pluginfile.php/378116/mod_resource/content/2/labquiz1-grading-rubric.txt 3/5
5/22/23, 2:07 PM https://moodle.iitb.ac.in/pluginfile.php/378116/mod_resource/content/2/labquiz1-grading-rubric.txt
Viva question 1 [1 mark] What are the various loops in the programs?
-----------
Viva question 1 [1 mark] How many times does the outer loop run?
Viva question 2 [1 mark] How many times does the inner loop run?
Viva question 3 [1 mark] What is the logic used to decide if we should print a star or a dash in
the inner loop?
------------------
Viva question 1 [1 mark] What are the various loops in the programs?
Viva question 3 [1 mark] Does your algorithm above identify 1 as a prime or not? How are you
https://moodle.iitb.ac.in/pluginfile.php/378116/mod_resource/content/2/labquiz1-grading-rubric.txt 4/5
5/22/23, 2:07 PM https://moodle.iitb.ac.in/pluginfile.php/378116/mod_resource/content/2/labquiz1-grading-rubric.txt
----------------
https://moodle.iitb.ac.in/pluginfile.php/378116/mod_resource/content/2/labquiz1-grading-rubric.txt 5/5
CS 101 Spring 2023
• A simple hello world program, so that you have the template code for the main function.
Instructions
• You have to write two programs in this lab quiz to solve the two questions given to you.
• Each question carries 10 marks, which will be assigned by the TA after a demo/viva, as per the
grading rubric provided at the end of the question paper.
• TAs will first run your code (demo) and then read the code as you explain it (viva). Marks for viva
will be given only if the demo is successful for at least one execution of the code. There will be
no partial marks for incorrect code that does not run correctly even once.
• Please run your code multiple times and test it thoroughly before you submit. Please check that
you submit the correct files. We will not permit any code changes after submission.
• The TAs will only assist you if you are facing any issues with the lab machines while writing code
or submitting it. Please do not ask the TAs for help in debugging your code.
• After writing your progams, follow the submission instructions provided. You must run the com-
mand check, followed by submit on the terminal. Please call your TA when running submit.
• After you submit, you may open Moodle, and upload your code under the labquiz2 assignment.
This code is only for your records, and we will not grade it.
1
Divison D1-A, Question 1
Write a program that counts the even numbers in an array of ten random numbers.
• Your program should declare an array of 10 integers in main and populate the array with random
numbers between 1 and 100.
• You must use the rand() function from stdlib.h for generating random numbers. You should
seed the random number generator by calling srand, to test your program with different sets of
random numbers.
• You must pass this array as an argument to the function countEven, which takes the array as an
argument and returns the count of the number of even numbers in the array. The function must use
a for or while loop to iterate over all the array elements.
• You must print the value returned from the function in main.
A sample run of the program is shown below. You may get different numbers in your execution.
Array = 51 26 73 81 2 90 42 38 65 49
Value returned by countEven = 5
2
Divison D1-A, Question 2
Write a program to play a simple card game described below.
• The program begins by initializing a deck of 52 cards. The deck has 13 types of cards (Ace, 2, 3,
4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) in each of the four suits (Spades, Hearts, Diamonds, Clubs).
• The program then shuffles the deck of cards, so that the cards can be picked in a random shuffled
order from the deck during the game. You can shuffle the cards using any technique of your choice,
but you must ensure that each of the 52 cards appears exactly once in the deck, the cards are not
in any predictable order, and the order of the cards in the deck is different for different runs.
• The game proceeds as follows. We pick cards one by one from the shuffled deck and print it to
screen. We maintain the sum of all the numbers seen on the cards so far. The number of a card
is defined as follows. For cards 2 to 10 of any suit, the number is simply the number on the card.
For the remaining cards (Ace, Jack, Queen, King), the number is assgined as follows: Ace=1,
Jack=11, Queen=12, King=13.
• After picking a card, if the sum of the numbers on all cards picked so far reaches the value of
exactly 21, then the player has won the game. If the sum crosses 21, the player has lost the game.
If the sum is less than 21, we continue playing the game by picking the next card.
• The game continues in this manner until the player wins or loses, at which point the program prints
the result and finishes.
A sample run of the game is shown below. The program ends when the sum of the numbers on the
cards reaches exactly 21 (2+1+5+7+6) and the player has won.
Two of Hearts
Ace of Spades
Five of Clubs
Seven of Spades
Six of Diamonds
Sum=21
You won!
Another sample run of the program is shown below. The program ends when the sum of numbers on
the cards crosses 21 (3+9+4+12=28) and the player has lost.
Three of Clubs
Nine of Diamonds
Four of Spades
Queen of Diamonds
Sum=28
You lost
3
Divison D1-B, Question 1
Write a program that prints the minimum and maximum values in an array of ten random numbers.
• Your program should declare an array of 10 integers in main and populate the array with random
numbers between 1 and 100 (range includes both 1 and 100).
• You must use the rand() function from stdlib.h for generating random numbers. You should
seed the random number generator by calling srand, to test your program with different sets of
random numbers.
• You must pass this array as an argument to the functions getMin and getMax, which take the
array as an argument, and return the minimum and maximum values of the array respectively. The
functions must use a for or while loop to iterate over all the array elements.
• You must print the values returned from the functions in main.
A sample run of the program is shown below. You may get different numbers in your execution.
Array = 51 26 73 81 2 90 42 38 65 49
Minimum = 2
Maximum = 90
4
Divison D1-B, Question 2
Write a program to play a simple card game described below.
• The program begins by initializing a deck of 52 cards. The deck has 13 types of cards (Ace, 2, 3,
4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) in each of the four suits (Spades, Hearts, Diamonds, Clubs).
• The program then shuffles the deck of cards, so that the cards can be picked in a random shuffled
order from the deck during the game. You can shuffle the cards using any technique of your choice,
but you must ensure that each of the 52 cards appears exactly once in the deck, the cards are not
in any predictable order, and the order of the cards in the deck is different for different runs.
• The game proceeds as follows. We pick cards one by one from the shuffled deck and print it to
screen. We keep doing this until a card of the same number as the current card was already seen
before in the previous cards. The number of a card is defined as follows. For cards 2 to 10 of any
suit, the number is simply the number on the card. For the remaining cards (Ace, Jack, Queen,
King), the number is assgined as follows: Ace=1, Jack=11, Queen=12, King=13.
• The game ends once the first pair of cards of any number is seen. Note that we are looking for a
matching pair amongst all cards picked, and not just in the cards picked consecutively.
A sample run of the game is shown below. The game stops when a pair of Aces is found.
Ace of Clubs
Two of Spades
Nine of Diamonds
Three of Hearts
Ace of Diamonds
PAIR FOUND!
Another sample run of the program is shown below. The game stops when a pair of 5s is found.
Eight of Diamonds
Seven of Hearts
Five of Diamonds
Ten of Clubs
Six of Hearts
Jack of Spades
Ace of Hearts
Five of Spades
PAIR FOUND!
5
Divison D2-A, Question 1
Write a program that computes the sum of an array of ten random numbers.
• Your program should declare an array of 10 integers in main and populate the array with random
numbers between 1 and 100.
• You must use the rand() function from stdlib.h for generating random numbers. You should
seed the random number generator by calling srand, to test your program with different sets of
random numbers.
• You must pass this array as an argument to the function getSum, which takes the array as an
argument and returns the sum of all numbers in the array. The function must use a for or while
loop to iterate over all the array elements.
• You must print the value returned from the function in main.
A sample run of the program is shown below. You may get different numbers in your execution.
Array = 51 26 73 81 2 90 42 38 65 49
Sum = 517
6
Divison D2-A, Question 2
Write a program to play a simple card game described below.
• The program begins by initializing a deck of 52 cards. The deck has 13 types of cards (Ace, 2, 3,
4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) in each of the four suits (Spades, Hearts, Diamonds, Clubs).
• The program then shuffles the deck of cards, so that the cards can be picked in a random shuffled
order from the deck during the game. You can shuffle the cards using any technique of your choice,
but you must ensure that each of the 52 cards appears exactly once in the deck, the cards are not
in any predictable order, and the order of the cards in the deck is different for different runs.
• The game proceeds as follows. We pick cards one by one from the shuffled deck and print it to
screen. We do this until we see two consecutive cards with the same number. The number of
a card is defined as follows. For cards 2 to 10 of any suit, the number is simply the number on
the card. For the remaining cards (Ace, Jack, Queen, King), the number is assgined as follows:
Ace=1, Jack=11, Queen=12, King=13.
• The game ends once the number of a card matches the number of the card before it, or we exhaust
all the cards in the deck.
Queen of Hearts
Three of Diamonds
Nine of Spades
Five of Spades
Ace of Diamonds
Four of Clubs
Ace of Clubs
King of Hearts
Ace of Hearts
Seven of Clubs
Three of Hearts
Two of Hearts
Two of Diamonds
Game DONE!
Nine of Hearts
Ace of Diamonds
Four of Clubs
Ten of Diamonds
Ten of Hearts
Game DONE!
7
Divison D2-B, Question 1
Write a program that counts the number of elements in an array of ten random numbers that are greater
than 50.
• Your program should declare an array of 10 integers in main and populate the array with random
numbers between 1 and 100.
• You must use the rand() function from stdlib.h for generating random numbers. You should
seed the random number generator by calling srand, to test your program with different sets of
random numbers.
• You must pass this array as an argument to the function countOver50, which takes the array
as an argument and returns the count of the number array elements that are greater than 50. The
function must use a for or while loop to iterate over all the array elements.
• You must print the value returned from the function in main.
A sample run of the program is shown below. You may get different numbers in your execution.
Array = 51 26 73 81 2 90 42 38 65 49
Value returned by countOver50 = 5
8
Divison D2-B, Question 2
Write a program to play a simple card game described below.
• The program begins by initializing a deck of 52 cards. The deck has 13 types of cards (Ace, 2, 3,
4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) in each of the four suits (Spades, Hearts, Diamonds, Clubs).
• The program then shuffles the deck of cards, so that the cards can be picked in a random shuffled
order from the deck during the game. You can shuffle the cards using any technique of your choice,
but you must ensure that each of the 52 cards appears exactly once in the deck, the cards are not
in any predictable order, and the order of the cards in the deck is different for different runs.
• The game proceeds as follows. Player 1 picks 5 cards from the deck and player 2 picks the next 5
cards. We then count the number of “high” cards that each player has. A high card is one of Ace,
Jack, Queen, or King. The player with the greater number of high cards is declared the winner. If
both players have the same number of high cards, the game is a tie.
• The program prints the cards of each player, the number of high cards of each player, and the
result to the screen.
5 cards of player 1
Ten of Clubs
Three of Clubs
Five of Diamonds
Ace of Hearts
Two of Diamonds
5 cards of player 2
Eight of Clubs
Queen of Spades
King of Hearts
Ten of Spades
Two of Clubs
Player1 highcard=1
Player2 highcard=2
Winner=Player2
5 cards of player 1
Ace of Clubs
Seven of Clubs
Ten of Clubs
Three of Spades
Ten of Spades
9
5 cards of player 2
Six of Hearts
Ace of Diamonds
Two of Hearts
Four of Clubs
Seven of Hearts
Player1 highcard=1
Player2 highcard=1
TIE
10
Grading Rubric for Question 1
• Run the program three times. Check that the output is correct, and that different random arrays
are generated each time. Two marks will be allocated for each successful run. [3x2=6 marks]
Note: If proper randomization is not used, and the arrays are the same in all three runs, then give
2 marks only for the first run, even if the program runs correctly all 3 times.
• Proceed to reading the code only if the program runs correctly at least once.
• Read the code to check that a function call is being made correctly. That is, you must check that
the function gets the array as argument and uses this argument for calculation, uses a loop to go
over all array elements, returns value correctly to main function, and the output is printed from
main. [2 marks]
Note: Do not award these 2 marks if any of the above aspects of the function call are not happening
correctly, even if the program generates correct output.
• Read the code to check that rand() and srand are being used correctly to generate random
numbers between 1 and 100. The function srand must be called with a seed value (like time or
something else) once at the start of the program, and rand() must be called every time you need
a random number. [2 marks]
Note: Do not award these 2 marks if randomization is not being done correctly, even if the program
generates correct output.
• Proceed to reading the code only if the program runs correctly at least once.
• Read the code to check that shuffling of cards is being done correctly. [2 marks]
Note: Do not give these 2 marks if the student cannot convince you that the shuffling algorithm is
correct, even if the program works correctly.
• Read the code to check that the rules of the game are implemented correctly. [2 marks]
Note: Do not give these 2 marks if the student cannot explain how the rules of the game are
implemented.
11
CS 101 Spring 2023
• A simple hello world program, so that you have the template code for the main function.
Instructions
• You have to write three programs in this lab quiz to solve the three questions given to you. The
questions carry 6, 6, and 8 marks respectively. The marks will be assigned by the TAs after running
your code and reading through it. Please note that your code has to compile and run correctly for
your TA to assign any marks for it. There will be no partial marks for incorrect code that does
not run correctly even once.
• Please run your code multiple times and test it thoroughly before you submit. Please check that
you submit the correct files. We will not permit any code changes after submission.
• The TAs will only assist you if you are facing any issues with the lab machines while writing code
or submitting it. Please do not ask the TAs for help in debugging your code.
• After writing your progams, follow the submission instructions provided. You must run the com-
mand check, followed by submit on the terminal. Please call your TA when running submit.
• After you submit, you may open Moodle, and upload your code under the labquiz3 assignment.
This code is only for your records, and we will not grade it.
1
Divison D1-A
1. Structured Programming. Write a program that takes a positive integer N as input, and prints
a Floyd’s triangle of N rows. A Floyd’s triangle is a right-angled number triangle with natural
numbers in sequence, with row i having i numbers in the sequence. A sample run of the program
is shown below.
Enter N: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
2. Functions. Write a program that takes a positive binary number (that is, positive integer containing
only 0s and 1s) as input, and prints its decimal equivalent. Your program must contain a function
convertBinaryToDecimal that takes the binary number as an argument and returns the
decimal equivalent. This function must be invoked from the main function, and the returned
value must be printed in main. A sample run of the program is shown below.
3. Linked Lists. Consider the following structure to store student information in a linked list in a
program.
struct student {
int rollno;
int marks;
struct student *next;
};
Write a program that contains a linked list of the above student structure. The linked list is accessed
using a pointer to the first node of the list, which is initialized to NULL as the linked list is empty
at the start of your program. Next, add the student information shown below to your linked list.
2
order). Next, ask to user to enter a roll number, search for the roll number in your linked list, and
display the marks of the student with the given number. You must display a suitable message if
the roll number is not found.
A sample run of the program is shown below.
3
Divison D1-B
1. Structured Programming. Write a program that takes a positive integer N as input, and prints a
triangle with N rows. The row i has the number i2 printed i times. A sample run of the program
is shown below.
Enter N: 5
1
4 4
9 9 9
16 16 16 16
25 25 25 25 25
2. Functions. Write a program that takes a positive integer as input and prints the number with its
digits reversed. Your program must contain a function reverseDigits that takes an integer as
an argument, and returns an integer with its digits reversed. This function must be invoked from
the main function, and the returned value must be printed in main. You must not use any math
library functions to perform your computation. A sample run of the program is shown below.
3. Linked Lists. Consider the following structure to store integers in a linked list in a program.
struct node {
int data;
struct node *next;
};
Write a program that contains a linked list of integers using the above structure. The linked list is
accessed using a pointer to the first node of the list, which is initialized to NULL as the linked list
is empty at the start of your program. Next, insert all 10 integers in the range 1–10 into your linked
list one after the other at the end of the list. Note that you must dynamically allocate memory for
each node of the linked list using malloc, and then insert the node into your linked list at the end
of the list. Next, ask the user for a number as input, delete that node from the linked list, and print
the final list. The memory of the deleted node should be freed by called the function free. If the
specified number does not exist in the list, then print a suitable message that the deletion could not
be performed, and print the list.
A sample run of the program is shown below.
4
Enter number to delete: 15
Node not found
Linked list:
1 --> 2 --> 3 --> 4 --> 5 --> 6 --> 7 --> 8 --> 9 --> 10 --> NULL
5
Divison D2-A
1. Structured Programming. Write a program that takes a positive integer N as input, and prints
an inverted right-angled triangle with N rows as shown below.
Enter N: 5
*****
****
***
**
*
2. Functions. Write a program that takes two positive numbers as input, and prints out the GCD
(Greatest Common Divisor) of the two numbers. Your program must contain a function computeGCD
that takes two numbers as arguments and returns their GCD. This function must be invoked from
the main function, and the returned value must be printed in main. You can use any algorithm to
compute the GCD. A sample run of the program is shown below.
3. Linked Lists. Consider the following structure to store information about banking transactions
(deposits or withdrawals) in a banking system software.
struct transaction {
int accountno;
int amount; //positive for deposit, negative for withdrawal
struct transaction *next;
};
Write a program that adds transactions entered by the user to a linked list using the above structure,
with new nodes always being added to the start of the list. The linked list is accessed using a pointer
to the first node of the list, which is initialized to NULL as the linked list is empty at the start of
your program. Next, the program should repeatedly ask the user to enter the account number and
transaction amount, create a transaction structure with these values, and add the new node to start
of the list. Note that you must dynamically allocate memory for each node of the linked list using
malloc, and then insert the node into your linked list at the start of the list. You must print the
list after every change. The program must end when the user enters a value of -1 for the account
number, indicating that the user has no more transactions to enter. A sample run of the program is
shown below.
6
The transaction list is (20, -222) --> (10, 111) --> NULL
Enter account number: 30
Enter amount: 333
The transaction list is (30, 333) --> (20, -222) --> (10, 120) --> NULL
Enter account number: -1
7
Divison D2-B
1. Structured Programming. Write a program that takes a positive integer N as input, and prints
a pattern that is a hollow rectangle of star symbols, with a height of N rows and width of 2N
symbols. A sample run of the program is shown below.
Enter N: 5
**********
* *
* *
* *
**********
2. Functions. Write a program that takes two positive numbers as input, and prints out the LCM
(Least Common Multiple) of the two numbers. Your program must contain a function computeLCM
that takes two numbers as arguments and returns their LCM. This function must be invoked from
the main function, and the returned value must be printed in main. You can use any method to
compute LCM. A sample run of the program is shown below.
3. Linked Lists. Consider the following structure to store information about the temperature read-
ings of a city (rounded to the nearest integer) recorded in a weather prediction software.
struct reading {
int temp;
struct reading *next;
};
Write a program that adds temperature readings entered by the user to a linked list using the above
structure, with new nodes always being added to the list in sorted order. The linked list is accessed
using a pointer to the first node of the list, which is initialized to NULL as the linked list is empty at
the start of your program. Next, the program should repeatedly ask the user to enter a temperature
reading, create a temperature reading structure with this value, and add the new node to the list in
its sorted position. Note that you must dynamically allocate memory for each node of the linked
list using malloc. You must print the list after every change. The program must end when the
user enters a value of -1 for the temperature, indicating that the user has no more readings to enter.
A sample run of the program is shown below.
Enter temperature: 34
The list is 34 --> NULL
Enter temperature: 23
The list is 23 --> 34 --> NULL
Enter temperature: 30
The list is 23 --> 30 --> 34 --> NULL
8
Enter temperature: 37
The list is 23 --> 30 --> 34 --> 37 --> NULL
Enter temperature: -1
9
Grading Rubric
TAs will download the submissions from the submission server and assign marks by running the code
with some test cases, and by reading through the code. Note that we will not award any marks when
reading the code if the program does not run correctly for at least one test case.
1. Question 1 carries 6 marks. Test the code for two different values of N (to be specified later).
The two test cases carry 2 marks each. If the code gives correct output for at least one of the two
test cases, read through the code and award 2 marks if the code is using iteration as required to
print the output.
2. Question 2 carries 6 marks. Test the code for two different input values (to be specified later).
The two test cases carry 2 marks each. If the code gives correct output for at least one of the two
test cases, read through the code and award 2 marks if the function call is made as specified in
the question. If the student is not making a function call but doing all calculation in main itself,
do not award these 2 marks.
• First, read the code to make sure that the submitted program uses a linked list to store data.
Do not grade the question if the student is using arrays or any other method to store data in
the program.
• Test the program for three different input values (to be specified later). The three test cases
carry 2 marks each.
• If the code passes at least one of the test cases above, read through the code. Award 2 marks
for correctly using malloc to allocate linked list nodes in the program.
10