Introduction to programming in C Week 2

Course Name: Introduction to programming in C

Course Link: Click Here

These are Introduction to programming in C Assignment 2 Answers


Question 1

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.
You have to output the second largest element of the sequence.
If there is no second largest element in the sequence then output 0.
Note : -1 is not a part of input. It only signifies that input has ended.

Solution:

#include <stdio.h>

int second_largest(int n, int numbers[n])
{
    int largest = numbers[0];
    for (int i = 1; i < n; i++){
        if (numbers[i] > largest){
            largest = numbers[i];
        }
    }

    int second_largest = 0;
    for (int i = 0; i < n; i++) {
        if (numbers[i] < largest && numbers[i] > second_largest) {
            second_largest = numbers[i];
        }
    }

    return second_largest;
}

int main() {
    int numbers[100];
    int n = 0;

    while (1) {
        int number;
        scanf("%d", &number);
        if (number == -1) {
            break;
        }
        numbers[n++] = number;
    }

    int result = second_largest(n, numbers);
    printf("%d", result);

    return 0;
}

These are Introduction to programming in C Assignment 2 Answers


Question 2

You are given a non decreasing sorted sequence of non negative integers, ending with -1.
That is if the sequence is a1,a2,…,an,−1 then ai≤ai+1 for all i from 1 to n-1.
You can assume that are at least two numbers before the ending -1.
You have to output the number of distinct elements in the sorted sequence.

Solution:

#include <stdio.h>

int main(){
    int num, m, n, counter=0;
    scanf("%d",&num);
    while(num!=-1){
       m=num;
       scanf("%d",&num);
       n=num;
       if((m!=n) && (num!=-1))
       counter++;
   }
   if(counter>=0)
      printf("%d",counter+1);
   else
      printf("0");
   return 0;
}

These are Introduction to programming in C Assignment 2 Answers


Question 3

In this assignment, you will be given an N×N matrix, with N>1
You have to determine whether the matrix is an upper triangular matrix.
A matrix is upper triangular if every entry below the diagonal is 0. The following is an example of an upper triangular matrix:
Note: The diagonal itself, and the entries above the diagonal can be zeroes or non-zero integers.
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 upper triangular, then print 1. Otherwise, print 0.

Solution:

#include <stdio.h>

int main(){
    int n,i,j,a,up=0,lp=0;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            scanf("%d",&a);
            if(i>j)
            {
                if(a!=0)
                up=1;
            }
            else if(i<j)
            {
                if(a!=0)
                lp=1;
            }
        }
    }
    if(up==0|lp==0)
    printf("1");
    else
    printf("0");
    return 0;
}

These are Introduction to programming in C Assignment 2 Answers

More Weeks of Introduction to programming in C: Click Here

More Nptel courses: http://13.235.254.184/nptel



These are Introduction to programming in C Assignment 2 Answers


These are Introduction to programming in C Assignment 2 Answers