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.
C Write a function that takes as input an array of int pointers, multiplies the ints...
C Write a function that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value after multiplying by -1. If...
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.
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
How to make a 2D array Tic Tac Toe game in C?
How to make a 2D array Tic Tac Toe game in C?
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT