Thread: Help with max value of an array

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Help with max value of an array

    Hi, my program needs to return the max value of an array given the input of the user. I believe I am pretty close to getting it to work, but I know I have a few errors. Calling functions still confuses me and I think that is where I am going wrong.

    Here is my code:

    Code:
    #include<stdio.h>
    
    int findLargest(double *array, int arrayLength);
    
    int main()
    {
     double nums[80];
     int pos;
    
       for (pos=0; pos<10 && nums[pos]!=EOF; ++pos)
       {
        printf("Enter in a number or EOF to quit: ");
        scanf("%lf", &nums[pos]);
       }
    
       for (pos=0; pos<10; ++pos)
       {
       printf("%lf\n", nums[pos]);
       }
       
     double maximum = findLargest(nums, pos);
     int OS;
     
        if(pos<=0)
    	printf("There is no maximum element,\n");
    	else
        printf("The maximum value is %lf at offset %d\n", maximum, OS); 
    }
    
    int findLargest(double *array, int arrayLength)
    {
       if (arrayLength <= 0)
       {
       return -1;
       }
       
     int offset=0;
     int i;
     double max = array[0];
     
       for (i=0; i<arrayLength; ++i)
       {
          if (max < array[i])
    	  {
    	   max=array[1];
    	  }
       }
     
     return max;
    }
    Any help or suggestions would be greatly appreciated!!

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    What about functions do you not understand?

    1. nums[pos] will never equal EOF - you don't scanf() until inside the for() loop.
    2. never check for programmer error ... if (arrayLength <= 0). name one C function that attempts to protect you against your own bad programming.
    3. why would you return -1 to be stored as maximum value? that is not logical. AND you already checked if(pos <= 0).
    4. you never use variable offset in findLargest()
    5. you never assign a value to variable OS.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    I just get confused with exactly how to get what I want out of the function.

    Okay so I have fixed up most of what I need, the only part I really don't get right now is how to use pointers properly here. I need to get the values for both max and offset from my function into main and print them in my printf statement. How do I do this?


    This is my newer code:

    Code:
    #include<stdio.h>
    
    int findLargest(double *array, int arrayLength);
    
    int main()
    {
     double nums[80];
     int pos;
     
    
       for (pos=0; pos<10; ++pos)
       {
        printf("Enter in a number or EOF to quit: ");
        if(scanf("%lf", &nums[pos])<=0)
    	  break;
       }
    
     double maximum = findLargest(nums, pos);
     
        if(pos<=0)
       	  printf("There is no maximum element.\n");
    	else
          printf("The maximum value is %lf at offset %d\n", maximum, pos); 
    }
    
    int findLargest(double *array, int arrayLength)
    {
       if (arrayLength <= 0)
         return -1;
       
     int offset=0;
     int i;
     double max = array[0];
     
       for (i=0; i<arrayLength; ++i)
       {
          if (max < array[i])
    	    max=array[i];
    		offset=i;
       }
     
     return offset;
    }

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    A function can only return one value. If you need more than that from a function, you need to use pass by reference, and give the function variable addresses as parameters where you want the function to change a value for you. You then need to use indirection or de-referencing to affect the change.

    Code:
    void changeMyVars( int [], int len, char *middle) ;
    
    int main( void )
    {
        int arr[] = "Hello World" ;
        char letter = ' ' ;
    
        changeMyVars( arr, strlen(arr), &letter ) ;  // passing the address of letter to the function
    //...
    }
    
    void changeMyVars( int a[], int len, char *mid )  // variable pointer mid gets the address of letter as it's value
    {
         len /= 2 ;
    
         *mid = a[len] ;  //indirection assigns the value of a[len] to the address of letter
    }
    Cheesy little code to show how a function can directly change a variable that isn't in it's own scope. Hope the example helps.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Tclausex View Post
    A function can only return one value. If you need more than that from a function, you need to use pass by reference, and give the function variable addresses as parameters where you want the function to change a value for you. You then need to use indirection or de-referencing to affect the change.
    A function can actually return a struct, and a struct may contain multiple values, pointers, arrays, or even other structs.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  2. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  3. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM