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

  • Home
  • SEARCH
  • 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 869615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:20:11+00:00 2026-05-15T10:20:11+00:00

I’m using g++ on fedora linux 13. I’m just practicing some exercises from my

  • 0

I’m using g++ on fedora linux 13.
I’m just practicing some exercises from my c++ textbook
and can’t get this one program to compile. Here is the code:

double *MovieData::calcMed() {
        double medianValue;
        double *medValPtr = &medianValue;
        *medValPtr = (sortArray[numStudents-1] / 2);
        return medValPtr;
}

Here is the class declaration:

    class MovieData 
{
private:
    int *students;                  // students points to int, will be dynamically allocated an array of integers.
    int **sortArray;                // A pointer that is pointing to an array of pointers.
    double average;                 // Average movies seen by students.
    double *median;                 // Median value of movies seen by students.
    int *mode;                  // Mode value, or most frequent number of movies seen by students.
    int numStudents;                // Number of students in sample.
    int totalMovies;                // Total number of movies seen by all students in the sample.
    double calcAvg();               // Method which calculates the average number of movies seen.
    double *calcMed();              // Method that calculates the mean value of data.
    int *calcMode();                // Method that calculates the mode of the data.
    int calcTotalMovies();              // Method that calculates the total amount of movies seen.
    void selectSort();              // Sort the Data using selection sort algorithm.
public:
    MovieData(int num, int movies[]);       // constructor
    ~MovieData();                   // destructor
    double getAvg() { return average; }     // returns the average
    double *getMed() { return median; } // returns the mean
    int *getMode()  { return mode; }        // returns the mode
    int getNumStudents() { return numStudents; }    // returns the number of students in sample
};

Here is my constructor and destructor and selectSort():

MovieData::MovieData(int num, int movies[]) {
    numStudents = num;

    // Now I will allocate memory for student and sortArray:
    if(num > 0) {
        students = new int[num];
        sortArray = new int*[num];

        // The arrays will now be initialized:
        for(int index = 0;index < numStudents;index++) {
            students[index] = movies[index];
            sortArray[index] = &students[index];
        }
        selectSort();   // sort the elements of sortArray[] that point to the elements of students.
        totalMovies = calcTotalMovies();
        average = calcAvg();
        median = calcMed();
        mode = calcMode();
    }
}

// Destructor:
// Delete the memory allocated in the constructor.
MovieData::~MovieData() {
    if(numStudents > 0) {
        delete [] students;
        students = 0;
        delete [] sortArray;
        sortArray = 0;
    }
}

// selectSort() 
// performs selection sort algorithm on sortArray[],
// an array of pointers.  Sorted on the values its 
// elements point to.
void MovieData::selectSort() {
    int scan, minIndex;
    int *minElement;

    for(scan = 0;scan < (numStudents - 1);scan++) {
        minIndex = scan;
        minElement = sortArray[scan];
        for(int index = 0;index < numStudents;index++) {
            if(*(sortArray[index]) < *minElement) {
                minElement = sortArray[index];
                minIndex = index;
            }
        }
        sortArray[minIndex] = sortArray[scan];
        sortArray[scan] = minElement;
    }
}

The compiler is giving this error:

moviedata.cpp: In memberfunction
‘double * MovieData::calcMed()’:

moviedata.cpp:82: error: invalid
operands of types ‘int*’ and ‘double’
to binary ‘operator/’

I’m not sure what to make of this error, i’ve tried static casting the types with no luck, what does this error message mean?

  • 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-15T10:20:11+00:00Added an answer on May 15, 2026 at 10:20 am

    sortArray[numStudents - 1] is a pointer to int, which can’t be on the left side of a division (when you remember pointers are addresses, this makes sense). If you post more of your code, we can help you correct it.

    Perhaps you want something like:

    int *MovieData::calcMed() {
          return sortArray[(numStudents - 1) / 2];
    }
    

    This returns the middle element in your array, which should be a pointer to the middle student. I’m not clear why you’re sorting lists of pointers (not the actual values), or why you’re returning a pointer here. The return value + 1 will be a pointer to the next value in students, which is not the next greater value numerically. So you might as well return the actual student (int from students). If you do this, you can also average the two middle elements when the count is even (this rule is part of the typical median algorithm).

    Note that I changed the return type to int *, the type of sortArray’s elements. Also, your comment is incorrect. This is the median, not the mean.

    Also, your selection sort is wrong. The inner loop should start at scan + 1.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.