Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help writing this function. I\'m not too familiar with vectors LANGUAGE:

ID: 3884930 • Letter: I

Question

I need help writing this function. I'm not too familiar with vectors

LANGUAGE: C++

//REQUIRES: v is not empty

//EFFECTS: returns a summary of the dataset as (value, frequency) pairs

// In the returned vector-of-vectors, the inner vector is a (value, frequency)

// pair. The outer vector contains many of these pairs. The pairs should be

// sorted by value.

// {

// {1, 2},

// {2, 3},

// {17, 1}

// }

// This means that the value 1 occurred twice, the value 2 occurred 3 times,

// and the value 17 occurred once

std::vector<std::vector<double> > summarize(std::vector<double> v);

We are also given a sorting function that sorts the vector from smallest to largest number.

//MODIFIES: v

//EFFECTS: sorts v

void sort(std::vector<double> &v);

Explanation / Answer

#include <iostream> // std::cout
#include <algorithm> // std::count
#include <vector> // std::vector
#include<iterator>
using namespace std;


void sort(vector<double> &v){
sort(v.begin(), v.end());   
}

vector<pair<double,double>> summarize(vector<double> v){
//declaring vector of pairs
vector< pair <double,double> > vect;
  
for (vector<double>::iterator it = v.begin() ; it != v.end(); ++it){
int mycount = std::count (v.begin(), v.end(), *it);
vect.push_back( make_pair(*it,mycount));
}
  
vect.erase(std::unique(vect.begin(), vect.end()),vect.end());

return vect;
}

int main()
{

vector<double> numbers;
numbers.push_back(40);
numbers.push_back(40);
numbers.push_back(40);
numbers.push_back(20);
numbers.push_back(30);
numbers.push_back(30);
  
sort(numbers);
  
vector< pair <double,double> > vect1 ;
  
vect1 = summarize(numbers);

// Printing the vector
cout << " Sorted Vector according to pair <value,frequency> " << " " ;
for (int i=0; i<3; i++)
{
cout << vect1[i].first << " "
<< vect1[i].second << endl;
}
  
return 0;
}