Return error

Hello guys, I'm trying to code a program where it asks the user to put a number represent the numbers in an array. The user then will enter the numbers in the array list.

I've made a few amendments. And the output is not like what I expect.

Assignment9_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

void minmax(double *numbers, int npts, double *min_ptr, double *max_ptr);

int main()
{
    int i, npts;
    double min, max;
    double arr[100];
    
    cout << "How many numbers would you like to enter?" << endl;
    
    cin >> npts;
    
    cout << "Enter the " << npts << " numbers separated by whitespace." << endl;
    
    for (i=0; i<npts; i++)
        cin >> arr[i];

    minmax(arr, npts, &min, &max);
    
    cout << "The min is " << min << " and the max is " << max << endl;
    
    return 0;
}


Assignment9_function.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Write your minmax function definition here
void minmax(double *numbers, int npts, double *min_ptr, double *max_ptr)
{
    int i;
    double ptr1, ptr2;
    double min, max;
    double arr[100];
    
	arr[npts];
    
    min = 0;
    max = 0;
    
    for(int i = 0; i < npts; i++)
    {
        if(arr[i] < min)
        min = arr[i];
    }
    
    for(int i = 0; i < npts; i++)
    {
        if(arr[i] > max)
        max = arr[i];
    }
    
    *min_ptr = min;
    *max_ptr = max;
}


1
2
3
4
5
How many numbers would you like to enter?                                                                                                                                                                                         
5                                                                                                                                                                                                                                 
Enter the 5 numbers separated by whitespace.                                                                                                                                                                                      
1 2 3 4 5                                                                                                                                                                                                                         
The min is 0 and the max is 6.9357e-310 


min should be 1 and max should be 5. I still don't understand why the output is not what I expected them to be.
Last edited on
You appear to be trying to return a pointer to the function, but your function is prototyped with a void return type, which means you shouldn't be returning anything. Your function implementation and your function prototype don't match as they should. Since you can't change the prototype you will need to change the implementation.



I don't have to return anything in both main and function?
You don't have to return anything from function void minmax(), first because the void return type prohibits it. Second, results are passed back to main via the pointer parameters min_ptr and max_ptr.

Also, the array defined in main() is passed via the first parameter, double *numbers. You should not be defining an additional array inside the function minmax (line 7).

Topic archived. No new replies allowed.