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;


}
13
  • 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. Commented Feb 22, 2021 at 18:56
  • 2
    mode takes int** parameter, you're passing int*. Commented Feb 22, 2021 at 18:59
  • 1
    Just use -- int mode(int* pies, int size) Commented Feb 22, 2021 at 19:01
  • 1
    Use std::vector<int>. It is C++, not C. Commented Feb 22, 2021 at 19:01
  • 2
    Handy reading: 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. Commented Feb 22, 2021 at 19:11

1 Answer 1

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;


}
Sign up to request clarification or add additional context in comments.

Comments

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.