Please help to check my program

This is a discussion on Please help to check my program within the C Programming forums, part of the General Programming Boards category; Hi expert, I need to write a program, function one assigns the maximum and minimum values in intArray to the ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    Please help to check my program

    Hi expert,

    I need to write a program, function one assigns the maximum and minimum values in intArray to the variables pointed by min and max respectively. The parameter size is the size of intArray.

    Code:
    void findMinMax(int intArray[], int size, int* min, int* max)
    Function two returns the smallest positive integer (greater than zero) found in intArray.

    Code:
    int findSmallestPositive(int intArray[], int size)
    The array parameter should be traversed only once to maximize the running efficiency of
    the function.

    This is my script:
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    void findMinMax(int intArray[], int size, int* min, int* max) {
       
       size=2;
      
       if (min<max) {
           intArray[0]=min;
           intArray[1]=max; 
    	 }
       else  {
    	   intArray[0]=max;
           intArray[1]=min; 
    	 }
    ;
    }
    
    int findSmallestPositive(int intArray[], int size) {
       
       size=2;
       if (intArray[0]<intArray[1])
    	   return intArray[0];
       else
    	   return intArray[1];
    }
    Am my code writing correctly? please told me my error

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void findMinMax(int intArray[], int size, int* min, int* max)
    You're way off. Consider what you're being asked to do. Given an array, you need to find the smallest and largest out of all of the items in the array. This alone suggests a loop. You can only traverse the array once, so you need to save the smallest and largest so far. That way when you get to the end, you have the smallest and largest total. A good first try would look like this:
    Code:
    void findMinMax(int intArray[], int size, int* min, int* max)
    {
      int i;
    
      *min = *max = intArray[0];
      for ( i = 1; i < size; i++ ) {
        if ( intArray[i] < *min )
          *min = intArray[i];
        if ( intArray[i] > *max )
          *max = intArray[i];
      }
    }
    A similar algorithm can be used to find the smallest positive number, just add the extra test that if intArray[i] < min and intArray[i] is also greater than or equal to 0, you make the assignment.
    Last edited by Prelude; 03-10-2004 at 10:14 PM.
    My best code is written with the delete key.

  3. #3
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182
    Code:
    int findSmallestPositive(int intArray[], int size) {
       
       int i = 1;
       int spos = intArray[0];
    
       //assumes array index begins at 0 and ends at size - 1
       for(; i < size; i++)
       {
             if( (intArray[i] >= 0) && (intArray[i] < spos) )
                 spos = intArray[i];
       }
    
        return spos;
    
    }
    You'll probably want to break that findMin and findMax up into two functions. I'll leave that to you.

    [edit]
    oops, forgot return statement.
    [/edit]
    Last edited by spoon_; 03-10-2004 at 10:12 PM.
    {RTFM, KISS}

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    7
    how can I return two value of min and max in one function?



    Originally posted by Prelude
    >void findMinMax(int intArray[], int size, int* min, int* max)
    You're way off. Consider what you're being asked to do. Given an array, you need to find the smallest and largest out of all of the items in the array. This alone suggests a loop. You can only traverse the array once, so you need to save the smallest and largest so far. That way when you get to the end, you have the smallest and largest total. A good first try would look like this:
    Code:
    void findMinMax(int intArray[], int size, int* min, int* max)
    {
      int i;
    
      *min = *max = intArray[0];
      for ( i = 1; i < size; i++ ) {
        if ( intArray[i] < *min )
          *min = intArray[i];
        if ( intArray[i] > *max )
          *max = intArray[i];
      }
    }
    A similar algorithm can be used to find the smallest positive number, just add the extra test that if intArray[i] < min and intArray[i] is also greater than or equal to 0, you make the assignment.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how can I return two value of min and max in one function?
    You cannot return two values in C, only one. To return multiple values you must take a roundabout method such as using pointer arguments that reference the original objects or returning a structure of the values you want.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 01-09-2009, 09:45 PM
  2. Suggestions for my Check Book program
    By Nor in forum C++ Programming
    Replies: 2
    Last Post: 11-17-2008, 06:44 PM
  3. program to check hard disk transfer rate
    By shadow99er in forum C Programming
    Replies: 3
    Last Post: 03-01-2002, 05:04 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21