Home > Mobile >  Calculates the position of the max element for array. function returns the max element. pass the arr
Calculates the position of the max element for array. function returns the max element. pass the arr

Time:01-20

i have a little problem with my college assignment. I don't really understand what's going on with pointers and reference. Could someone point me where I am making a mistake??

using namespace std;
int i, n, maax, *position;
void tablica()
{
    int tab[n];
    cout<<"enter data:"<<endl;
    for (i=0; i<n; i  )
    {
        cin>>tab[i];
    }
    maax = tab[0];
    for (i=0; i<n; i  )
    {
        if (maax<tab[i])
        {
            maax=tab[i];
            *position=i 1;
        }
    }
}

int main()
{
    cout<<"array size:"<<endl;
    cin>>n;
    tablica();
    cout<<"max el. position is: "<<&position<<endl;
    return 0;
}

CodePudding user response:

You shouldn't use global variables (see Are global variables bad?). int tab[n]; is not standard C , its a variable length array that is only available as extension on some compilers (see Why aren't variable-length arrays part of the C standard?). The segfault is because you never allocate memory for the position, it is initialized because its a global, but it doesnt point to an int (see Is dereferencing a pointer that's equal to nullptr undefined behavior by the standard?).

You do not need any array to get the max value and position. And there is no need to use a pointer in your code. Determine the maximum value and position in the same loop that is reading the input and return the result from the function instead of using the global variable:

#include <iostream>

int tablica(int n) {
    std::cout<<"enter data:\n";
    
    int max = 0;
    int max_pos = 0;
    std::cin >> max;
    
    for (int i=1; i<n; i  ) {
        int number = 0;
        std::cin>>number;        
        if (max<number) {
            max=number;
            max_pos = i;
        }
    }
    return max_pos;
}

int main()
{
    std::cout<<"input size:\n";
    int n;
    std::cin>>n;
    int position = tablica(n);
    std::cout<<"max el. position is: "<< position << "\n";
    return 0;
}

CodePudding user response:

Look at what the function should do:

"function returns the max element. pass the array by the pointer and the pos. by the reference"

It should not read any array elements.
It should not receive or return values in global variables.
It should not use a pointer to the position.

It should be passed an array (as a pointer) and somewhere to store the maximum position (as a reference), and return the maximum value.

That is, its prototype should look like

int tablica(const int* input, int size, int& max_position)

and main should look something like this:

int main()
{
    int n = 0;
    cout << "Array size: " << endl;
    cin >> n;
    int* data = new int[n];
    for (int i = 0; i < n; i  )
    {
        cin >> data[i];   
    }
    int position = -1;
    int max_element = tablica(data, n, position);
    cout << "The maximum element is " << max_element << ", at index " << position << endl;
    delete [] data;
}

Implementing tablica left as an exercise.

  •  Tags:  
  • Related