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;
}


Come again learn again !
« iCode Buzz »


No comments:

Post a Comment