0

So im still pretty new to C++ and have been doing a program for a while now. I think I am slowly getting it but keep getting an error "Intellisense: operand of '*' must be a pointer." on line 36 column 10. What do I need to do to fix this error? Im going to get to the other functions as i finish each one so sorry for the extra function declaration

// This program will take input from the user and calculate the 
// average, median, and mode of the number of movies students see in a month.

#include <iostream>
using namespace std;

// Function prototypes
double median(int *, int);
int mode(int *, int);
int *makeArray(int);
void getMovieData(int *, int);
void selectionSort(int[], int);
double average(int *, int);

// variables
int surveyed;



int main()
{
    cout << "This program will give the average, median, and mode of the number of movies students see in a month" << endl;
    cout << "How many students were surveyed?" << endl;
    cin >> surveyed;

    int *array = new int[surveyed];

    for (int i = 0; i < surveyed; ++i)
    {
        cout << "How many movies did student " << i + 1 << " see?" << endl;
        cin >> array[i];
    }



    median(*array[surveyed], surveyed);

}


double median(int *array[], int num)
{
    if (num % 2 != 0)
    {
        int temp = ((num + 1) / 2) - 1;
        cout << "The median of the number of movies seen by the students is " << array[temp] << endl;
    }
    else
    {
        cout << "The median of the number of movies seen by the students is " << array[(num / 2) - 1] << " and " << array[num / 2] << endl;
    }

}
4
  • 3
    So array is an array of int. On line 36 you do *array[surveyed]. This accesses the array array (bad naming here) at index surveyed. This gives an int which then the * tries to de-reference. An int is a primitive type, which isn't a pointer so can't be de-referenced. Commented Feb 11, 2015 at 19:34
  • iow, change *array[surveyed] to array. Commented Feb 11, 2015 at 19:37
  • 1
    So im still pretty new to C++ and have been doing a program for a while now So you're saying that you couldn't find one of the literally hundreds of thousands of examples showing you the proper way to pass an array and how to declare the function properly? Commented Feb 11, 2015 at 19:37
  • My advice, do not use new, delete, pointers (T*), c-arrays (T[]) , eof and read some books. Commented Feb 11, 2015 at 19:39

1 Answer 1

2

Problems:

  1. The expression *array[surveyed] used in the following line:

    median(*array[surveyed], surveyed);
    

    is not right. array[surveyed] is the surveyed-th element of the array. It is not a pointer. It doesn't make sense to dereference it.

  2. The type of the first argument of median used in the declaration is different than the type used in the definition. The declaration seems to be right one. Change the implementation to:

    double median(int *array, int num)
    
  3. Fix the way you call median. Instead of

    median(*array[surveyed], surveyed);
    

    use

    median(array, surveyed);
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you SO much for this. Also realized i had forgotten a return at the end of the function so after adding a good ol return 0; i was able to get it to run. Time to move onto the next function. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.