Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

NPTEL Introduction to programming in C Programming Assignments Jan-2024 Swayam

 

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.
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
--------------------
9 10 1 1001
Sample 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 1001
Sample 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.

Your last recorded submission was on 2024-02-04, 10:41 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
    int hundreds,tens,ones;
5
    int price;
6
    int money;
7
    
8
    scanf("%d",&hundreds);
9
    scanf("%d",&tens);
10
    scanf("%d",&ones);
11
    scanf("%d",&price);
12
    
13
    money = hundreds*100 + tens*10 + ones;
14
    
15
    if( price <= money){
16
        printf("1");
17
    }
18
    else{
19
        printf("0");
20
    }
21
    
22
    return 0;
23
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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 EvaluationStatus
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.

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 1 
Sample Output 1
----------------------

9

Explanation
----------------

1 < 9 < 10

Sample input 2
--------------------
-4 3 9
Sample Output 2
----------------------

3

Explanation
----------------

-4 < 3 < 9
Your last recorded submission was on 2024-02-04, 10:43 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
int main(){
3
  int a,b,c;
4
  scanf("%d %d %d", &a, &b, &c);
5
  if( a > b )
6
    if(a > c)
7
        if( c > b )
8
            printf("%d", c);
9
        else
10
            printf("%d", b);
11
    else 
12
        printf("%d", a);
13
  else
14
    if(b > c)
15
        if ( c > a )
16
            printf("%d", c);
17
        else
18
            printf("%d", a);
19
    else
20
        printf("%d", b);
21
  return 0;
22
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
9 10 1
9
9
Passed
Test Case 2
-1 3 9
3
3
Passed





Private Test cases used for EvaluationStatus
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,

1+1=12+2=2

Find the solutions to  and 

Input

Input consists two lines.
The first line contains coefficients of first equation,  111  in that order
The second line contains coefficients of second equation,  222 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 6
Sample Output 
----------------------

2 1 


Explanation
----------------

The set of equations are :

2+3=742=6

The solution is =2;=1.


Hint

Recall the gaussian elimination method learned in high school.

For example consider the set of equations,

2+3=742=6

To solve for x, multiply the first equation by 2=2 and the second equation by 1=3, we get 


46=14126=18

Subtracting equation 1 and 2, we get 16=32 and therefore =3216=2..

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.




Your last recorded submission was on 2024-02-04, 10:44 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
int main(){
3
  int a1,b1,c1,a2,b2,c2;
4
  scanf("%d %d %d", &a1, &b1, &c1);
5
  scanf("%d %d %d", &a2, &b2, &c2);
6
  
7
  int x = (b2*c1-c2*b1)/(b2*a1-b1*a2);
8
  int y = (a2*c1-c2*a1)/(a2*b1-a1*b2);
9
  
10
  printf("%d %d", x, y);
11
 
12
  return 0;
13
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2 3 7
4 -2 6
2 1
2 1
Passed





Private Test cases used for EvaluationStatus
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.

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 -1
Sample Output 1
----------------------

1  

Explanation

----------------
The input sequence is  011 . It has two ones. Since 2 is even, the sequence has even parity.


Sample input 2
--------------------
0 1 0 1 0 1 -1
Sample Output 1
----------------------

0  

Explanation

----------------
The input sequence is 010101 . The number of 1's is three. Since 3 is odd,  the sequence does not have even parity.
Your last recorded submission was on 2024-02-04, 10:50 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
int main()
3
{
4
  int b, count = 0;
5
  scanf("%d", &b);
6
  while ( b!= -1)
7
  {
8
    if( b == 1)
9
      count++;
10
    scanf("%d", &b);
11
  }
12
  printf("%d", count%2==0);
13
  return 0;
14
}    
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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 EvaluationStatus
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.

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

----------------------



Explanation

----------------

The sequence has a single 1, therefore the output (by problem description) is 0.
Your last recorded submission was on 2024-02-04, 10:52 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
int main()
3
{
4
  int bit, count = 0, flag = 0, sum = 0;
5
  scanf("%d", &bit);
6
  while ( bit!= -1)
7
  {
8
    if( bit == 1)
9
    {flag = 1;
10
      count+=sum;
11
      sum = 0;
12
     scanf("%d", &bit);}
13
    if( flag )
14
      if( bit == 0)
15
      sum++;
16
      scanf("%d", &bit);
17
  }
18
  printf("%d", count);
19
  return 0;
20
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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 EvaluationStatus
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

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.

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.

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
Your last recorded submission was on 2024-02-06, 15:41 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
void main()
3
{
4
int count=1;
5
float x0,x1;
6
   
7
    scanf("%f",&x0);
8
    while(x0!=-1)
9
    {
10
          scanf("%f",&x1);
11
          if(x1!=-1 && count)
12
            printf("%.1f",(x0+x1)/2);
13
          if(x1!=-1 && !count)
14
            printf(" %.1f",(x0+x1)/2);
15
      count=0;
16
      x0=x1;
17
    }
18
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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 EvaluationStatus
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.

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.

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.
Your last recorded submission was on 2024-02-06, 15:42 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
3
int is_prime(int n){
4
  int i,r;
5
  for(i=n/2;i>1;i--)
6
  {
7
    r=n%i;
8
    if(r==0)
9
        return 0;
10
  }
11
  return 1;
12
}
13
14
int main() {
15
16
  int n,num;
17
  scanf("%d", &n);
18
19
  for(int i=0;i<n;i++){
20
      scanf("%d",&num);
21
      if(is_prime(num)){
22
          printf("%d ",num);
23
      }
24
  }
25
26
  return 0;
27
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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 EvaluationStatus
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

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. 
Your last recorded submission was on 2024-02-06, 15:44 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
int find_odd(int k);
3
void main()
4
{
5
    int k;
6
    scanf("%d",&k);
7
    printf("%d",find_odd(k));
8
  
9
}
10
int find_odd(int k){
11
  int x,cnt=0, flag=1;
12
  scanf("%d",&x);
13
    while(x!=-1)
14
    {
15
          if(x%2!=0)
16
                ++cnt;
17
          if(cnt==k)
18
          { return x;
19
          }
20
      scanf("%d",&x);
21
    }
22
  if (flag)
23
    return -1;
24
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
2
1 4 8 3 6 5 2 3 4 1 -1
3
3
Passed





Private Test cases used for EvaluationStatus
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 [1,2], the mean of the array is defined as ==1.

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.
  
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.
Your last recorded submission was on 2024-02-10, 10:35 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
3
#define SIZE 25
4
int main() {
5
  int arr[SIZE];
6
  int i;
7
  int j;
8
  int sum = 0;
9
  float mean;
10
  int n;
11
  scanf("%d", &n);
12
  
13
  for (i = 0; i < n; i++) {
14
    scanf("%d", &arr[i]);
15
    sum+=arr[i];
16
  }
17
  
18
  mean = (float)sum/n;
19
  sum=0;
20
  for (i = 0; i < n; i++) 
21
      if (arr[i] >= mean)
22
          sum+=arr[i];
23
  printf("%d",sum);
24
  return 0;
25
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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 EvaluationStatus
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.

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.

Note : The sizes of the arrays are smaller than 20.


Sample input 1
--------------------
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.
Your last recorded submission was on 2024-02-10, 10:57 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
3
#define MAX 20
4
int read_array(int arr[]) {
5
  int i,n;
6
  scanf("%d",&n);
7
  for (i = 0; i < n; i++)
8
    scanf("%d", & arr[i]);
9
  return n;
10
}
11
int present(int arr[], int n, int elt) {
12
  int i;
13
  for (i = 0; i < n; i++) {
14
    if (arr[i] == elt) {
15
      return 1;
16
    }
17
  }
18
  return 0;
19
}
20
int main() {
21
  int n1,n2, flag = 1;
22
  int arr1[MAX];
23
  int arr2[MAX];
24
  
25
  n1 = read_array(arr1);
26
  n2 = read_array(arr2);
27
  
28
  int i, smallest = 36727;
29
  
30
  for (i = 0; i < n1; i++) {
31
    int val = arr1[i];
32
    if (present(arr2, n2, val)) {
33
      if (smallest > val) {
34
        smallest = val;
35
        flag = 0;
36
      }
37
    }
38
  }
39
  
40
  if (flag)
41
    printf("%d", -1);
42
  else
43
    printf("%d", smallest);
44
45
  return 0;
46
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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 EvaluationStatus
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.

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.

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

Note : The sizes of the both the arrays can be assumed to be the same.


Sample input 1
--------------------
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.
Your last recorded submission was on 2024-02-10, 12:25 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
3
#define MAX 20
4
int read_array(char arr[]) {
5
  int i,n;
6
  scanf("%d",&n);
7
  for (i = 0; i < n; i++)
8
    scanf(" %c", &arr[i]);
9
  return n;
10
}
11
12
void lexicographical_sort(char arr[], int n) {
13
  int i,j;
14
  char temp;
15
  for (i = 0; i < n-1; i++) {
16
    for (j = i+1; j < n; j++)
17
      if (arr[i] > arr[j]) {
18
        temp = arr[j];
19
        arr[j] = arr[i];
20
        arr[i] = temp;
21
      }
22
  }    
23
}
24
25
int main() {
26
  int n1,n2,i;
27
  char arr1[MAX];
28
  char arr2[MAX];
29
  
30
  n1 = read_array(arr1);
31
  n2 = read_array(arr2);
32
33
  lexicographical_sort(arr1,n1);
34
  lexicographical_sort(arr2,n2);
35
36
  for (i = 0; i < n1; i++) 
37
    if ( arr1[i] == arr2[i] )
38
      continue;
39
    else
40
    {
41
      printf("0");
42
      return 0;
43
    }
44
  printf("1");
45
  return 0;
46
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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 EvaluationStatus
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.


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
  1.     f(7) = 22
  2.     f(f(7)) = f(22) = 11
  3.     f(f(f(7))) = f(11) = 34
  4.     f(34) = 17
  5.     f(17) = 52
  6.     f(52) = 26
  7.     f(26) = 13
  8.     f(13) = 40
  9.     f(40) = 20
  10.     f(20) = 10
  11.     f(10) = 5
  12.     f(5) = 16
  13.     f(16) = 8
  14.     f(8) = 4
  15.     f(4) = 2
  16.     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
Your last recorded submission was on 2024-02-19, 17:46 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
3
int f(int n){
4
  static int count = 0;
5
  if( n == 1 )
6
    return count;
7
  else {
8
    if(n%2)
9
      f(3*n + 1);
10
    else
11
      f(n/2);
12
  count++;
13
  }
14
}
15
16
int main(){
17
  int n;
18
  scanf("%d",&n);
19
  printf("%d",f(n));
20
  return 0;
21
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
7
16
16
Passed
Test Case 2
13
9
9
Passed





Private Test cases used for EvaluationStatus
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:


                          =[]

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
--------------------
2
7 1 

Sample Output 2

----------------------

6

Sample Input 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


Your last recorded submission was on 2024-02-19, 18:17 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
3
int BlockSum(int a[], int size){
4
  if( size == 2 )
5
    return a[0]-a[1];
6
  else
7
    return BlockSum(a, size/2) - BlockSum((a+(size/2)), size/2);
8
}
9
10
int main(){
11
  int size;
12
  scanf("%d",&size);
13
  int a[size];
14
  for(int i = 0; i<size; i++)
15
    scanf("%d",&a[i]);
16
  printf("%d",BlockSum(a, size));
17
  return 0;
18
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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 EvaluationStatus
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 


More precisely, the smoothed matrix   
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

Your last recorded submission was on 2024-02-25, 12:17 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
3
int max(int a, int b){
4
    if(a>b)
5
        return a;
6
    return b;
7
}
8
9
int min(int a, int b){
10
    if(a<b)
11
        return a;
12
    return b;
13
}
14
15
int main() {
16
    int A[100][100];
17
    int B[100][100];
18
    
19
    int n,l,sum;
20
    
21
    scanf("%d",&n);
22
    scanf("%d",&l);
23
    
24
    for(int i = 0; i< n;i++){
25
        for(int j = 0; j< n;j++)
26
            scanf("%d",&A[i][j]);
27
    }
28
    
29
    for(int i = 0; i< n;i++){
30
        for(int j = 0; j< n;j++){
31
            
32
            int ih,il,jh,jl;
33
            sum =0;
34
              
35
            /*
36
               Code for calculating B[i][j]
37
           */
38
          for(il = max(i-l,0) ; il <= min(i+l,n-1) ; il++ )
39
            for(jl = max(j-l,0) ; jl <= min(j+l,n-1) ; jl++)
40
              sum += A[il][jl];
41
            B[i][j] = sum;
42
        }
43
    }
44
    
45
    for(int i = 0; i< n;i++){
46
        for(int j = 0; j< n;j++){
47
            printf("%d ",B[i][j]);
48
        }
49
        printf("\n");
50
    }
51
    return 0;
52
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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 EvaluationStatus
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.

NoteYou 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);

Your last recorded submission was on 2024-02-25, 12:34 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
3
void findPath(int matrix[100][100], int n, int x, int y, char* path, int pathIndex) {
4
    
5
    // If the destination is reached, print the path and return
6
    if (x == n - 1 && y == n - 1) {
7
        path[pathIndex] = '\0'; // Null terminate the string
8
        printf("%s\n", path);
9
        return;
10
    }
11
12
    if ( matrix[x][y + 1] == 1) {
13
       path[pathIndex] = 'R';
14
       findPath(matrix, n, x, y+1, path, pathIndex+1);   
15
    }
16
    else if ( matrix[x+ 1][y] == 1 ){
17
       path[pathIndex] = 'D';
18
       findPath(matrix, n, x+1, y, path, pathIndex+1);
19
    }
20
}
21
22
int main() {
23
    int n;
24
    scanf("%d", &n);
25
    
26
    int matrix[100][100];
27
    char path[200]; // Assuming the path will not be longer than 2n-1 steps
28
29
    // Read the matrix
30
    for (int i = 0; i < n; i++) {
31
        for (int j = 0; j < n; j++) {
32
            scanf("%d", &matrix[i][j]);
33
        }
34
    }
35
    
36
    findPath(matrix, n, 0, 0, path, 0);
37
38
    return 0;
39
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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 EvaluationStatus
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).

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
1 0 0 0 0
1 0 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.













 
 
Public Test CasesInputExpected OutputActual OutputStatus
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  
==13=13.
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.

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  910+105+8510+5+5=18020=9.0.

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 1010+55+8510+5+5=16520=8.25, 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 CasesInputExpected OutputActual OutputStatus
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

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.
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 CasesInputExpected OutputActual OutputStatus
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:

struct node{
int id;
int priority;
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:
  1. Create and return a node e with given id and value val
    struct node * create_node(int id, int val);

  2. Add an node e to the beginning of the list. Return the new list.
    struct node * append(struct node * list, struct node * e);

  3. Search for a node e with id inside the list. Return a pointer to  if found, else return NULL
    struct node * search(struct node * list, int id); 

  4. 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) ;

  5. Extract the element in the list with maximum priority. Return pointer to new list.
    struct node* extract_max(struct node * list);
After extract_max, the element having the max priority is removed from the list. Extract max also prints the id of the removed element in the following format "Max: id".

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
Output
---------
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
C 2 30
S 2
E

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 CasesInputExpected OutputActual OutputStatus
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.