Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6839361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:42:35+00:00 2026-05-26T23:42:35+00:00

Currently I’m trying to figure out why my helper function for computing the median

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:42:35+00:00Added an answer on May 26, 2026 at 11:42 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, working with RC0 Denali, having some issues I am attempting to review data
Currently I'm learning C++ and I've been trying to create a simple image processing
Currently working on database part of android project. The main aim of the project
Currently, db4o does not allow indexing on the contents of collections. What object databases
Currently I’m trying to get the hang of a block copy with my current
Currently I am using a pretty basic function to pre-load images: function preload(arrayOfImages) {
I need a function that will clean a strings' special characters. I do NOT
Currently, working with Denali RC0 and unable to add BI Time Intelligence to the
Currently i m working on a project where there are users with four roles
Currently my program generates random 8 character strings made from numbers. See below public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.