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.
Send an element from 2d array to a function using pointer to pointer. c++ I am...
Send an element from 2d array to a function using pointer to pointer. c++ I am having an issue with being able to send specific elements to the swap function. #include <iostream> using namespace std; void swap(int **a, int **b){    int temp = **a;    **a=**b;    **b=temp; } void selSort(int **arr, int d){    for(int i =0; i<d-1; i++){        int min = *(*arr+i);        int minin = i;        for(int j =i+1; j<d; j++){...
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.
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...
Using C++: Write a function that recursively calculates the sum of an array. The function should...
Using C++: Write a function that recursively calculates the sum of an array. The function should have 2 parameters, the array and an integer showing the number of elements (assume they are all integers) in the array. The function will use a recursive call to sum the value of all elements. You must prompt for a series of integers to be entered in the array. As your program reads the numbers in, increment a count so it knows how many...
Write a method that will accept a 2D character array. The 2D array represents a grid...
Write a method that will accept a 2D character array. The 2D array represents a grid (table) of characters in which a triple may occur. A triple is 3 matching characters. This method will search through the array to determine whether or not it contains a set of 3 matching characters. Specifically, it will check to see if the 3 matching characters appear somewhere in the array as three adjacent characters either horizontally (left to right) or vertically (top to...
"Write a function named "firstLast2" that takes as input a vector of integers. The function should...
"Write a function named "firstLast2" that takes as input a vector of integers. The function should return true if the vector starts and ends with the digit 2. Otherwise, it should return false. Test your function with vectors of different length and with the digit 2 at the beginning of the vector, end of the vector, middle of the vector, and missing from the vector." Additional Requirements: >Must use a loop allowing the user to continue until he/she quits. >Read...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT