Pointers

closed account (G3AqfSEw)
Write a function find_min that takes a double pointer indicating the start of an array, and an int indicating the length of the array. It should return a pointer to the minimum number in the array.

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
  #include <iostream>
using namespace std;

double* find_min(double* a, int n) 
{
/*not sure what goes in here. this is what I have so far:
double *min= a;
for (int i=0; i<n; i++) {...
*/
}

int main() {
   int n;
   cin >> n;
   double arr[100];
   for (int i=0; i<n; i++) cin >> arr[i];

   // call your function and save result
   double* min_loc = find_min(arr, n);
   
   cout << ...;    // there is some hidden testing code here
   cout << "Pointed-to value is " << *min_loc << endl;

   // change that variable to 1000
   *min_loc = 1000;
   // run your code again
   min_loc = find_min(arr, n);
   
   cout << ...;    // there is some hidden testing code here
   cout << "Pointed-to value is " << *min_loc << endl;
}
min = a[0];
for(lc = 1; lc < n; lc++)
{
so if the current one, a[lc] is less than the stored one (min)... do something.
can you get it now?

}

you may check a against null before doing anything at all if you want to be extra clean.
Last edited on
Topic archived. No new replies allowed.