NPTEL Programming in Modern C++ Programming Assignment Week-9 Sept-2023 Swayam

 

W9_Programming_Qs.1

Due on 2023-09-28, 23:59 IST
Consider the following program that finds the minimum element in an array. Fill in the blanks
as per the instructions given below:
   • Fill in the blank at LINE-1 with appropriate template declaration.
   • Fill in the blanks at LINE-2 and LINE-3 with appropriate conditional statement.
The program must satisfy the given test cases.
Your last recorded submission was on 2023-09-18, 17:05 IST
Select the Language for this assignment. 
#include<iostream>
 
template<class T, class Itr>    //LINE-1
void min(Itr first, Itr last, T& mv) {
    mv = *first++;
    while (first !=last) {          //LINE-2
 
        if(*first<mv)            //LINE-3
            mv = *first;
        ++first;
    }
}
int main(){
    int iArr[10];
    int n;
    std::cin >> n;
    for(int i = 0; i < n; i++)
        std::cin >> iArr[i];
        
    double minVal = 0.0;
    min(iArr, iArr + n, minVal);
    std::cout << minVal;
    return 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
5
50 27 10 70 23
10
10
Passed
Test Case 2
6
94 5 22 45 32 1
1
1
Passed



W9_Programming_Qs.2

Due on 2023-09-28, 23:59 IST
Consider the following program that takes inputs a lower limit (l) and an upper limit (u) of
a vector. If all the elements of the input vector are within the given lower and upper limits,
the program prints "all in [l, u]". Otherwise, the program prints the first element of the vector
which is not within the given limits. Fill in the blanks as per the instructions given below:
  • at LINE-1 with appropriate header to overload function operator,
  • at LINE-2 with appropriate template definition,
  • at LINE-3 with appropriate condition,
such that the program will satisfy the given test cases. 
Your last recorded submission was on 2023-09-18, 17:06 IST
Select the Language for this assignment. 
#include<iostream>
#include<vector>
 
struct Bound{
    Bound(int l, int u) : l_(l), u_(u){}
 
    bool operator()(int n){     //LINE-1
        return (n >= l_ && n <= u_);
    }
    int l_, u_;
};
 
template<class Itr, class Pred>      //LINE-2
 
Itr search(Itr first, Itr last, Pred bd) {
    while (first != last){
 
        if(!bd(*first))     //LINE-3
            return first;
        ++first;
    }     
    return first;
}
int main(){
    int iArr[] = { 70, 20, 50, 40, 90, 10, 80 }; 
    std::vector<int> iVec(iArr, iArr + sizeof(iArr) / sizeof(*iArr));
    int l, u;
    std::cin >> l >> u;
    Bound bd(l, u);
    
    std::vector<int>::iterator it = search(iVec.begin(), iVec.end(), bd);
    
    if(it == iVec.end())
        std::cout << "all in [" << l << ", " << u << "]";
    else
        std::cout << *it << " is the first element not in [" << l << ", " << u << "]";
    return 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
10 100
all in [10, 100]
all in [10, 100]
Passed
Test Case 2
10 70
90 is the first element not in [10, 70]
90 is the first element not in [10, 70]
Passed


W9_Programming_Qs.3

Due on 2023-09-28, 23:59 IST
Consider the following program, which computes the frequency of occurrence (histogram) of
each integer in a given vector. Fill in the blanks as per the instructions given below:
   • at LINE-1 with appropriate statement to iterate over the given vector v,
   • at LINE-2 with appropriate statement to iterate over the given map hi,
such that it will satisfy the given test cases.
Your last recorded submission was on 2023-09-18, 17:07 IST
Select the Language for this assignment. 
#include <iostream>
#include <map>
#include <vector>
 
std::map<int, int> histo(std::vector<int> v){
    std::map<int, int> hi; 
    for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it)   //LINE-1
        hi[*it]++;
    return hi;
}
 
void print(std::map<int, int> hi){
    for (std::map<int, int>::iterator it = hi.begin(); it != hi.end(); ++it)   //LINE-2
        std::cout << it->first << ": " << it->second << ", ";
}
int main() { 
    std::vector<int> vec;
    for(int i = 0; i < 10; i++){
        int a;
        std::cin >> a;
        vec.push_back(a);
    }
    std::map<int, int> hi = histo(vec); 
    print(hi);  
    return 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
9 4 5 3 4 6 7 4 3 5
3: 2, 4: 3, 5: 2, 6: 1, 7: 1, 9: 1,
3: 2, 4: 3, 5: 2, 6: 1, 7: 1, 9: 1, 
Passed after ignoring Presentation Error
Test Case 2
6 6 7 3 4 5 6 5 4 7
3: 1, 4: 2, 5: 2, 6: 3, 7: 2,
3: 1, 4: 2, 5: 2, 6: 3, 7: 2, 
Passed after ignoring Presentation Error




netaji gandi Thursday, September 28, 2023

NPTEL Programming in Java Jan 2024 Week 11

  Week 11 : Programming Assignment 1 Due on 2024-04-11, 23:59 IST The following code is missing some information needed to run the code. Add...