Question

In: Computer Science

How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...

How to write a C++ of CountingSort function using 2D vector?

CountingSort(vector > array)

Input
# of rows: 2
Input Row 1: 9 8 7 6 3 2 1 5 4

Input Row 2: 1 2 4 3 5 6 9 8 7

Output

1,2,3,4,5,6,7,8,9

1,2,3,4,5,6,7,8,9

Solutions

Expert Solution

#include <algorithm>
#include <iostream>
#include <vector>
#include <bits/stdc++.h> 
using namespace std;
vector<int> myVector;

void CountingSort(vector<int> vec,int m) 
{ //sorting 
    sort(vec.begin(), vec.end());
    auto end = vec.end();
        for (auto it = vec.begin(); it != end; ++it) {
                end = std::remove(it + 1, end, *it);
        }

        vec.erase(end, vec.end());
   //display result
   cout<<"output of input row "<<m+1<<"\n";
    cout<<"\n\nAfter sorting vector : ";
    for(auto i=vec.begin(); i<vec.end(); i++)
    {
        cout<<" "<<*i;
    }
    
}
int main()
{
    int n;
cout<<"enter number of rows:\n";
cin>>n;
//iterate number of rows times
for(int m=0;m<n;m++){
    
    cout<<"\n Enter input elements for row "<<m+1<<":";
        for (int i = 0; i < 9; i++) 
        {
                int inp;
                //prompt user to enter elements
                cin >> inp;       myVector.push_back(inp);
        
        }
        //function call 
        CountingSort(myVector,m);

}

    

    return 0;
}


Related Solutions

Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and...
Write a function called ReturnOddEntries.m that accepts as input a column or row array (vector) and returns only the odd index entries. Do this by first setting the even entries to 0, and then removing the 0 entries by using a logical array. The first line of your code should read function p = ReturnOddEntries(p) For example, if you run in the command window p = ReturnOddEntries([1.2 7.1 8.4 -42 100.1 7 -2 4 6]), then you should get p...
C++ How do you make a 2d array with a user input. For example, the row...
C++ How do you make a 2d array with a user input. For example, the row and columns of the 2d array will be the same so the program can create a square matrix.
Write a function declaration for a function that sums each row of a 2D array, where...
Write a function declaration for a function that sums each row of a 2D array, where each row size is 10. The function does not return a result. IN C Program.
Write a function declaration for a function that sums each row of a 2D array, where...
Write a function declaration for a function that sums each row of a 2D array, where each row size is 10. The function does not return a result. Need it in 10 minutes, please.
write a function declaration for a 2d array where each row size is 8 and the...
write a function declaration for a 2d array where each row size is 8 and the function does not return anything.
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should...
Write a function called HW5_P1 that accepts 1 input argument: an array, a. The function should output an array, b, that is computed as: b=3a+5. Write a MATLAB function called “fit_line” that accepts 2 input arguments: a column vector of x data and a column vector of y data. The nth element in the input arguments should correspond to the nth Cartesian data point i.e. (xn,yn). The function should compute and return 2 outputs: the slope, m, and the y...
Write a function script DirCos.m that takes a vector (any row or column array) as the...
Write a function script DirCos.m that takes a vector (any row or column array) as the argument and returns the direction cosines for that vector. This is for a MatLab script
Challenge 3 – Make 2D Array Write a function that takes 3 parameters and makes use...
Challenge 3 – Make 2D Array Write a function that takes 3 parameters and makes use of your prior two functions to create a 2D array filled with a default parameter. var twoD = Init2D(<width>, <height>, <fill val>); Challenge 4 – Random Integer in Range Write a function to return a random integer between a minimum value and maximum value. var ival = IntRandomRange(<min>, <max>); Challenge 5 – Random Int 2D Array Use your prior functions to provide a function...
Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function...
Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function that, given an arbitrary 2-d array as input, returns its perimeter sum, which is defined as the sum of all the elements along its perimeter. You must name your function perimeter_sum
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT