Using a function with pointers to find a min value in array

Hello everyone,
I'm not sure why my pointer is not returning the proper minimum value when the program runs.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #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;
}

void minmax(double *numbers, int npts, double *min_ptr, double *max_ptr)
{
    int i; //for loop index
    
    min_ptr = numbers;        
    for (i=1; i<npts; i++)
    {
        if (*numbers + i < *min_ptr); 
            {
    			cout << *(numbers + i) << endl;
                min_ptr = (numbers + i);
              
            }
    }
    
        
    return;
    
}
try something like
1
2
3
4
5
6
7
8
9
double minval = numbers[0];
double maxval = numbers[0];

for (int i = 1; i < npts; ++i) {
  if (numbers[i] < minval)
    minval = numbers[i];
  else if (numbers[i] > maxval)
    maxval = numbers[i];
}
Topic archived. No new replies allowed.