Thread: Array with Pointers Help

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    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. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,606
    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. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,127
    num is used uninitialized in finder.
    Your compiler should warn you about that.
    You need to pass in num.

    And you don't need those stupid pointers.
    Just call finder like this:
    Code:
      finder(data, num, &large, &small);
    (I didn't see the point of nextLarge, so I removed it.)
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extract numbers from char array into array of pointers
    By lmanukyan in forum C Programming
    Replies: 5
    Last Post: 10-05-2016, 05:44 PM
  2. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. Replies: 4
    Last Post: 07-01-2009, 01:53 PM
  5. Replies: 1
    Last Post: 10-21-2007, 07:44 AM

Tags for this Thread