# Thread: Array with Pointers Help

1. ## Array with Pointers Help

I need to find the largest and smallest element in an array however I keep getting the first element in the array for both values when I run the program.

Can anyone tell me what I'm doing wrong?
Code:
```#include <stdio.h>
#include <math.h>

void finder(int data[], int* px, int* py, int* pn);

int main(void) {
int data[100], i, num, steps, temp;

int large, *pl; //*px
int small, *ps; //*py
int nextLarge, *pn; //*pz

pl = &large;
ps = &small;
pn = &nextLarge;

/*Enter elements in array */

printf("Enter the number of elements to be in array: ");
scanf("%d", &num);
for(i = 0; i < num; ++i)
{
printf("%d. Enter element: ", i+1);
scanf("%d", &data[i]);
}
for(steps = 0; steps < num; ++steps)
for(i = steps + 1; i < num; ++i)
{
if(data[steps] > data[i])
/* Selection sort begins */
{
temp = data[steps];
data[steps] = data[i];
data[i] = temp;
}
}
finder(data,pl, ps, pn);

printf("The smallest number is: %d\n", small);
printf("The largest number is: %d\n", large);

printf("Sorted array is: ");
for(i = 0; i < num; ++i)
printf("%d ", data[i]);

return 0;
}// end of main

void finder(int data[],int* px, int* py, int* pn){

int i, num;

/* Finds large number */
*px = data[0];
for(i = 1; i < num; i++){
if(data[i] > (int)*px)
*px = data[i];
}

/* Finds small number */
*py = data[0];
for(i = 1; i < num; ++i){
if(data[i] < (int)*py)
*py = data[i];
}
}//end of finder```

2. Are you sure all the values in the function have been initialized before use?

By the way since you seem to be sorting the array why not just wait until after the sort? After the sort you will know which element is the smallest and which is the largest without needing to search the array for them.

3. num is used uninitialized in finder.
`  finder(data, num, &large, &small);`