Home

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

Introduction To Programming In C | Week 2 : Assignment 2 | Answers | Jan-2022 | NPTEL | Swayam

NPTEL » Introduction To Programming In C


Please subscribe to our YouTube Channel :  Swayam Solver


Week 2: Assignment 2 - Question 1

Due on 2022-03-10, 23:59 IST

In this assignment, you will be given an NxN matrix. You have to
determine whether the matrix is a upper triangular or lower triangular matrix or both or not a triangular matrix. 

The diagonal of the matrix M of size NxN is the set of entries M(0,0),
M(1,1), M(2,2), ..., M(N,N). 

A matrix is upper triangular if every entry below the diagonal is
0. For example,  
1 1 1
0 0 1
0 0 2
is an upper triangular matrix. (The diagonal itself, and the entries
above can be zeroes or non-zero integers.) 

A matrix is lower triangular if every entry above the diagonal is
0. For example, 
2 0 0
3 1 0
4 2 2
is a lower triangular matrix  (The diagonal itself, and the entries
below can be zeroes or non-zero integers.) . 

A matrix is not a triangular matrix if it is neither a upper triangular nor a  lower
triangular.

You may not use arrays for this program.

Input
First, you will be given N, which is the size of the matrix.

Then you will be given N rows of integers, where each row consists of
N integers separated by spaces. 

Output
If the input matrix is lower triangular, then print -1.
If the input matrix is upper triangular, then print 1.
If the input matrix is both lower and upper triangular, then print 2.
If the input matrix is not a triangular matrix, then print 0.

Kindly do not use arrays in the code.
Your last recorded submission was on 2022-03-03, 22:09 IST
Select the Language for this assignment. 

Just scroll your mouse wheel over this code or swipe on this code snippet to scroll down to see the remaining code.
1
#include <stdio.h>
void main()
{
int i,j,N,v;
  int lflag=1;
  int uflag=1;
   
scanf("%d", &N);
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            scanf("%d",&v);
          if(j<i && v!=0)
                lflag=0;
          if(i<j && v!=0)
                uflag=0;    
        }
    }
  
  if(lflag && uflag)
        printf("2");
  else if(lflag)
          printf("1");
    else if(uflag)
            printf("-1");
         else
            printf("0");
  
}
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
0 0 0
1 1 0
1 2 3
-1
-1
Passed
Test Case 2
4
1 2 3 4
1 3 5 7
7 9 3 -1
0 0 0 0
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 2: Assignment 2 - Question 2

Due on 2022-03-10, 23:59 IST
You are given a sorted(either in the increasing or in the decreasing order) sequence of positive numbers, ending with a -1. You can assume that there are atleast three numbers before the ending -1. 

Note : -1 is not a part of input. It only signifies that input has ended.

Let us call the sequence x0  x1 ... xn -1.

You have to output 1 if there are atleast three distinct numbers in the sequence.
otherwise output 0

Kindly do not use arrays in the code.
Your last recorded submission was on 2022-03-03, 22:10 IST
Select the Language for this assignment. 
1
#include <stdio.h>
void main()
{
int cnt,x0,x1;
  
  cnt=0;
   
    scanf("%d",&x0);
    while(x0!=-1)
    {
            scanf("%d",&x1);
          if(x1!=x0)
                ++cnt;
      x0=x1;
    }
  if ( cnt>=3 )
    printf("1");
  else
    printf("0");
}
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 2 3 4 -1
1
1
Passed
Test Case 2
2 1 1 -1
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 2: Assignment 2 - Question 3

Due on 2022-03-10, 23:59 IST
You are given a non-negative sequence of numbers, ending with a -1. You can assume that there are at least two numbers before the ending -1. 

Note : -1 is not a part of input. It only signifies that input has ended.

Let us call the sequence x0  x1 ... xn -1.

You have to output the second largest element of the sequence. if there is no second largest element in the sequence then output 0.

Kindly do not use arrays in the code. 
Your last recorded submission was on 2022-03-03, 22:12 IST
Select the Language for this assignment. 
1
#include <stdio.h>
void main()
{
int x,max,smax=-1;
    scanf("%d",&x);
    max=x;
    while(x!=-1)
    {
            scanf("%d",&x);
          if(x>max)
          {smax=max;    
           max=x;}
      else if(x<max && x>smax)
        smax=x;
    }
  if ( smax!=-1)
    printf("%d",smax);
  else
    printf("0");
}
You may submit any number of times before the due date. The final submission will be considered for grading.


 
 

Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4 5 -1
4
4
Passed
Test Case 2
2 2 2 -1
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



Watch the video to see the program in motion.


Please subscribe to our YouTube Channel :  Swayam Solver

This will help the creator to continue making quality content...

Have a great day !



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.