Question

Currently I'm trying to figure out why my helper function for computing the median of a vector of HW numbers is not working correctly. My helper function is suppose to work with more than just a vector. Error:

./a.out
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check
AVG: 30Aborted

Student.h:

#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <vector>
#include <string>
using namespace std;


class Student
{

 public:

  Student(string tname);

  double getHWWeight();
  void setHWWeight(double arg);

  double getmidtermExamWeight();
  void setmidtermExamWeight(double arg);

  double getfinalExamWeight();
  void setfinalExamWeight(double arg);

  double getmidterm();
  void setmidterm(double arg);

  double getfinal();
  void setfinal(double arg);

  void addHW(double arg);

  void readHW(istream &i);

  double getHWAverage();
  double getHWMedian();


 private:

  string name;
  double midterm;
  double final;
  vector<double> HW;

  static double HWWeight;
  static double midtermExamWeight;
  static double finalExamWeight;

};

#endif

Student.cpp:

#include "Student.h"
#include <algorithm>

double Student::HWWeight = 60;
double Student::midtermExamWeight = 15;
double Student::finalExamWeight = 25;


template<typename T>
T VecAverage(vector<T> arg){

  typename vector<T>::iterator it;
  T temp=0;


  for(it=arg.begin(); it < arg.end(); it++)temp+=*it;
  temp/=arg.size();
  return temp;


}

template<typename T>
T VecMedian(vector<T> arg){
  int medians = arg.size()/2;
  medians+=1;
  sort(arg.begin(),arg.end() );
  return arg.at(medians);

}

Student::Student(string tname){

  name = tname;

}

double Student::getHWWeight(){

  return HWWeight;

}

void Student::setHWWeight(double arg){

  HWWeight = arg;

}

double Student::getmidtermExamWeight(){

  return midtermExamWeight;

}

void Student::setmidtermExamWeight(double arg){

  midtermExamWeight = arg;

}

double Student::getfinalExamWeight(){

  return finalExamWeight;

}

void Student::setfinalExamWeight(double arg){

  finalExamWeight = arg;

}

double Student::getmidterm(){

  return midterm;

}

void Student::setmidterm(double arg){

  midterm = arg;

}

double Student::getfinal(){

  return final;

}

void Student::setfinal(double arg){

  final = arg;

}

void Student::addHW(double arg){

  HW.push_back(arg);

}



void Student::readHW(istream &i){

  int x;

  i >> x ;

  while(x >= 0){

    HW.push_back(x);
    i >> x;

  }

}

double Student::getHWAverage(){

  return VecAverage(HW);

}

double Student::getHWMedian(){

  return VecMedian(HW);

}

Thank you.

Was it helpful?

Solution

Consider your median function

 T VecMedian(vector<T> arg){
  int medians = arg.size()/2;
  medians+=1;
  sort(arg.begin(),arg.end() );
  return arg.at(medians);

}

If the input arg size is 2 .. you can only access arg[0] and arg[1];

your medians will be 2 and thus out of range

OTHER TIPS

template<typename T>
T VecMedian(vector<T> arg){
  int medians = arg.size()/2;
  medians+=1;
  sort(arg.begin(),arg.end() );
  return arg.at(medians);

}

. Suppose you have an empty vector. medians will be 0 + 1 and you'll try to access index 1 for an empty vector.

Add check like

if( medians < arg.size() )
//..

The same would happen for size 2, for example.

return arg.at(medians);

Most likely exception is thrown in this line of code, when medians becomes invalid index for std::vector. You can catch this exception in gdb. Load your program and set catchpoint for exceptions like this (gdb) catch throw. This will help to further debug the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top