Thread: How to find the smallest number. (Writing a subprogram)

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    61

    How to find the smallest number. (Writing a subprogram)

    I got my test back today and my grade was a 71. I lost 20 points on my test because I didn't correctly write a function to find the smallest number. My final is on Thursday and I need to figure this out before then.
    The exam question is:
    "Write the arguments and code for a function called find_smallest() that takes an array of integers and a single integer as arguments. The function will search the array from the first element to the number of elements specified by the second argument and find the value of the smallest element in the array. This value is then returned to the calling function. In the main() program below, the call to find_smallest() would yield a value of z of -38."

    What I wrote is below.

    Code:
    #define NUM_ELEMENTS 7
    int find_smallest (int x[], int y)
    {
    int i, smallest, small;
    
    x[0] = x[smallest]
    
    for(i=0;i<y;i++){
    if(x[i] <= x[smallest])
      x[i]=small;
    return(small);
    
    }
    }
    here is the main that was given
    Code:
    void main (void)
    {
    int x[NUM_ELEMENTS] = {89,234,-3,0,45,-38,75};
    int y = NUM_ELEMENTS;
    int z;
    
    z= find_smallest(x,y);
    printf("Smallest value in the array is %d",z);
    
    }
    Last edited by november1992; 05-02-2012 at 03:55 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    x[0] = x[smallest]
    is an error
    Code:
    x[i]=small;
    is not right, think about it.
    Kurt
    Last edited by ZuK; 05-02-2012 at 04:23 PM.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Are you asking a question, or...?

    But while you posted your code, you should probably be aware of a couple things:
    - void main is deprecated, use int main and return 0
    - name your variables descriptively in functions, this is especially important because you're using "x" and "y" when your function has nothing to do with coordinates
    - you don't need to compare "less than or equal to", only "less than"
    - you should set "small" to x[0], instead of setting x[0] to a random index in the array. upon initialization, "smallest" could be anything.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    Well, I was asking how to write the function. I posted my code hoping someone would tell me what was wrong with it.
    I didn't write the main function, my teacher wrote it.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    See message #2 in the thread.

    What are the value of small and smallest throughout your function?

    Why do you think they have those values?

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    Quote Originally Posted by ZuK View Post
    Code:
    x[0] = x[smallest]
    is an error
    Kurt
    Yeah, I realized now that isn't what I meant to write. I was thinking of setting the initial element in the array as the smallest number and then I would create a for loop to test if the remaining numbers in the list are smaller.

    Code:
    #define NUM_ELEMENTS 7
    
    int find_smallest (int x[], int y)
    {
    int i,small;
    
     
    
    x[0] = small
    
     
    
    for(i=0;i<y;i++){
    if(x[i] < small)
    
      x[i]=small;
    return(small);
     
    }
    }

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    Quote Originally Posted by ZuK View Post
    Code:
    x[i]=small;
    is not right, think about it.
    Kurt
    I'm not sure why this is wrong though.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    For assignment, which side of the assignment operator is the one that receives the value from the expression and which side is the expression?

    You are putting small which has no initial value (so it could be anything) and putting its unknown value in the first array location x[0].
    Last edited by pheininger; 05-02-2012 at 07:15 PM.

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    Oh, I didn't think about that. So is this right?
    Code:
    #define NUM_ELEMENTS 7
    
    int find_smallest (int x[], int y)
    {
    int i,small;
    
     
    
    small = x[0] 
    
     
    
    for(i=0;i<y;i++){
    if(x[i] < small)
    
      small = x[i];
    return(small);
     
    }
    }

  10. #10
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    1) Missing a semi-colon on line 9. (You would have found this if you tried to compile it.)
    2) Poor indentation in all of it.
    3) After you fixed your indentation, you might have noticed that you will return from the function as soon as it looks at the x[0] entry in the for loop. Not after the entire array is checked.
    4) As mentioned in message 3, names of your parameters are poor. For example, the name y should probably be size or num_entries or some similar name.

    Warning: I also have not tried to compile it and test it so there may be other errors.
    Last edited by pheininger; 05-02-2012 at 07:48 PM. Reason: Added the issue about returning early and then fix that comment. Add reference to message 3.

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    Okay, I tested it now and it works. Thank you for the help.

  12. #12
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Also, please tell your teacher that actual programmers think he should teach good programming practice by:
    * not using "void main ()"
    * Return 0 from main()
    * Initializing all variables
    * Indenting code properly
    * Not using unnecessary variables like 'y'
    * terminating printf statements with a newline.

    It's a ten-line program he's written for you and it's appalling!
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    I'm just curious, what is wrong with using void main()? He never really explained the point of returning values.

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by november1992 View Post
    I'm just curious, what is wrong with using void main()? He never really explained the point of returning values.
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 09-23-2010, 10:12 PM
  2. recursive find of the smallest member..
    By transgalactic2 in forum C Programming
    Replies: 35
    Last Post: 01-13-2009, 09:21 AM
  3. need to find smallest int
    By lankeveil in forum C Programming
    Replies: 3
    Last Post: 11-30-2008, 05:48 AM
  4. Find the Largest and Smallest Number
    By Nightsky in forum C Programming
    Replies: 27
    Last Post: 09-04-2006, 03:40 PM
  5. find the k-th smallest element in O(logk) time
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-22-2001, 03:51 AM