NPTEL » Introduction to programming in C
Jan 2024 NPTEL course
Scroll Down for latest Assignments
Week 1: Assignment 1 - Question 1
Due on 2024-02-08, 23:59 IST
Problem Statement
You have a certain number of 100 rupee notes, 10 rupee notes and 1 rupee notes with you.
You have a certain number of 100 rupee notes, 10 rupee notes and 1 rupee notes with you.
There is an item you want to buy whose price is given to you.
Write a program to find if the item is affordable, that is the price of the item is less than or equal to the current money you have.
Input
Four non negative integers.
The first input is an integer representing the number of 100 rupee notes.
The second input is an integer representing the number of 10 rupee notes.
The third input is an integer representing the number of 1 rupee notes.
The fourth input is an integer representing the price of the item.
Output
You have to output 1 if the item is affordable.
You have to output 0 if the item is not affordable.
Sample Inputs and Outputs
Sample input 1
--------------------
----------------------
1
Explanation
----------------
The total amount of money is 900 + 100 + 1 = 1001, which is equal to the price of the item.
So, the item is affordable.
Sample input 2
--------------------
----------------------
0
Explanation
----------------
The total amount of money is 900 + 100 + 0 = 1000, which is less than the price of the item.
So, the item is not affordable.
Sample Inputs and Outputs
Sample input 1
--------------------
9 10 1 1001Sample Output 1
----------------------
1
Explanation
----------------
The total amount of money is 900 + 100 + 1 = 1001, which is equal to the price of the item.
So, the item is affordable.
Sample input 2
--------------------
9 10 0 1001Sample Output 2
----------------------
0
Explanation
----------------
The total amount of money is 900 + 100 + 0 = 1000, which is less than the price of the item.
So, the item is not affordable.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 9 10 1 1001 | 1 | 1 | Passed |
Test Case 2 | 9 10 0 1001 | 0 | 0 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Test Case 4 | Passed |
Test Case 5 | Passed |
Week 1: Assignment 1 - Question 2
Due on 2024-02-08, 23:59 IST
Problem Statement
Given three distinct integers a b and c, write a C program to find the second largest number among them.
Given three distinct integers a b and c, write a C program to find the second largest number among them.
Input
Three distinct integers a b c.
The first input is the integer a.
The second input is the integer b.
The third input is is the integer c.
Output
The second largest among a, b and c
Sample Inputs and Outputs
Sample input 1
--------------------
9 10 1Sample Output 1
----------------------
9
Explanation
----------------
1 < 9 < 10
Sample input 2
--------------------
-4 3 9Sample Output 2
----------------------
3
Explanation
----------------
-4 < 3 < 9
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 9 10 1 | 9 | 9 | Passed |
Test Case 2 | -1 3 9 | 3 | 3 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Test Case 4 | Passed |
Test Case 5 | Passed |
Week 1: Assignment 1 - Question 3
Due on 2024-02-08, 23:59 IST
Problem Statement
Given the coefficients of a pair of linear equations,
Find the solutions to and
Given the coefficients of a pair of linear equations,
Find the solutions to and
Input
Input consists two lines.
The first line contains coefficients of first equation, in that order
The first line contains coefficients of first equation, in that order
The second line contains coefficients of second equation, in that order
Output
The solutions to x and y.
Note : You can assume that both x and y will always be integers.
You can also assume that the solution is unique.
Sample input
--------------------
2 3 7 4 -2 6Sample Output
----------------------
2 1
Explanation
----------------
The set of equations are :
The solution is
Hint
Recall the gaussian elimination method learned in high school.
For example consider the set of equations,
To solve for x, multiply the first equation by and the second equation by , we get
Subtracting equation 1 and 2, we get and therefore .
One can similarly solve for y.
Formulate the steps involved during gaussian elimination in terms of a1 b1 c1 and a2 b2 c2 and convert this into a program.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 2 3 7
4 -2 6 | 2 1 | 2 1 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 2: Assignment 2 - Question 1
Due on 2024-02-08, 23:59 IST
Parity Checker
You are given a sequence of bits (1's and 0's).
The sequence is said to have even parity if and only if the number of 1's in the sequence if even.
Write a C program to that outputs 1 if the sequence has even parity and 0 otherwise.
A sequence of bits (0's and 1's) ending with a -1.
(Note : -1 is not a part of input. It only signifies that input has ended)
Output
1 if the number of ones in the sequence is even.
0 if the number of ones in the sequence is odd.
Sample Inputs and Outputs
Sample input 1
--------------------
----------------------
1
Explanation
----------------
The input sequence is . It has two ones. Since 2 is even, the sequence has even parity.
Sample input 2
--------------------
----------------------
0
Explanation
----------------
The input sequence is . The number of 1's is three. Since 3 is odd, the sequence does not have even parity.
You are given a sequence of bits (1's and 0's).
The sequence is said to have even parity if and only if the number of 1's in the sequence if even.
Write a C program to that outputs 1 if the sequence has even parity and 0 otherwise.
Input
A sequence of bits (0's and 1's) ending with a -1.
(Note : -1 is not a part of input. It only signifies that input has ended)
Output
1 if the number of ones in the sequence is even.
0 if the number of ones in the sequence is odd.
Sample Inputs and Outputs
Sample input 1
--------------------
0 1 1 -1Sample Output 1
----------------------
1
Explanation
----------------
The input sequence is . It has two ones. Since 2 is even, the sequence has even parity.
Sample input 2
--------------------
0 1 0 1 0 1 -1Sample Output 1
----------------------
0
Explanation
----------------
The input sequence is . The number of 1's is three. Since 3 is odd, the sequence does not have even parity.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 0 1 1 -1 | 1 | 1 | Passed |
Test Case 2 | 0 1 0 1 0 1 -1 | 0 | 0 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
There is no Question 2 provided in the assignments from the NPTEL course team.
Week 2: Assignment 2 - Question 3
Due on 2024-02-08, 23:59 IST
Count the Number of 0's Between the First and Last 1
You are given a binary sequence.
Write a C program to count the number of 0's between the first and last 1 in the sequence.
Write a C program to count the number of 0's between the first and last 1 in the sequence.
Input
A sequence of bits (0's and 1's) ending with a -1.
(Note : -1 is not a part of input. It only signifies that input has ended)
Output
The number of 0's Between the First and Last 1 in the sequence.
Note : Make no assumptions about the data in the sequence.
For instance if there is no starting and ending 1 ( say the sequence is all 0), you have to output 0.
Sample Inputs and Outputs
Sample input 1
--------------------
0 1 0 0 1 1 0 1 0 0 -1
Sample Output 1
----------------------
3
Explanation
----------------
The sequence between first and last 1 is 1 0 0 1 1 0 1 and the sequence has 3 zeroes in between.
Sample input 2
--------------------
0 0 0 1 1 1 1 1 1 0 0 -1
Sample Output 2
----------------------
0
Explanation
----------------
The sequence between first and last 1 is 1 1 1 1 1 1 and the sequence has 0 zeroes in between.
Sample input 3
--------------------
0 0 1 0 0 0 -1
Sample Output 1
----------------------
0
Explanation
----------------
The sequence has a single 1, therefore the output (by problem description) is 0.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 0 1 0 0 1 1 0 1 0 0 -1 | 3 | 3 | Passed |
Test Case 2 | 0 0 0 1 1 1 1 1 1 0 0 -1 | 0 | 0 | Passed |
Test Case 3 | 0 0 0 1 0 1 1 0 1 0 0 -1 | 2 | 2 | Passed |
Test Case 4 | 0 0 1 0 0 0 -1 | 0 | 0 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 3: Assignment 3 - Question 1
Due on 2024-02-15, 23:59 IST
Find moving average
Output
In this question, you have to output the "two moving average" of a sequence of non-negative numbers.
The two moving average is the sequence of averages of the last 2 entries.
For the first number, no average is output.
The two moving average is the sequence of averages of the last 2 entries.
For the first number, no average is output.
For example, if the sequence of numbers is
The 2-moving average is
Input
-------
The input is a sequence of non-negative numbers, terminated by a -1.
There will be at least 3 numbers in the sequence.
Note: The -1 is not part of the sequence. It is just to indicate that the input has ended.
There will be at least 3 numbers in the sequence.
Note: The -1 is not part of the sequence. It is just to indicate that the input has ended.
Output
----------
You have to output the moving average of the sequence. The output should be printed correct to one digit after the decimal.
Hint : Use the format specifier "%.1f" inside printf.
Sample Input 1
---------------------
1 3 2 4 -1
Sample Output 1
-------------------------
2.0 2.5 3.0
Explanation
-------------------------
(1+3)/2 = 2
(3+2)/2 = 2.5
(2+4)/2 = 3
Explanation
-------------------------
(1+3)/2 = 2
(3+2)/2 = 2.5
(2+4)/2 = 3
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 1 3 2 4 -1 | 2.0 2.5 3.0 | 2.0 2.5 3.0 | Passed |
Test Case 2 | 1 3 2 4 -1 | 2.0 2.5 3.0 | 2.0 2.5 3.0 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 3: Assignment 3 - Question 2
Due on 2024-02-15, 23:59 IST
Prime Checking
Complete the function int is_prime(int n) to check if a positive number n is prime or not.
The function returns 1 if n is prime, and 0 otherwise.
The function will be used in a program (code given) that prints the prime numbers in a given sequence.
The function returns 1 if n is prime, and 0 otherwise.
The function will be used in a program (code given) that prints the prime numbers in a given sequence.
Input
-------
The first line of input is a positive integer N.
The next line contains N positive integers for
Output
---------
The elements in the input list which are primes, in the original order.
The next line contains N positive integers for
Output
---------
The elements in the input list which are primes, in the original order.
Sample input
------------------
12
2 3 4 5 6 7 8 9 10 11 12 13
Sample output
--------------------
2 3 5 7 11 13
Note: Ignore the "Passed after ignoring Presentation Error" comment.
------------------
12
2 3 4 5 6 7 8 9 10 11 12 13
Sample output
--------------------
2 3 5 7 11 13
Note: Ignore the "Passed after ignoring Presentation Error" comment.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 12
2 3 4 5 6 7 8 9 10 11 12 13 | 2 3 5 7 11 13 | 2 3 5 7 11 13 | Passed after ignoring Presentation Error |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 3: Assignment 3 - Question 3
Due on 2024-02-15, 23:59 IST
Find the kth Odd integer in sequence
Input
--------
Write a C function to find the kth occurrence of an odd integer in a sequence of non-
negative integers.
Input
--------
You are given the input in two lines:
- The first line contains a positive integer k.
- In the second line, you will be given a sequence of numbers terminated with a -1.
You have to find the kth occurrence of an odd integer in the sequence.
Note: The -1 is not part of the sequence.
Output
----------
----------
If there are k odd numbers in the sequence, then output the kth occurrence of an odd number in the sequence, if present. If there are less than k odd numbers in the sequence, output -1.
Sample Input
------------------
------------------
2
1 4 8 3 6 5 2 3 4 1 -1
Sample Output
--------------------
--------------------
3
Explanation
-------------------
The odd integers in the sequence in order are :
1 3 5 3 1
The second odd integer in the list is therefore 3.
Explanation
-------------------
The odd integers in the sequence in order are :
1 3 5 3 1
The second odd integer in the list is therefore 3.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 2
1 4 8 3 6 5 2 3 4 1 -1 | 3 | 3 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 4: Assignment 4 - Question 1
Due on 2024-02-22, 23:59 IST
Description
Given an array of positive integers, write a C Program to output the sum of the elements that are above the mean.
Given an array , the mean of the array is defined as .
Input
The first line contains the size of the array.
The second line contains the contents of the array.
Output
Output the sum of elements in the array which are greater than or equal to the mean.
Output the sum of elements in the array which are greater than or equal to the mean.
Note : The size of the array is always smaller than 20.
Sample input 1--------------------
5 1 2 3 4 5
Sample Output 1
----------------------
12
Explanation
----------------
The mean of the array is (1+2+3+4+5)/5 = 3.
The elements 3,4,5 are greater than or equal to 3, and 3+4+5 = 12.
Sample input 2
--------------------
8 1 8 2 1 6 5 9 4
Sample Output 2
----------------------
28
Explanation
----------------
The mean is 4.5 and the numbers greater than or equal to this value in the array are 8,6,5,9
8+6+5+9 = 28.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 5
1 2 3 4 5 | 12 | 12 | Passed |
Test Case 2 | 8
1 8 2 1 6 5 9 4 | 28 | 28 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 4: Assignment 4 - Question 2
Due on 2024-02-22, 23:59 IST
Description
Given two arrays of positive integers, write a C Program to output the smallest number in the first array that is also present in the second one.
Given two arrays of positive integers, write a C Program to output the smallest number in the first array that is also present in the second one.
Input
The first line contains the size of the first array.
The second line contains the contents of first array.
The third line contains the size of the second array.
The fourth line contains the contents of second array.
Output
Output the smallest number occurring in the first array that also occurs in the second.
In case there is no such number, output -1.
Output the smallest number occurring in the first array that also occurs in the second.
In case there is no such number, output -1.
Note : The sizes of the arrays are smaller than 20.
--------------------
3 2 4 3 4 1 3 6 4
Sample Output 1
----------------------
3
Explanation
----------------
The numbers in the first array that also appear in the second array are 3 , 4 .
The smallest among them is 3.
Sample input 2
--------------------
4 1 8 2 1 4 6 5 9 3
Sample Output 2
----------------------
-1
Explanation
----------------
No numbers from the first array appear in the second array.
The output is -1.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 3
2 4 3
4
1 3 6 4 | 3 | 3 | Passed |
Test Case 2 | 4
1 8 2 1
4
6 5 9 3 | -1 | -1 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Test Case 4 | Passed |
Week 4: Assignment 4 - Question 3
Due on 2024-02-22, 23:59 IST
Anagram Checking
Given two strings(character arrays), write a C Program to check if one of them is an anagram of the other.
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.
Examples are LISTEN and SILENT , KNEE and KEEN.
Given two strings(character arrays), write a C Program to check if one of them is an anagram of the other.
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once.
Examples are LISTEN and SILENT , KNEE and KEEN.
Input
The first line contains the size of the character array.
The second line contains the contents of first array.
The third line contains the size of the second array.
The first line contains the size of the character array.
The second line contains the contents of first array.
The third line contains the size of the second array.
Note: The maximum size of the character array can be assumed to be 20.
The contents of both arrays are only upper case alphabets.
Output
1 If the contents of the arrays are anagrams
0 If the contents of the arrays are not anagrams
1 If the contents of the arrays are anagrams
0 If the contents of the arrays are not anagrams
Note : The sizes of the both the arrays can be assumed to be the same.
--------------------
6 SILENT LISTEN
Sample Output 1
----------------------
1
Explanation
----------------
SILENT and LISTEN are anagrams.
Sample input 2
--------------------
4 FARE SAFE
----------------------
0
Explanation
----------------
FARE and SAFE are not anagrams.
FARE has an 'R' and does not contain 'S' from SAFE.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 6
LISTEN
SILENT | 1 | 1 | Passed |
Test Case 2 | 4
FARE
SAFE | 0 | 0 | Passed |
Test Case 3 | 4
KEEP
KELP | 0 | 0 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 5: Assignment 5 - Question 1
Due on 2024-02-29, 23:59 IST
Convergence depth of Collatz function
The Collatz function is defined for a positive integer n as follows.
It is conjectured that no matter which positive integer n you start from, this sequence eventually will have 1 in it. It has been verified to hold for numbers up to 5 × 260 [Wikipedia: Collatz Conjecture].
The Collatz function is defined for a positive integer n as follows.
We consider the repeated application of the Collatz function starting with a given integer n, as
follows:
It is conjectured that no matter which positive integer n you start from, this sequence eventually will have 1 in it. It has been verified to hold for numbers up to 5 × 260 [Wikipedia: Collatz Conjecture].
e.g. If n=7, the sequence is
- f(7) = 22
- f(f(7)) = f(22) = 11
- f(f(f(7))) = f(11) = 34
- f(34) = 17
- f(17) = 52
- f(52) = 26
- f(26) = 13
- f(13) = 40
- f(40) = 20
- f(20) = 10
- f(10) = 5
- f(5) = 16
- f(16) = 8
- f(8) = 4
- f(4) = 2
- f(2) = 1
Thus if you start from n=7, you need to apply f 16 times in order to first get 1.
In this question, you will be given a positive number <= 32,000. You have to output how many
times f has to be applied repeatedly in order to first reach 1.
Sample Input
------------------
7
Sample Output
--------------------
16
Sample Input
------------------
7
Sample Output
--------------------
16
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 7 | 16 | 16 | Passed |
Test Case 2 | 13 | 9 | 9 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 5: Assignment 5 - Question 2
Due on 2024-02-29, 23:59 IST
BlockSum of an Array
Given an integer array having size n which is power of 2,
Write a recursive code to find the BlockSum of the array M.
The following is the recursive definition of BlockSum:
If size of M is 2, say , where a and b are integers, then .
Otherwise (when n > 2), partition M into two subarrays of equal size:
Given an integer array having size n which is power of 2,
Write a recursive code to find the BlockSum of the array M.
The following is the recursive definition of BlockSum:
If size of M is 2, say , where a and b are integers, then .
Otherwise (when n > 2), partition M into two subarrays of equal size:
The BlockSum of M is defined recursively as :
Here A and B are arrays of Size n/2 each.
A is the first n/2 elements of M ( in the same order) and B is the last n/2 elements of M ( in the same order).
Note : You can assume that size of input array is a power of 2, and the size is less than 1024.
Input
The first line contains the array size n
The next n lines contains the elements of the array M.
Output
Sample Inputs and Outputs
Sample Input 1
--------------------
2 3 2
Sample Output 1
----------------------
1
Sample Input 2
--------------------
Sample Output 2
----------------------
--------------------
2 7 1
Sample Output 2
----------------------
6
Sample Input 3
--------------------
Sample Output 3
----------------------
--------------------
4 7 1 3 2
Sample Output 3
----------------------
5
Explanation
--------------------
BlockSum( [ 3, 2 ] ) = 3 - 2 = 1
BlockSum( [ 7, 1 ] ) = 7 - 1 = 6
BlockSum( [ 7 , 1 , 3, 2 ] ) = BlockSum( [ 7, 1 ] ) - BlockSum( [ 3, 2 ] ) = 6 - 1 = 5
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 2
3 2 | 1 | 1 | Passed |
Test Case 2 | 4
7 1 3 2 | 5 | 5 | Passed |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Test Case 4 | Passed |
Week 6: Assignment 6 - Question 1
Due on 2024-03-07, 23:59 IST
â„“-window smoothing.
Given an integer Matrix A and an positive number , write a C program to print the window smoothing of A.
To get the -window smoothing of A , we replace with the sum of the values of the imaginary submatrix S of A with centre at
Given an integer Matrix A and an positive number , write a C program to print the window smoothing of A.
To get the -window smoothing of A , we replace with the sum of the values of the imaginary submatrix S of A with centre at
More precisely, the smoothed matrix
where .
where .
Input
The first line contains the dimension of the matrix n. Assume n < 100.
The second line contains the smoothing parameter .
The next n lines contains the contents of the matrix A, each row per line.
Output
The smoothed matrix of A
Note: Ignore the Passed after ignoring Presentation Error comment.
Example
Input
Output
Explanation
A[0][0] = 1 + 2 + 4 + 5 = 12
A[1][1] = 1 + 2 +3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 4
1
1 2 3 4
4 5 6 7
7 8 9 1
1 2 3 4 | 12 21 27 20 \n
27 45 45 30 \n
27 45 45 30 \n
18 30 27 17 | 12 21 27 20 \n
27 45 45 30 \n
27 45 45 30 \n
18 30 27 17 \n
| Passed after ignoring Presentation Error |
Test Case 2 | 6
1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1 | 4 6 6 6 6 4 \n
6 9 9 9 9 6 \n
6 9 9 9 9 6 \n
6 9 9 9 9 6 \n
6 9 9 9 9 6 \n
4 6 6 6 6 4 | 4 6 6 6 6 4 \n
6 9 9 9 9 6 \n
6 9 9 9 9 6 \n
6 9 9 9 9 6 \n
6 9 9 9 9 6 \n
4 6 6 6 6 4 \n
| Passed after ignoring Presentation Error |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 6: Assignment 6 - Question 2
Due on 2024-03-07, 23:59 IST
Simple Path Finding
Given an binary Matrix A , where each entry is 0 or 1.
A has a unique path of 1's from A[0][0] to A[n-1][n-1].
The path always goes Right (R) or Down (D).
Write a C Program.to print the directions of this path.
Given an binary Matrix A , where each entry is 0 or 1.
A has a unique path of 1's from A[0][0] to A[n-1][n-1].
The path always goes Right (R) or Down (D).
Write a C Program.to print the directions of this path.
Note: You can assume that there is exactly one correct path.
All 1's in A are in this unique path, there are no dead ends.
Input
The first line contains the dimension of the matrix n. Assume n < 100.
The second line contains the contents of the matrix A, each row per line.
Output
The path of 1's in the Matrix.
Example
Input
4
1 1 1 0
0 0 1 1
0 0 0 1
0 0 0 1
Output
RRDRDD
Explanation
The path of 1's from A[0][0] to A[3][3] is
A[0][0] Right --> A[0][1] Right --> A[0][2] Down --> A[1][2] Right --> A[1][3] Down --> A[2][3] Down --> A[3][3].
Note: The code for reading inputs etc is given to you, complete the code of the function
void findPath(int matrix[100][100], int n, int x, int y, char* path, int pathIndex);
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 4
1 1 1 0
0 0 1 1
0 0 0 1
0 0 0 1 | RRDRDD | RRDRDD\n
| Passed after ignoring Presentation Error |
Private Test cases used for Evaluation | Status |
Test Case 1 | Passed |
Test Case 2 | Passed |
Test Case 3 | Passed |
Week 6: Assignment 6 - Question 3
Due on 2024-03-07, 23:59 IST
Recursive Path Finding
Given an binary Matrix A , where each entry is 0 or 1.
A has a unique path of 1's from A[0][0] to A[n-1][n-1].
The path can go Right (R) Left (R) Down (D) or Up (U).
Given an binary Matrix A , where each entry is 0 or 1.
A has a unique path of 1's from A[0][0] to A[n-1][n-1].
The path can go Right (R) Left (R) Down (D) or Up (U).
Write a C Program.to print the directions of this path.
Note: You can assume that there is exactly one correct path.
All 1's in A need not be in this unique path, there can be dead ends.
Input
The first line contains the dimension of the matrix n. Assume n < 100.
The second line contains the contents of the matrix A, each row per line.
Output
The path of 1's in the Matrix.
Example
Input
6
1 1 1 1 0 0
0 1 0 0 0 0
0 1 0 1 1 1
1 1 1 1 0 1
0 0 0 0 0 1
0 0 0 0 0 1
Output
RDDDRRURRDDD
Explanation
The correct path of 1's from A[0][0] to A[5][5] is
A[0][0] Right --> A[0][1] Down --> A[1][1] Down --> A[2][1] Down --> A[3][1] Right --> A[3][2] Right --> A[3][3] Up -->
A[2][3] Right --> A[2][4] Right --> A[2][5] Down --> A[3][5] Down --> A[4][5] Down --> A[5][5].
1 1 1 1 0 0
0 1 0 0 0 0
0 1 0 1 1 1
1 1 1 1 0 1
0 0 0 0 0 1
0 0 0 0 0 1
Note: The code for reading inputs etc is given to you, complete the code of the function
void findPath(int matrix[100][100], int n, int x, int y, char* path, int pathIndex);
Hint
Try all the paths LRDU one by one recursively [except the opposite of last direction taken].
If any of the recursive calls succeed, the function succeeds, return '1' immediately.
If all of the recursive calls fail, the function fails, return 0.
void findPath(int matrix[100][100], int n, int x, int y, char* path, int pathIndex);
Hint
Try all the paths LRDU one by one recursively [except the opposite of last direction taken].
If any of the recursive calls succeed, the function succeeds, return '1' immediately.
If all of the recursive calls fail, the function fails, return 0.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 6
1 1 1 1 0 0
0 1 0 0 0 0
0 1 0 1 1 1
1 1 1 1 0 1
0 0 0 0 0 1
0 0 0 0 0 1 | RDDDRRURRDDD | RDDDRRURRDDD | Passed |
Test Case 2 | 4
1 1 1 0
0 0 1 1
0 0 0 1
0 0 0 1 | RRDRDD | RRDRDD | Passed |
*********
Week 7: Assignment 7 - Question 1
Due on 2024-03-14, 23:59 IST
CGPA calculation
You are the new tech administrator of a college. The college offers four courses listed below.
Course name Course code Credits
Science 1001 10
Maths 1002 5
Literature 1003 5
Philosophy 1004 1
Each student has to take exactly three courses and she gets a grade from (0 - 10)
on the course, depending on her performance.
The CGPA of a student is calculated as .
That is for each course the student took, you multiply the grade obtained with the credits of the course.
Sum this value over each course, and divide it by the total credits of all courses the student took.
The previous administrator wrote an incomplete C program for a CGPA calculator, using structures.
Complete the C code for the program, by writing the code for the function
float calculate_gpa(student s);
You are the new tech administrator of a college. The college offers four courses listed below.
Course name Course code Credits
Science 1001 10
Maths 1002 5
Literature 1003 5
Philosophy 1004 1
Each student has to take exactly three courses and she gets a grade from (0 - 10)
on the course, depending on her performance.
The CGPA of a student is calculated as .
That is for each course the student took, you multiply the grade obtained with the credits of the course.
Sum this value over each course, and divide it by the total credits of all courses the student took.
The previous administrator wrote an incomplete C program for a CGPA calculator, using structures.
Complete the C code for the program, by writing the code for the function
float calculate_gpa(student s);
This function takes as an input the struct student variable of a student s, which contains all the information
about the courses the student s took and her grades in the courses.
You have to return the total cgpa of the student as the output.
Input
The first line contains the number of students n.
The next n lines contains the information on the students in the following order:
Name CourseCode1 Marks1 CourseCode2 Marks2 CourseCode3 Marks3
Output
The names and CGPAs (to a single decimal point) of students, line by line in the following format:
Name CGPA
Note: Ignore the Passed after ignoring Presentation Error comment.
Note: Ignore the Passed after ignoring Presentation Error comment.
Example
Input1
1
Akhil 1001 9 1002 10 1003 8
Output1
Akhil 9.0
Explanation
Akhil has scored
9/10 in 1001 (10 Credits)
10/10 in 1002 (5 Credits)
8/10 in 1003 (5 Credits)
So his total cgpa is .
Input2
4
Akash 1001 10 1002 5 1003 8
Akshat 1004 5 1001 10 1002 10
Amey 1002 0 1004 5 1001 10
Anuj 1002 5 1004 10 1003 0
Output2
Akash 8.2
Akshat 9.7
Amey 6.6
Anuj 3.2
Explanation
Akash has total cgpa , which is 8.2 after rounding off to one decimal place.
Similarly, the rest follows.
Copy code button is not functional yet. Please select text and copy code.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 1
Akhil 1001 9 1002 10 1003 8 | Akhil 9.0 | Akhil 9.0\n
| Passed after ignoring Presentation Error |
Test Case 2 | 4
Akash 1001 10 1002 5 1003 8
Akshat 1004 5 1001 10 1002 10
Amey 1002 0 1004 5 1001 10
Anuj 1002 5 1004 10 1003 0 | Akash 8.2\n
Akshat 9.7\n
Amey 6.6\n
Anuj 3.2 | Akash 8.2\n
Akshat 9.7\n
Amey 6.6\n
Anuj 3.2\n
| Passed after ignoring Presentation Error |
Week 7: Assignment 7 - Question 2
Copy code button is not functional yet. Please select text and copy code.
Week 8: Assignment 8 - Question 1
Due on 2024-03-21, 23:59 IST
Deletion from Doubly linked list
Input Format
You will be given a positive number N in the first line.
Output format
The output will be the list after deleting M.
Note : Only delete the first occurrence of M in list. If M is not present in the list, no change should happen to the list. Maintain the remaining elements in the original order.
Note : You do not have to add any additional code to print the list - once you code the removeData() function, the code
In this question, you have to write code to remove the first node in a doubly linked list containing a specified number. The code to create the linked list is already given.
The main() function calls a function removeData(head,tail,n) to remove the first node containing data n from the linked list.
The main() function calls a function removeData(head,tail,n) to remove the first node containing data n from the linked list.
Complete the code by writing the missing function. You can also write additional functions which you may need.
Input Format
You will be given a positive number N in the first line.
This will be followed by a line containing N integers.
This will be followed by an integer M.
Output format
The output will be the list after deleting M.
Note : Only delete the first occurrence of M in list. If M is not present in the list, no change should happen to the list. Maintain the remaining elements in the original order.
Note : You do not have to add any additional code to print the list - once you code the removeData() function, the code
will print the contents of the remaining list.
Note: Ignore the Passed after ignoring Presentation Error comment.
Example
Sample Input1
4
1 2 3 4
3
Sample Output1
1,2,4
Explanation
3 was present in the list and was removed.
The elements other than 3 are printed.
Sample Input2
5
1 2 5 3 4
6
Sample Output2
1,2,5,3,4
Explanation
6 was not present in the list so nothing was removed.
All the elements in the list are printed.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 4
1 2 3 4
3 | 1,2,4 | 1,2,4 | Passed |
Test Case 2 | 5
1 2 5 3 4
6 | 1,2,5,3,4 | 1,2,5,3,4 | Passed |
Test Case 3 | 4
1 2 3 4
1 | 2,3,4 | 2,3,4 | Passed |
Test Case 4 | 4
1 2 3 4
4 | 1,2,3 | 1,2,3 | Passed |
Week 8: Assignment 8 - Question 2
Due on 2024-03-21, 23:59 IST
Priority Queue using Linked List
In this question, a linked list is partially implemented where each element in the linked list is a structure of the following format:
In this question, a linked list is partially implemented where each element in the linked list is a structure of the following format:
struct node{
int id;
int priority;
struct node *next;
}; struct node *next;
The field priority is a positive integer, which denotes the priority of an element inside the list.
The higher the value of integer in this field, the higher priority.
You have to complete the C code for performing the following operations in the linked list:
- Create and return a node e with given id and value val
struct node * create_node(int id, int val); - Add an node e to the beginning of the list. Return the new list.
struct node * append(struct node * list, struct node * e); - Search for a node e with id inside the list. Return a pointer to e if found, else return NULL
struct node * search(struct node * list, int id); - Change the value of an element with given id (if found), in the list to the new value val.
void change_priority(struct node * list, int id, int val) ; - Extract the element in the list with maximum priority. Return pointer to new list.
struct node* extract_max(struct node * list);
Note: You can assume that the priority of each element in the list is unique.
Note: The code for manipulating the input as well as output is given to you. You only have to write code for the incomplete functions.
Input
-------
A set of lines, each lines containing a character representing the operation and its inputs.
The operation can be one of the following:
- A <id> <val>
Add an node with id and val to the list, at the start of the list. - C <id> <val>
Change the priority field of the element with id to val.
If an element with this id is not found, do nothing.
- S <id>
If an element with the id is in the list print the id and the priority and a newline.
Else, print the id and -1 and a newline. - M
Extract the element in the list with maximum priority. Print the id of the element as "Max: id" - E
End of input, exit from the program
---------
The output of search queries and extract max operations.
Sample input
-----------------
A 1 10
A 2 20
S 2
A 3 30
S 3
M
S 3
S 3
M
S 3
C 2 30
S 2
E
Sample Output
---------------------
Sample Output
---------------------
2 20
3 30
Max: 3
3 -1
2 30
Explanation
---------------
- The list is initially empty
- Add an element 1 with value 10
list : (1,10) -> NULL - Add an element 2 with value 20
list : (2,20) -> (1,10) -> NULL - Search for element with id 2, print 2 20
- Add an element 3 with value 30
list : (3,30) - > (2,20) -> (1,10) -> NULL - Extract Max prints
Max: 3
list : (2,20) -> (1,10) -> NULL - Search for element with id 3, print
3 -1 - Change priority of 2 to 30
list : (2,30) -> (1,10) -> NULL - Search for element with id 2, print 2 30
End of input
**8**
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | A 1 10
A 2 20
S 2
A 3 30
S 3
M
S 3
C 2 30
S 2
E | 2 20\n
3 30\n
Max: 3\n
3 -1\n
2 30 | 2 20\n
3 30\n
Max: 3\n
3 -1\n
2 30\n
| Passed after ignoring Presentation Error |
Test Case 2 | A 1 10
A 2 20
A 3 30
S 3
S 4
C 2 40
M
C 1 5
M
A 4 50
S 4
A 5 40
S 5
S 1
E | 3 30\n
4 -1\n
Max: 2\n
Max: 3\n
4 50\n
5 40\n
1 5 | 3 30\n
4 -1\n
Max: 2\n
Max: 3\n
4 50\n
5 40\n
1 5\n
| Passed after ignoring Presentation Error |
No comments:
Post a Comment
Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.