Displaying output of prototype function in main function

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.
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).
I did that. This error displayed.

1
2
3
4
Assignment9_function.cpp: In function β€˜void minmax(double*, int, double*, double*)’:                                                                                                                                              
Assignment9_function.cpp:8:2: error: β€˜arr’ was not declared in this scope                                                                                                                                                         
  arr[npts];                                                                                                                                                                                                                      
  ^                                     
You dont declare array in your function definition so it wont accept it when you try to pass it.
^I did this:

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
// 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];
    
    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;
}


and the output:
How many numbers would you like to enter?                                                                                                                                                                                         
5                                                                                                                                                                                                                                 
Enter the 5 numbers separated by whitespace.                                                                                                                                                                                      
5 4 3 2 1                                                                                                                                                                                                                         
The min is 0 and the max is 6.94525e-310
Compare the two functions here. Both do exactly the same thing:
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
#include <iostream>

using namespace std;

void output (double array[], int size)
{
    cout << "output: ";
    for (int i= 0; i<size; ++i)
        cout << array[i] << ' ';
    cout << '\n';
}

void print (double *numbers, int npts)
{
    cout << "print:  ";
    for (int i= 0; i<npts; ++i)
        cout << numbers[i] << ' ';
    cout << '\n';
}

int main()
{
    double array[3] = {123, 3.14, 76543 };
    
    output(array, 3);
    print(array, 3);
}
output: 123 3.14 76543
print:  123 3.14 76543


Notice
1. the syntax array[] and *array have the same effect when passing an array as a function parameter.

2. the name of the parameter in the function need not be the same as in main().
Also, instead of
1
2
    double min = 0;
    double max = 0;
try
1
2
    double min = numbers[0];
    double max = numbers[0];

i.e. use the first array element as the initial value.
Topic archived. No new replies allowed.