1

I am trying to use pointers whenever possible in the following code and am having difficulty figuring out how, exactly, to institute the pointers and how to return a pointer value at the end of my first function. I have done some research on the subject but none of the methods I found have been helpful so far, so I was hoping you may have some specialized tips.

Note: I am a beginner.

#include <iostream>
using namespace std;

int mode(int *pies[], int size) {


    int count = 1;
    int max = 0;
    int *mode=pies[0];


    for (int i=0; i<size-1; i++)
    {
        if (pies[i] == pies[i+1])
        {
            count++;
            if (count>max)
            {
                max = count;
                mode = pies[i];
            
            }
    
        }
        else
            count = 1;
    
    }

    return *mode;
}


int main() {

    int n;

    cout<<"Input the number of people: "<<endl;

    cin>>n;

    int survey[n];

    cout << "Enter the amount of pie eaten by each person:" << endl;


    for(int i = 0; i < n; i++) {
    
        cout <<"Person "<<(i + 1)<< ": "<<endl;
    
        cin>>survey[i];
    
    }

    cout<<"Mode: "<<mode(survey, n)<< endl;

        return 0;


}
  • 6
    _trying to use pointers whenever possible_ Why? :/ Anyway, if you want to return a pointer, then return a pointer the way you would return any other data type. Of course, you'd have to change the function's return type to be a pointer as well. – ChrisMM Feb 22 '21 at 18:56
  • I will try that. Thank you. Do you have any advice for this error message in my main program? "No matching function for call to 'mode'" – W. W. Atkinson Feb 22 '21 at 18:58
  • 2
    `mode` takes `int**` parameter, you're passing `int*`. – Evg Feb 22 '21 at 18:59
  • How do I fix this? – W. W. Atkinson Feb 22 '21 at 19:00
  • 1
    Just use -- int mode(int* pies, int size) – Anon Feb 22 '21 at 19:01
  • 1
    Use `std::vector`. It is C++, not C. – Evg Feb 22 '21 at 19:01
  • Thank you. I appreciate it. – W. W. Atkinson Feb 22 '21 at 19:01
  • Now I am getting this error. "Expected '(' for function-style cast or type construction" when I use int mode(int* pies, int size). – W. W. Atkinson Feb 22 '21 at 19:03
  • 1
    `int survey[n];` is not standard c++. [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). I strongly suggest you to look at `std::vector`. Trying to do stuff manually with pointers but using a fancy compiler extension isnt consistent – 463035818_is_not_an_ai Feb 22 '21 at 19:04
  • Using namespace std; is a course requirement for me. – W. W. Atkinson Feb 22 '21 at 19:05
  • Thank you. I will try this. – W. W. Atkinson Feb 22 '21 at 19:08
  • That was very helpful. Thanks again. – W. W. Atkinson Feb 22 '21 at 19:11
  • 2
    Handy reading: [What is array to pointer decay?](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) TL;DR version: an array is not a pointer, but implicitly converts to one as required. As a result, you almost never pass an array because you quietly pass a pointer to the array. This behaviour can be annoying, but made perfect sense back in the old days when you did not have the resources to pass an array by value. – user4581301 Feb 22 '21 at 19:11

1 Answers1

1

Here is an attempt to answer. In your main(), you call the mode() function with mode(survey, n) while int survey[n]; is an array of int, so you may use int mode(int *pies, int size) instead of int mode(int *pies[], int size) (as the array int survey[n] can be implicitly converted into pointer).

However, you need to modify two more things in your function:

  • int *mode=pies[0]; is wrong as pies[0] is the first element of an array of int, thus is an int, while int* mode is a pointer on an int which is incompatible. mode should be an int to receive pies[0]. The correct code is then int mode = pies[0].
  • Your function signature is int mode(int *pies, int size), thus, again, you should return an int. You should then just return mode;

These are only hints on how to make the code compile. Your next step is to formalize what you would like it to do and then modify the code accordingly

NB: The correct practice is to think about what you would like to achieve first and then code afterwards (but let us say that this is for the sake of helping each other)

To get started using pointers, you may look at some simple tutorials at first:

Here is the modified code with the stated modifications above (it compiles):

#include <iostream>
using namespace std;

int mode(int *pies, int size) {


    int count = 1;
    int max = 0;
    int mode=pies[0];


    for (int i=0; i<size-1; i++)
    {
        if (pies[i] == pies[i+1])
        {
            count++;
            if (count>max)
            {
                max = count;
                mode = pies[i];

            }

        }
        else
            count = 1;

    }

    return mode;
}


int main() {

    int n;

    cout<<"Input the number of people: "<<endl;

    cin>>n;

    int survey[n];

    cout << "Enter the amount of pie eaten by each person:" << endl;


    for(int i = 0; i < n; i++) {

        cout <<"Person "<<(i + 1)<< ": "<<endl;

        cin>>survey[i];

    }

    cout<<"Mode: "<<mode(survey, n)<< endl;

        return 0;


}
Pat. ANDRIA
  • 2,330
  • 1
  • 13
  • 27