Question

Language: c++ Write the templated function T kMin(const T* A, const size_t n, const size_t k)...

Language: c++

Write the templated function T kMin(const T* A, const size_t n, const size_t k) that takes as input an array A of values of type T with length n>= 0 and an integer k and returns the k-th smallest value in the array. If k >= n, throw a invalid_argument exception

Example: A = [8,6,7,5,3,0,9] (array of ints), n = 7, k = 2, expected value = 5

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include<algorithm>

using namespace std;

template <class T>

void bubble(T *arr, int n)

{

T t = 0;

for(int a=1; a<n; a++)

for(int b=n-1; b>=a; b--)

if(arr[b-1] > arr[b]) {

t = arr[b-1];

arr[b-1] = arr[b];

arr[b] = t;

}

}

template <typename T>

T kMin(T* A, const size_t n, const size_t k) {

bubble(A, n);

return A[k-1];

}

int main() {

int A[] = {8,6,7,5,3,0,9};

cout << kMin(A, 7, 2);

}

#include <iostream> #include<algorithm> using namespace std; clang version 7.0.0-3-ubuntu0.18.04.1 (tags/RELEASE_700/final ?

Add a comment
Know the answer?
Add Answer to:
Language: c++ Write the templated function T kMin(const T* A, const size_t n, const size_t k)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write the templated function T kMin(const T* A, const size_t n, const size_t k) that...

    C++ Write the templated function T kMin(const T* A, const size_t n, const size_t k) that takes as input an array A of values of type T with length n>= 0 and an integer k and returns the k-th smallest value in the array. If k >= n, throw a invalid_argument exception Example: A = [8,6,7,5,3,0,9] (array of ints), n = 7, k = 2, expected value = 5.

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • C++ Language: Do not use namespace Test Case: 3-D vectors Write a templated function, called "add 3d", that can...

    C++ Language: Do not use namespace Test Case: 3-D vectors Write a templated function, called "add 3d", that can add two const references to 3-d vectors [vector<vector-vector<T>>>]. It should return a new 3-d vector that performed element-wise addition. You cannot use indexing on this problem, instead you must use iterators to access the values of end vector. INPUT OF THE TEST CASE 1 #include <vector> 2 using std: : vector; 3 const vector<double> a{1, 4, -6}; const vector<double> b{3, 6,...

  • Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[],...

    Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[], char destination[], int n); This function copies string source to string destination. Parameter n represents the size of array destination. If the latter array is not sufficiently large to hold the whole source string then only the prefix of the string which has room in the latter array should be copied. Note that after copying, the null character should also be included to mark...

  • C++ Language Given an array length 1 or more of ints, return the difference between the...

    C++ Language Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Examples: largeDiff (410, 3, 5, 6}, 4) - 7 largeDiff ({7, 2, 10, 9}, 4) - 8 largeDiff ({2, 10, 7, 2}, 4) + 8 largeDiff ({2}, 1) = 0 code.cpp + New I Full Screen 2- int largeDiff (const int a[], int n) { 3 4 5}

  • Write out C++ code for the following: Declare a templated function that has a return type...

    Write out C++ code for the following: Declare a templated function that has a return type of T". It has two parameters: An array of T items named arr, and an integer named size. This function is assumed to work with numerical data types. Within the function, create a new T variable named sum and initialize it to 0. Use a for-loop to go from 0 (inclusive) to size (exclusive), adding each element from arr to the sum. Finally, return...

  • help me solving this both please in c++ language 6 Write function max(int al Lint n)...

    help me solving this both please in c++ language 6 Write function max(int al Lint n) that receives an array of integer and its length, it returns the maximum number in that array. Test your function in main. 7) Write functin reverse (char x[ .int n) that reieves an array of characters and reverse the order of its elements. Test this function in main.

  • Please use python 3 programming language Write a function that gets a string representing a file...

    Please use python 3 programming language Write a function that gets a string representing a file name and a list. The function writes the content of the list to the file. Each item in the list is written on one line. Name the function WriteList. If all goes well the function returns true, otherwise it returns false. Write another function, RandomRange that takes an integer then it returns a list of length n, where n is an integer passed as...

  • Question: In C++ Write a correct and complete C++ program th... Bookmark In C++ Write a...

    Question: In C++ Write a correct and complete C++ program th... Bookmark In C++ Write a correct and complete C++ program that inputs 20 integer values from the user and performs three calculations. In the main function, input the values from the user. As part of your answer, write a sub-function, named calcAvg (), which takes two arguments and returns the average of all the integers. Also write another sub-function, named reverseArray (), which takes two arguments and reverses the...

  • 20. [5 points] Rewrite the following ML. function using patterns fun factn. ifn-0 then 1 else n fact (n-1) 21. [S points) Using map function, write an ML function int2real of type int list real l...

    20. [5 points] Rewrite the following ML. function using patterns fun factn. ifn-0 then 1 else n fact (n-1) 21. [S points) Using map function, write an ML function int2real of type int list real list that takes a list of integers and returns the same numbers converted to real. For example, if the input is [1,2,3], the output should be like [1.0,2.0,3.0 22. [5 points] Using foldr function, write a function duplist of type·a list 'a list that takes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT