Showing posts with label NPTEL Assignments - Programming in C. Show all posts
Showing posts with label NPTEL Assignments - Programming in C. Show all posts

Sunday, 20 March 2022

NPTEL - WEEK 5 - ASSIGNMENT 5 [ QUESTION 02 ] SOLUTION

WEEK 5 ASSIGNMENT 5 QUESTION 02 SOLUTION 

QUESTION : 

Consider a matrix M of integers. Divide M into 4 sub-matrices. These sub-matrices are called as Quadrants. Report the Quadrant number which has the smallest minimum-element. If two or more quadrants have same smallest minimum, report the smallest quadrant index.
The matrix M is divided into four quadrants by halving the rows and columns. If row/column is an odd number, divide them in such a way that the first half of the row/column should be one smaller than the second half.

The four quadrants are numbered from 1 to 4 in the structure shown below: Q1 | Q2
---+---
 Q3 | Q4
INPUT FORMAT: M is a matrix of integers. You would be given two numbers m and n specifying the number of rows and columns. This would be followed by m lines of n integers each specifying the data of the m*n matrix M.
n and m will be greater than 1 and less than 12. OUTPUT FORMAT: Print in a separate line, the quadrant number with the smallest minimum-element.

Tuesday, 15 March 2022

NPTEL - WEEK 4 - ASSIGNMENT 4 - QUESTION 02

 WEEK 4 - ASSIGNMENT 4 - QUESTION 02 SOLUTION

QUESTION : 

Write a program that replaces the occurence of a given character (say c)
in a primary string (say PS) with another string (say s).

Input:
The first line contains the primary string (PS)
The next line contains a character (c)
The next line contains a string (s)

Output:
Print the string PS with every occurence of c replaced by s.

NOTE:
- There are no whitespaces in PS or s. 
- Maximum length of PS is 100.
- Maximum length of s is 10.


SOLUTION : 

#include<stdio.h>
#include<string.h>

int main()
{
    char ps[100], s[10], c, buff;
    int i=0,k=0, j=0, occ=0, len_ps, len_s, tlen;

    scanf("%s", ps);
    scanf("%c", &buff);
    scanf("%c", &c);
    scanf("%s", s);

    len_ps = strlen(ps);
    len_s = strlen(s);
    occ = 0;
    for(i=0; i<len_ps; i++)
    {
        if(ps[i] == c)
        {
            occ++;
        }
    }
    tlen = len_ps + ((len_s * occ) - occ);
   
    j = tlen;
   
    for(i=len_ps; i>0; i--)
    {
        if(ps[i-1] == c)
        {
            for(k=len_s; k>0; k--)
            {
                ps[j-1] = s[k-1];
                j--;
            }
        }
        else
        {
            ps[j - 1] = ps[i-1];
             j--;
        }
      
    }
    ps[tlen] = '\0';
    puts(ps);
    return 0;
}

Saturday, 12 March 2022

NPTEL - WEEK 4 ASSIGNMENT 4 - QUESTION 03

 WEEK 4 - ASSIGNMENT 4 - QUESTION 03

QUESTION :

Given a threshold floating point number and an array of floating point numbers strictly between 0 and 1.
    Modify the array with the following rules: If the number is greater than threshold value, change it to 1 and if less 
    than or equal to threshold value, change to 0.
    
    Output the sum of the modified array.
    Constraint: Array contains atmost 20 elements.

    Input: First line contains the threshold value.
           Next line gives the size of an array.
           Next line provides the content of an array.
          
    Output: Sum of modified array.

Example:   Input:
            0.3
            4
            0.1 0.9 0.11 0.98
           
           Modified array: 0 1 0 1

           Output : 2


SOLUTION :

#include<stdio.h>

int main()
{
    float num[20], tshld;
    int sum=0, n;

    scanf("%f", &tshld);
    scanf("%d", &n);

    for(int i=0; i<n; i++)
    {
        scanf("%f", &num[i]);
    }

    for(int i=0; i<n; i++)
    {
        if(num[i] > tshld)
        {
            num[i] = 1.0;
        }
        else if(num[i] <= tshld)
        {
            num[i] = 0.0;
        }
    }
    for(int i=0; i<n; i++)
    {
        sum += num[i];
    }
    printf("%d", sum);
    return 0;
}


NPTEL - WEEK 4 - ASSIGNMENT 4 - QUESTION 01

WEEK 4 - ASSIGNMENT 4 - QUESTION 01

QUESTION :

Given two arrays of integers output the largest number in the
first array not present in the second one.

Input: 

The first line contains the size of the first array.
Next line give the contents of the first array.
Next line contains the size of the second array.
Next line give the contents of the second array.

Output Format:
Output must be a single number which is the largest number occurring
in the first array that does not occur in the second. In case there is
no such number, output 0.

Variable Constraints:
The sizes of the arrays are smaller than 20.
Each array entry is an integer which fits an int data type.

Example:
Input:
3
2 3 4
4
1 4 5 7

Output: 3

Input
1
1
2
1 2

Output: 0

SOLUTION :

#include<stdio.h>

int main()
{
    int arr1[10], max, count, arr2[10], n=0, m=0;

    scanf("%d", &n);
    for(int i=0; i<n; i++)
    {
        scanf("%d", &arr1[i]);
    }

    scanf("%d", &m);
    for(int j=0; j<m; j++)
    {
        scanf("%d", &arr2[j]);
    }

    while(1)
    {
        max = 0;
        count = 0;
        for(int i=0; i<n; i++)
        {
            if(max < arr1[i])
            {
                max = arr1[i];
                count = i;
            }
        }
        for(int j=0; j<m; j++)
        {
            if(max == arr2[j])
            {
               arr1[count] = 0;
              
            }
           
        }
        if(arr1[count] != 0)
        {
            break;
        }
        if(max == 0)
        {
           break;
        }
    }
   
    printf("%d", max);
    return 0;
}

« iCode Buzz »

Saturday, 5 March 2022

NPTEL - WEEK 3 ASSIGNMENT 3 QUESTION 03

 QUESTION :

Write a C program to list all the factorial numbers less than or equal
to an input number n.

A number N is called a factorial number if it is the factorial of a
positive integer. For example, the first few factorial numbers are

   1, 2, 6, 24, 120, ...

*Note* - We do not list the factorial of 0.

Input
-----
A positive integer, say n.

Output
------
All factorial numbers less than or equal to n.


SOLUTION :

#include<stdio.h>

int main()
{
    int n, i, j, fact = 1;

    scanf("%d", &n);

    for(i=1; i<n; i++)
    {
            fact = 1;
            for(j=1; j<=i; j++)
            {
                fact *= j;
            }
            if(fact <= n)
            {
               printf("%d ", fact);
            }
            else
            {
               break;
            }
    }
    return 0;
}


NPTEL - WEEK 3 ASSIGNMENT 3 QUESTION 02

 QUESTION :

In this question, you have to output the "moving average" of a
sequence of non-negative numbers. The 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

a1, a2, a3, a4, a5

then the 2-moving average is

(a1+a2)/2, (a2+a3)/2, (a3+a4)/2, (a4+a5)/2 


Input
-----

The input is a sequence of non-negative floating point numbers,
terminated by a -1. The -1 is not part of the sequence. There will be
at least 3 numbers in the sequence.

Output
--------------------------------------------------------------------------------------------
You have to output the moving average of the sequence. The output
should be printed correct to one digit after the decimal. 

Sample Input 1
-------------------------------------
1 2 3 -1

Sample Output 1
-------------------------
 1.5 2.5

SOLUTION :

#include<stdio.h>

int main()
{
    float prev = 0, curr = 0, avg = 0;
   
    scanf("%f", &prev);
   
    while(curr != -1)
    {
        scanf("%f", &curr);
        if(curr == -1)
        {
            break;
        }
        avg = (prev + curr)/2;
        printf("%.1f ", avg);
        prev = curr;
    }
    return 0;
}

NPTEL - WEEK 3 ASSIGNMENT 3 QUESTION 01

Question :

Write a C function to find the kth occurrence of an odd integer in a sequence of non-negative integers, and then call your function from main. 

Your function should be according to the following declaration:

int find_odd(int k);

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. 

You have to find the kth occurrence of n in the sequence below. 

The second line consists of a sequence of non-negative integers,
terminated with a -1.  The -1 is not part of the sequence.

Output
If there are  k odd numbers in the sequence, then output the  kth
occurrence of odd in the sequence. Otherwise, output  -1.

Solution :

#include<stdio.h>
int find_odd(int k)
{
    int seq, count=0, num;
   
        while(seq != -1)
    {
        scanf("%d", &seq);
        if(seq%2 != 0)
        {
            count++;
            num = seq;
        }
        if(count == k)
        {
            break;
        }
    }
    return num;
}

int main()
{
    int k, seq = 0,num, count=0;

    scanf("%d", &k);

    num = find_odd(k);
   
    printf("%d", num);
    return 0;
}