-4

This is the problem that I'm trying to solve for class in C++.

Write a function that returns a pointer to the maximum value of an array of floating-point data: double* maximum(double* a, int size). If size is 0, return nullptr.

The issues I'm having are that:

  1. The final output is not the correct location for the maximum value in the array.

  2. An error that says: "cannot convert 'double**' to 'double*' in the initialization".

  3. If I use nullptr at any point in this code, CodeBlocks gives me an error.

#include <iostream>
using namespace std;

// return pointer to location from function
double * maximum(double* a, int size)
{
    double maxVal = a[0]; // this is the starting max value
    double* max_pos = &a; // points to the value in a[0]
    // initialis]ze both variables
    for(int i = 0; i < size; i++){

        if(a[i] > maxVal){
            maxVal = a[i];
            cout << max_pos << endl;
            max_pos = &a[i];
        }
    }
    // return address
    return max_pos;
}

int main()
{
    double myarr[5];
    int i = 0;
    int arrSize = 5;

    cout << "Input 5 floating point values for your array" << endl;

    for(i = 0; i < arrSize; i++){ // loop to input values

        cin >> myarr[i];

    }
    for(int j = 0; j < arrSize; j++){
        cout << "Location for " << myarr[j] << " = " << &myarr[j] << endl;
    }
    double* maxNum = maximum( myarr, arrSize);     
    cout << &maxNum << endl;


    return 0;
}

This is the output I'm getting after finding max_pos:

image

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
rmarti89
  • 3
  • 1
  • 3
  • `nullptr` does not appear to be defined, so why *wouldn't* you expect an error if you try to use it? – Scott Hunter Jan 30 '18 at 01:52
  • 1
    `double* max_pos = &a;` looks a little odd to me. It takes the address of a pointer and tries to assign it to a pointer, not a pointer to a pointer. – user4581301 Jan 30 '18 at 01:53
  • How do you know the address being printed is wrong? How do you know what the *correct* address should be? – Scott Hunter Jan 30 '18 at 01:53
  • you're printing the address of the pointer in the last line `cout << &maxNum << end;` Just print the pointer itself `cout << maxNum << endl;` – twain249 Jan 30 '18 at 01:53
  • @ScottHunter What do you mean? Also I didn't add it on this code cause I was trying to get it to run without it first. But I've gotten the error before in another program I've had to use it for. – rmarti89 Jan 30 '18 at 01:54
  • &a doesn’t point to the value in a[0]; a does – Rodolfo Jan 30 '18 at 01:55
  • 1
    `nullptr` is most likely missing because C++11 support has not been turned on. Give [How can I add C++11 support to Code::Blocks compiler?](https://stackoverflow.com/questions/18174988/how-can-i-add-c11-support-to-codeblocks-compiler) a read. – user4581301 Jan 30 '18 at 01:55
  • on main() - I have each value listed with its location to test the code and it doesn't come out right. – rmarti89 Jan 30 '18 at 01:55
  • @user4581301 Are you saying I need to change `&a` to `a`?? And thanks a bunch for that link for helping me fix `nullptr` it worked on my other code. – rmarti89 Jan 30 '18 at 01:59
  • No I wasn't, I wasn't suggesting any solution because I'm a jerk. That is the correct thing to do, though. – user4581301 Jan 30 '18 at 02:01
  • Have you tried using a debugger? – user202729 Jan 30 '18 at 02:06
  • I haven't, I'm a super noob. I'm still learning my way around this complier I'm using. – rmarti89 Jan 30 '18 at 02:10
  • @RicardoMartinez `{return std::max_element(a, a + size);}` -- Wow, that was simple. – PaulMcKenzie Jan 30 '18 at 02:43

1 Answers1

0

The code you showed has a few mistakes in it:

  1. using namespace std; is bad!

  2. you are not following your instructions to return nullptr when size is 0.

  3. you are trying to initialize max_pos (a double*) with &a (a double**), which is a compiler error.

  4. you are passing &maxNum (a double**) to std::cout, printing the address of the maxNum variable itself, not the address that it is pointing to (the found array element). You need to pass maxNum (a double*) if you want to print the address of the found element, or pass *maxNum (a double) if you want to print the value of the found element.

Try something more like this instead:

#include <iostream>

// return pointer to location from function
double* maximum(double *a, int size)
{
    if (size == 0) return 0;

    // initialize both variables
    double* max_pos = a; // points to the value in a[0]
    double maxVal = *max_pos; // this is the starting max value
    std::cout << "max_pos = " << max_pos << " (" << maxVal << ")" << std::endl;

    for(int i = 1; i < size; ++i){
        if (a[i] > maxVal){
            max_pos = &a[i];
            maxVal = *max_pos;
            std::cout << "max_pos = " << max_pos << " (" << maxVal << ")" << std::endl;
        }
    }

    // return address
    return max_pos;
}

int main()
{
    const int arrSize = 5;
    double myarr[arrSize];

    std::cout << "Input " << arrSize << " floating point values for your array" << std::endl;

    for(int i = 0; i < arrSize; ++i) { // loop to input values
        std::cin >> myarr[i];
    }

    for(int j = 0; j < arrSize; ++j) {
        std::cout << "Location for " << myarr[j] << " = " << &myarr[j] << std::endl;
    }

    double* maxNum = maximum(myarr, arrSize);
    std::cout << "maxNum = " << maxNum << " (" << *maxNum << ")" << std::endl;

    return 0;
}

Live Demo

And then, you can throw it all away and use STL algorithms instead, like std::max_element():

#include <iostream>
#include <algorithm>    
#include <iterator>

int main()
{
    const int arrSize = 5;
    double myarr[arrSize];

    std::cout << "Input " << arrSize << " floating point values for your array" << std::endl;

    // loop to input values
    std::copy_n(std::istream_iterator<double>(std::cin), arrSize, myarr);

    for(int i = 0; i < arrSize; ++i) {
        std::cout << "Location for " << myarr[i] << " = " << &myarr[i] << std::endl;
    }

    double *maxNum = std::max_element(myarr, myarr + arrSize);
    std::cout << "maxNum = " << maxNum << " (" << *maxNum << ")" << std::endl;

    return 0;
}

Live Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Just curious, but why do you use STL algorithms instead? For every class that I've done, we've never been shown this. – rmarti89 Jan 31 '18 at 05:17
  • @RicardoMartinez because C++ classes tend to not teach *real world* C++, they focus more on C-isms, algorithm theories for things that the STL employs (case in point - a manual implementation of `std::max_element()`), etc, while *real* programmers in *real world* situations do use the STL. It is standardized, safer, and saves time and effort. – Remy Lebeau Jan 31 '18 at 09:37