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

1. 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);

}```

2. Code:
`x[0] = x[smallest]`
is an error
Code:
`x[i]=small;`
is not right, think about it.
Kurt

3. 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. 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. 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. Originally Posted by ZuK
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. Originally Posted by ZuK
Code:
`x[i]=small;`
is not right, think about it.
Kurt
I'm not sure why this is wrong though.

8. 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].

9. 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. 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.

11. Okay, I tested it now and it works. Thank you for the help.

12. 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!

13. I'm just curious, what is wrong with using void main()? He never really explained the point of returning values.

14. Originally Posted by november1992
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